class Dependabot::Cargo::Requirement

Constants

PATTERN
PATTERN_RAW

Public Class Methods

new(*requirements) click to toggle source
Calls superclass method
# File lib/dependabot/cargo/requirement.rb, line 43
def initialize(*requirements)
  requirements = requirements.flatten.flat_map do |req_string|
    req_string.split(",").map(&:strip).map do |r|
      convert_rust_constraint_to_ruby_constraint(r.strip)
    end
  end

  super(requirements)
end
parse(obj) click to toggle source

Use Cargo::Version rather than Gem::Version to ensure that pre-release versions aren't transformed.

# File lib/dependabot/cargo/requirement.rb, line 23
def self.parse(obj)
  return ["=", Cargo::Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

  unless (matches = PATTERN.match(obj.to_s))
    msg = "Illformed requirement [#{obj.inspect}]"
    raise BadRequirementError, msg
  end

  return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"

  [matches[1] || "=", Cargo::Version.new(matches[2])]
end
requirements_array(requirement_string) click to toggle source

For consistency with other langauges, we define a requirements array. Rust doesn't have an `OR` separator for requirements, so it always contains a single element.

# File lib/dependabot/cargo/requirement.rb, line 39
def self.requirements_array(requirement_string)
  [new(requirement_string)]
end

Private Instance Methods

convert_caret_req(req_string) click to toggle source
# File lib/dependabot/cargo/requirement.rb, line 86
def convert_caret_req(req_string)
  version = req_string.gsub(/^\^/, "")
  parts = version.split(".")
  first_non_zero = parts.find { |d| d != "0" }
  first_non_zero_index =
    first_non_zero ? parts.index(first_non_zero) : parts.count - 1
  upper_bound = parts.map.with_index do |part, i|
    if i < first_non_zero_index then part
    elsif i == first_non_zero_index then (part.to_i + 1).to_s
    else 0
    end
  end.join(".")

  [">= #{version}", "< #{upper_bound}"]
end
convert_rust_constraint_to_ruby_constraint(req_string) click to toggle source
# File lib/dependabot/cargo/requirement.rb, line 55
def convert_rust_constraint_to_ruby_constraint(req_string)
  if req_string.include?("*")
    ruby_range(req_string.gsub(/(?:\.|^)[*]/, "").gsub(/^[^\d]/, ""))
  elsif req_string.match?(/^~[^>]/) then convert_tilde_req(req_string)
  elsif req_string.match?(/^[\d^]/) then convert_caret_req(req_string)
  elsif req_string.match?(/[<=>]/) then req_string
  else ruby_range(req_string)
  end
end
convert_tilde_req(req_string) click to toggle source
# File lib/dependabot/cargo/requirement.rb, line 65
def convert_tilde_req(req_string)
  version = req_string.gsub(/^~/, "")
  parts = version.split(".")
  parts << "0" if parts.count < 3
  "~> #{parts.join('.')}"
end
ruby_range(req_string) click to toggle source
# File lib/dependabot/cargo/requirement.rb, line 72
def ruby_range(req_string)
  parts = req_string.split(".")

  # If we have three or more parts then this is an exact match
  return req_string if parts.count >= 3

  # If we have no parts then the version is completely unlocked
  return ">= 0" if parts.count.zero?

  # If we have fewer than three parts we do a partial match
  parts << "0"
  "~> #{parts.join('.')}"
end