trailofbits / dmarc

Ruby DMARC Parser
https://rubygems.org/gems/dmarc
MIT License
25 stars 14 forks source link

Repetitive pattern in known tag definitions #7

Closed nisanthchunduru closed 9 years ago

nisanthchunduru commented 10 years ago

wsp? >> str('=') >> wsp? is repetitive among definitions of known tags like dmarc_version, dmarc_policy etc.

Since they are essentially tag value pairs, should we extract it out into a method/dsl?

class Parser < Parslet::Parser

  def tag_value_pair(tag_definition, value_definition)
    tag_definition >>
    wsp? >> str('=') >> wsp? >>
    value_definition
  end
rule(:dmarc_version) do
  tag_value_pair(
    str('v'),
    str('DMARC1').as(:v)
  )
end

in place of

rule(:dmarc_version) do
  str('v') >> wsp? >>
  str('=') >> wsp? >>
  str('DMARC1').as(:v)
end

and

rule(:dmarc_request) do
  tag_value_pair(
    str('p'),
    (
      str('none') |
      str('quarantine') |
      str('reject')
    ).as(:p)
  )
end

in place of

rule(:dmarc_request) do
  str('p') >> wsp? >> str('=') >> wsp? >> (
    str('none') |
    str('quarantine') |
    str('reject')
  ).as(:p)
end
postmodern commented 10 years ago

We should make a class method for this called tag_rule.