Skip to main content

package_parser/pkgs/
rubygems.rs

1use crate::error::SourcePkgError;
2use crate::pkgs::common::model::{Package, PackageManifest};
3use crate::pkgs::spec::Spec;
4
5use std::path::Path;
6
7pub struct RubyGems {}
8
9impl RubyGems {
10    pub fn new() -> Self {
11        Self {}
12    }
13}
14
15#[async_trait::async_trait]
16impl PackageManifest for RubyGems {
17    fn get_name(&self) -> String {
18        "gem".to_string()
19    }
20
21    async fn recognize(&self, path: &Path) -> Result<Package, SourcePkgError> {
22        let spec = Spec::new();
23        let spec_info = spec.parse_spec(path)?;
24
25        let package = Package {
26            name: spec_info.name.unwrap_or_default(),
27            version: spec_info.version.unwrap_or_default(),
28            primary_language: "Ruby".into(),
29            declared_license: spec_info.license.unwrap_or_default(),
30            ..Default::default()
31        };
32
33        Ok(package)
34    }
35
36    fn file_name_patterns(&self) -> &'static [&'static str] {
37        &["*.gemspec"]
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use std::path::PathBuf;
45
46    #[tokio::test]
47    async fn test_ruby_gems() {
48        let filepath = PathBuf::from(concat!(
49            env!("CARGO_MANIFEST_DIR"),
50            "/testdata/rubygems/gemspec/arel.gemspec"
51        ));
52
53        let parser = RubyGems::new();
54        let p = parser.recognize(&filepath).await.unwrap();
55        println!("{:?}", p);
56    }
57}