aboutsummaryrefslogtreecommitdiff
path: root/embassy-macros/src/util/path.rs
blob: 00fca7bdb9c2527a4c7c71c4e54230b85ea199c5 (plain)
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
use darling::{FromMeta, Result};
use proc_macro2::Span;
use syn::{LitStr, Path};

#[derive(Debug)]
pub struct ModulePrefix {
    literal: LitStr,
}

impl ModulePrefix {
    pub fn new(path: &str) -> Self {
        let literal = LitStr::new(path, Span::call_site());
        Self { literal }
    }

    pub fn append(&self, component: &str) -> ModulePrefix {
        let mut lit = self.literal().value();
        lit.push_str(component);
        Self::new(lit.as_str())
    }

    pub fn path(&self) -> Path {
        self.literal.parse().unwrap()
    }

    pub fn literal(&self) -> &LitStr {
        &self.literal
    }
}

impl FromMeta for ModulePrefix {
    fn from_string(value: &str) -> Result<Self> {
        Ok(ModulePrefix::new(value))
    }
}

impl Default for ModulePrefix {
    fn default() -> ModulePrefix {
        ModulePrefix::new("::")
    }
}