Skip to main content

mir/
import.rs

1use crate::{Ident, Visibility};
2
3pub struct Import {
4    /// Path that we're importing from
5    /// e.g. plaid.model in `from plaid.model import ...`
6    pub path: String,
7    /// Specific items that are imported
8    /// e.g. `Account` in `from plaid.model import Account`
9    pub imports: Vec<ImportItem>,
10    /// If a wildcard import and if we want to alias, then alias
11    pub alias: Option<String>,
12    pub vis: Visibility,
13    pub feature: Option<String>,
14}
15
16impl Import {
17    pub fn package(path: impl Into<String>) -> Self {
18        Self {
19            path: path.into(),
20            imports: vec![],
21            alias: None,
22            vis: Visibility::Private,
23            feature: None,
24        }
25    }
26
27    pub fn new(path: impl Into<String>, imports: impl IntoIterator<Item = impl Into<ImportItem>>) -> Self {
28        Self {
29            path: path.into(),
30            imports: imports.into_iter().map(|s| s.into()).collect(),
31            alias: None,
32            vis: Visibility::Private,
33            feature: None,
34        }
35    }
36
37    pub fn alias(path: &str, alias: &str) -> Self {
38        Self {
39            path: path.to_string(),
40            imports: Vec::new(),
41            alias: Some(alias.to_string()),
42            vis: Visibility::Private,
43            feature: None,
44        }
45    }
46
47    pub fn public(mut self) -> Self {
48        self.vis = Visibility::Public;
49        self
50    }
51}
52
53pub struct ImportItem {
54    /// This might not conform to standard ident rules for the language, so its a string, not an ident.
55    pub name: String,
56    pub alias: Option<String>,
57}
58
59impl ImportItem {
60    pub fn alias(name: &str, alias: &str) -> Self {
61        Self {
62            name: name.to_string(),
63            alias: Some(alias.to_string()),
64        }
65    }
66
67    pub fn validate(&self) -> Result<(), String> {
68        if self.name.is_empty() {
69            return Err("ImportItem name cannot be empty".to_string());
70        }
71        if self.name.chars().all(|c| c.is_digit(10)) {
72            return Err("ImportItem name cannot be all digits".to_string());
73        }
74        Ok(())
75    }
76}
77
78impl From<&String> for ImportItem {
79    fn from(s: &String) -> Self {
80        let r = Self {
81            name: s.clone(),
82            alias: None,
83        };
84        r.validate().unwrap();
85        r
86    }
87}
88
89impl From<String> for ImportItem {
90    fn from(s: String) -> Self {
91        let r = Self { name: s, alias: None };
92        r.validate().unwrap();
93        r
94    }
95}
96
97impl From<&str> for ImportItem {
98    fn from(s: &str) -> Self {
99        let r = Self {
100            name: s.to_string(),
101            alias: None,
102        };
103        r.validate().unwrap();
104        r
105    }
106}
107
108impl From<Ident> for ImportItem {
109    fn from(s: Ident) -> Self {
110        Self { name: s.0, alias: None }
111    }
112}