class Bibliothecary::Parsers::Cargo

Public Class Methods

mapping() click to toggle source
# File lib/bibliothecary/parsers/cargo.rb, line 6
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: {}) click to toggle source
# File lib/bibliothecary/parsers/cargo.rb, line 44
def self.parse_lockfile(file_contents, options: {}) # rubocop:disable Lint/UnusedMethodArgument
  manifest = Tomlrb.parse(file_contents)
  manifest.fetch("package",[]).map do |dependency|
    next if not dependency["source"] or not dependency["source"].start_with?("registry+")
    Dependency.new(
      name: dependency["name"],
      requirement: dependency["version"],
      type: "runtime",
    )
  end
    .compact
end
parse_manifest(file_contents, options: {}) click to toggle source
# File lib/bibliothecary/parsers/cargo.rb, line 23
def self.parse_manifest(file_contents, options: {}) # rubocop:disable Lint/UnusedMethodArgument
  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",
      )
    end
  end

  parsed_dependencies.flatten.compact
end