Skip to main content

graphy_parser/
frontend.rs

1use std::path::Path;
2
3use anyhow::Result;
4use graphy_core::ParseOutput;
5
6/// Trait that all language frontends implement.
7///
8/// A frontend takes a file path + source code and produces GIR nodes and edges.
9pub trait LanguageFrontend {
10    fn parse(&self, path: &Path, source: &str) -> Result<ParseOutput>;
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16    use std::path::Path;
17
18    /// A trivial implementation to verify the trait is object-safe and usable.
19    struct DummyFrontend;
20    impl LanguageFrontend for DummyFrontend {
21        fn parse(&self, _path: &Path, _source: &str) -> Result<ParseOutput> {
22            Ok(ParseOutput::default())
23        }
24    }
25
26    #[test]
27    fn dummy_frontend_returns_empty_output() {
28        let frontend = DummyFrontend;
29        let output = frontend.parse(Path::new("test.py"), "").unwrap();
30        assert!(output.nodes.is_empty());
31        assert!(output.edges.is_empty());
32    }
33
34    #[test]
35    fn trait_is_object_safe() {
36        // Verify LanguageFrontend can be used as a trait object
37        let frontend: Box<dyn LanguageFrontend> = Box::new(DummyFrontend);
38        let output = frontend.parse(Path::new("test.py"), "x = 1").unwrap();
39        assert!(output.nodes.is_empty());
40    }
41
42    #[test]
43    fn frontend_handles_unicode_source() {
44        let frontend = DummyFrontend;
45        let output = frontend.parse(Path::new("日本語.py"), "変数 = '値'").unwrap();
46        assert!(output.nodes.is_empty());
47    }
48}