ligen_parser/parser/universal/
identifier.rs

1use crate::{prelude::*, parser::ParserConfig};
2
3use ligen_ir::Identifier;
4use crate::parser::Parser;
5
6#[derive(Default)]
7pub struct IdentifierParser;
8
9impl IdentifierParser {
10    pub fn new() -> Self {
11        Default::default()
12    }
13}
14
15impl Parser<String> for IdentifierParser {
16    type Output = Identifier;
17    fn parse(&self, input: String, config: &ParserConfig) -> Result<Self::Output> {
18        self.parse(input.as_str(), config)
19    }
20}
21
22impl Parser<&str> for IdentifierParser {
23    type Output = Identifier;
24    fn parse(&self, input: &str, _config: &ParserConfig) -> Result<Self::Output> {
25        // TODO: check if ident is valid identifier.
26        let name = input.into();
27        Ok(Identifier { name })
28    }
29}
30
31impl Parser<&std::path::Path> for IdentifierParser {
32    type Output = Identifier;
33    fn parse(&self, input: &std::path::Path, config: &ParserConfig) -> Result<Self::Output> {
34        let identifier = input
35            .file_stem()
36            .ok_or(Error::Message(format!("Failed to parse file stem from path: {}", input.display())))?
37            .to_str()
38            .ok_or(Error::Message(format!("Failed to parse file stem to string: {}", input.display())))?;
39        self.parse(identifier, config)
40
41    }
42}
43
44impl Parser<syn::Ident> for IdentifierParser {
45    type Output = Identifier;
46    fn parse(&self, ident: syn::Ident, _config: &ParserConfig) -> Result<Self::Output> {
47        let name = ident.to_string();
48        Ok(Self::Output { name })
49    }
50}
51
52#[cfg(test)]
53mod test {
54    use super::*;
55    use crate::assert::*;
56    use ligen_ir::identifier::mock;
57
58    #[test]
59    fn identifier() -> Result<()> {
60        assert_eq(IdentifierParser, mock::identifier(), "identifier")
61    }
62}