Class: RustLang

Inherits:
Language show all
Extended by:
LanguageVersionHelpers
Defined in:
lib/cutting_edge/langs/rust.rb

Constant Summary collapse

API_URL =
'https://crates.io/api/v1/crates'
CARGOFILE_SECTIONS =
{
  :runtime => 'dependencies',
  :development => 'dev-dependencies',
  :build => 'build-dependencies'
}

Class Method Summary collapse

Methods inherited from Language

#website

Methods included from LanguageHelpers

#dependency_with_latest, #log_error, #unknown_dependency

Class Method Details

.latest_version(name) ⇒ Object

Find the latest versions of a dependency by name

name - String name of the dependency

Returns a Gem::Version



43
44
45
46
47
48
49
50
51
52
# File 'lib/cutting_edge/langs/rust.rb', line 43

def latest_version(name)
  begin
    content = HTTP.timeout(::CuttingEdge::LAST_VERSION_TIMEOUT).get(::File.join(API_URL, name)).parse
    version = content['crate']['max_version']
    Gem::Version.new(canonical_version(version))
  rescue StandardError, HTTP::Error => e
    log_error("Encountered error when fetching latest version of #{name}: #{e.class} #{e.message}")
    nil
  end      
end

.locations(name = nil) ⇒ Object

Defaults for projects in this language



18
19
20
# File 'lib/cutting_edge/langs/rust.rb', line 18

def locations(name = nil)
  ['Cargo.toml']
end

.parse_file(name, content) ⇒ Object

Parse a dependency file

name - String contents of the file content - String contents of the file

Returns an Array of tuples of each dependency and its latest version: [[<Gem::Dependency>, <Gem::Version>]]



32
33
34
35
36
# File 'lib/cutting_edge/langs/rust.rb', line 32

def parse_file(name, content)
  return nil unless content
  results = parse_toml(content, CARGOFILE_SECTIONS)
  dependency_with_latest(results) if results
end

.translate_requirement(req) ⇒ Object

Translate Cargo version requirement syntax to a String or Array of Strings that Gem::Dependency.new understands Cargo.toml files support * and ^ (wildcard and caret) requirements, which Ruby does not See: doc.rust-lang.org/cargo/reference/specifying-dependencies.html

req - String version requirement

Returns a translated String version requirement



61
62
63
64
65
66
67
68
69
# File 'lib/cutting_edge/langs/rust.rb', line 61

def translate_requirement(req)
  if req =~ /~|<|>|\*|=/
    return translate_wildcard(req) if req =~ /\*/
    req.sub!('~', '~>')
    req
  else
    translate_caret(req)
  end
end

.website(name) ⇒ Object



22
23
24
# File 'lib/cutting_edge/langs/rust.rb', line 22

def website(name)
  "https://crates.io/crates/#{name}"
end