Class: Bibliothecary::Parsers::Cargo

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/cargo.rb

Class Method Summary collapse

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bibliothecary/parsers/cargo.rb', line 8

def self.mapping
  {
    match_filename("Cargo.toml") => {
      kind: "manifest",
      parser: :parse_manifest,
    },
    match_filename("Cargo.lock") => {
      kind: "lockfile",
      parser: :parse_lockfile,
    },
  }
end

.parse_lockfile(file_contents, options: {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bibliothecary/parsers/cargo.rb', line 48

def self.parse_lockfile(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)
  manifest.fetch("package", []).map do |dependency|
    next if !dependency["source"] || !dependency["source"].start_with?("registry+")

    Dependency.new(
      name: dependency["name"],
      requirement: dependency["version"],
      type: "runtime",
      source: options.fetch(:filename, nil)
    )
  end
    .compact
end

.parse_manifest(file_contents, options: {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bibliothecary/parsers/cargo.rb', line 25

def self.parse_manifest(file_contents, options: {})
  manifest = Tomlrb.parse(file_contents)

  parsed_dependencies = []

  manifest.fetch_values("dependencies", "dev-dependencies").each_with_index do |deps, index|
    parsed_dependencies << deps.map do |name, requirement|
      if requirement.respond_to?(:fetch)
        requirement = requirement["version"] or next
      end

      Dependency.new(
        name: name,
        requirement: requirement,
        type: index.zero? ? "runtime" : "development",
        source: options.fetch(:filename, nil)
      )
    end
  end

  parsed_dependencies.flatten.compact
end