Skip to content

Commit

Permalink
feat: add support for Cargo
Browse files Browse the repository at this point in the history
Signed-off-by: Luke-zhang-04 <luke.zhang2004dev@gmail.com>
  • Loading branch information
Luke-zhang-04 committed Nov 4, 2020
1 parent deefcb5 commit f5ea1f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ strum = { version = "0.19.5", features = ["derive"] }
image = "0.23.11"
regex = "1"
error-chain = "0.12"
toml = "0.5.7"

[target.'cfg(windows)'.dependencies]
ansi_term = "0.12"
Expand Down
15 changes: 14 additions & 1 deletion src/onefetch/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
};

pub enum PackageManager {
Cargo,
GoModules,
Npm,
Pip,
Expand All @@ -13,6 +14,7 @@ pub enum PackageManager {
impl std::fmt::Display for PackageManager {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
PackageManager::Cargo => write!(f, "Cargo"),
PackageManager::GoModules => write!(f, "Go Modules"),
PackageManager::Npm => {
if std::path::Path::new("./yarn.lock").exists() {
Expand All @@ -34,7 +36,16 @@ pub struct Detector {

// Package parsers go here. Parsers should take stirng contents and output a i32
mod package_parsers {
use regex::Regex;
use {
regex::Regex,
toml::Value,
};

pub fn cargo(contents: &str) -> Option<i32> {
let parsed = contents.parse::<Value>().unwrap();

Some(parsed["dependencies"].as_table().unwrap().len() as i32)
}

pub fn gomodules(contents: &str) -> Option<i32> {
let count = Regex::new(r"v[0-9]+").unwrap().find_iter(contents).count();
Expand All @@ -60,6 +71,8 @@ impl Detector {
let mut package_managers: HashMap<String, (DependencyParser, PackageManager)> =
HashMap::new();

package_managers
.insert(String::from("Cargo.toml"), (package_parsers::cargo, PackageManager::Cargo));
package_managers.insert(
String::from("go.mod"),
(package_parsers::gomodules, PackageManager::GoModules),
Expand Down

0 comments on commit f5ea1f7

Please sign in to comment.