daipendency_extractor/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#[derive(Debug)]
pub struct PackageMetadata {
    pub name: String,
    pub version: Option<String>,
    pub documentation: String,
    pub entry_point: std::path::PathBuf,
}

#[derive(Debug)]
pub struct Namespace {
    pub name: String,
    pub symbols: Vec<Symbol>,
    pub doc_comment: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Symbol {
    pub name: String,
    pub source_code: String,
}

impl Namespace {
    #[cfg(test)]
    pub fn get_symbol(&self, name: &str) -> Option<&Symbol> {
        self.symbols.iter().find(|s| s.name == name)
    }
}

#[cfg(test)]
mod tests {
    use assertables::{assert_none, assert_some};

    use super::*;

    #[test]
    fn get_symbol_found() {
        let symbol = Symbol {
            name: "test_symbol".to_string(),
            source_code: "fn test() {}".to_string(),
        };
        let namespace = Namespace {
            name: "test_namespace".to_string(),
            symbols: vec![symbol],
            doc_comment: None,
        };

        let found = namespace.get_symbol("test_symbol");

        assert_some!(found);
        assert_eq!(found.unwrap().name, "test_symbol");
    }

    #[test]
    fn get_symbol_not_found() {
        let namespace = Namespace {
            name: "test_namespace".to_string(),
            symbols: vec![],
            doc_comment: None,
        };

        let symbol = namespace.get_symbol("nonexistent");

        assert_none!(symbol);
    }
}