Sha256: f15cda941c08cc72c5e5c141d03c1d7b65a78a7863eb648b0485265c450ebb1b

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 KB

Contents

module Bibliothecary
  module Parsers
    class Cargo
      include Bibliothecary::Analyser

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

      add_multi_parser(Bibliothecary::MultiParsers::CycloneDX)
      add_multi_parser(Bibliothecary::MultiParsers::Spdx)
      add_multi_parser(Bibliothecary::MultiParsers::DependenciesCSV)

      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

      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
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bibliothecary-12.0.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-11.0.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-11.0.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.2.4 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.2.3 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.2.2 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.2.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.1.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-10.0.0 lib/bibliothecary/parsers/cargo.rb