Class: Packages::Debian::ParseDebian822Service

Inherits:
Object
  • Object
show all
Defined in:
app/services/packages/debian/parse_debian822_service.rb

Overview

Parse String as Debian RFC822 control data format manpages.debian.org/unstable/dpkg-dev/deb822.5

Constant Summary collapse

InvalidDebian822Error =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ ParseDebian822Service

Returns a new instance of ParseDebian822Service.


10
11
12
# File 'app/services/packages/debian/parse_debian822_service.rb', line 10

def initialize(input)
  @input = input
end

Instance Method Details

#executeObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/packages/debian/parse_debian822_service.rb', line 14

def execute
  output = {}
  @input.each_line('', chomp: true) do |block|
    section = {}
    section_name, field = nil
    block.each_line(chomp: true) do |line|
      next if comment_line?(line)

      if continuation_line?(line)
        raise InvalidDebian822Error, "Parse error. Unexpected continuation line" if field.nil?

        section[field] += "\n"
        section[field] += line[1..] unless paragraph_separator?(line)
      elsif match = match_section_line(line)
        section_name = match[:name] if section_name.nil?
        field = match[:field]

        raise InvalidDebian822Error, "Duplicate field '#{field}' in section '#{section_name}'" if section.include?(field)

        section[field] = match[:value]
      else
        raise InvalidDebian822Error, "Parse error on line #{line}"
      end
    end

    raise InvalidDebian822Error, "Duplicate section '#{section_name}'" if output[section_name]

    output[section_name] = section
  end

  output
end