<commit_before>require 'toml'

module Bibliothecary
  module Parsers
    class Cargo
      PLATFORM_NAME = 'cargo'

      def self.parse(filename, file_contents)
        toml = TOML.parse(file_contents)
        if filename.match(/Cargo\.toml$/)
          parse_manifest(toml)
        elsif filename.match(/Cargo\.lock$/)
          parse_lockfile(toml)
        else
          []
        end
      end

      def self.analyse(folder_path, file_list)
        [analyse_cargo_toml(folder_path, file_list),
          analyse_cargo_lock(folder_path, file_list)]
      end

      def self.analyse_cargo_toml(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/Cargo\.toml$/) }
        return unless path

        manifest = TOML.load_file(path)

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_manifest(manifest)
        }
      end

      def self.parse_manifest(manifest)
        manifest.fetch('dependencies', []).map do |name, requirement|
          {
            name: name,
            requirement: requirement,
            type: 'runtime'
          }
        end
      end

      def self.analyse_cargo_lock(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/Cargo\.lock$/) }
        return unless path

        manifest = TOML.load_file(path)

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_lockfile(manifest)
        }
      end

      def self.parse_lockfile(manifest)
        manifest.fetch('package',[]).map do |dependency|
          {
            name: dependency['name'],
            requirement: dependency['version'],
            type: 'runtime'
          }
        end
      end
    end
  end
end<commit_msg>correctly return all potential cargo files, not just the first of each<commit_after>require 'toml'

module Bibliothecary
  module Parsers
    class Cargo
      PLATFORM_NAME = 'cargo'

      def self.parse(filename, file_contents)
        toml = TOML.parse(file_contents)
        if filename.match(/Cargo\.toml$/)
          parse_manifest(toml)
        elsif filename.match(/Cargo\.lock$/)
          parse_lockfile(toml)
        else
          []
        end
      end

      def self.analyse(folder_path, file_list)
        [analyse_cargo_toml(folder_path, file_list),
          analyse_cargo_lock(folder_path, file_list)].flatten
      end

      def self.analyse_cargo_toml(folder_path, file_list)
        paths = file_list.select{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/Cargo\.toml$/) }
        return unless paths.any?

        paths.map do |path|
          manifest = TOML.load_file(path)

          {
            platform: PLATFORM_NAME,
            path: path,
            dependencies: parse_manifest(manifest)
          }
        end
      end

      def self.parse_manifest(manifest)
        manifest.fetch('dependencies', []).map do |name, requirement|
          {
            name: name,
            requirement: requirement,
            type: 'runtime'
          }
        end
      end

      def self.analyse_cargo_lock(folder_path, file_list)
        paths = file_list.select{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/Cargo\.lock$/) }
        return unless paths.any?

        paths.map do |path|
          manifest = TOML.load_file(path)

          {
            platform: PLATFORM_NAME,
            path: path,
            dependencies: parse_lockfile(manifest)
          }
        end
      end

      def self.parse_lockfile(manifest)
        manifest.fetch('package',[]).map do |dependency|
          {
            name: dependency['name'],
            requirement: dependency['version'],
            type: 'runtime'
          }
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10785584651283381, 'fraction_numerical': 0.0, 'mean_word_length': 1.838852097130243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3609713', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1147, 'n_words': 224}