miden_assembly_syntax/ast/
imports.rs

1use core::fmt;
2
3use miden_debug_types::{SourceSpan, Spanned};
4
5use crate::{LibraryNamespace, LibraryPath, ast::Ident};
6
7// IMPORT
8// ================================================================================================
9
10/// Represents an import statement in Miden Assembly syntax.
11#[derive(Clone)]
12pub struct Import {
13    /// The source span of the statement.
14    pub span: SourceSpan,
15    /// The local module name/alias.
16    ///
17    /// When the imported item is aliased to a new name, this field contains the alias, while
18    /// `path.last()` can be used to obtain the actual name.
19    pub name: Ident,
20    /// The fully-qualified path.
21    pub path: LibraryPath,
22    /// The number of times this import has been used locally.
23    pub uses: usize,
24}
25
26impl Import {
27    /// Returns true if this import is aliased to a different name in its containing module.
28    pub fn is_aliased(&self) -> bool {
29        self.name.as_ref() != self.path.last()
30    }
31
32    /// Returns the namespace of the imported module.
33    pub fn namespace(&self) -> &LibraryNamespace {
34        self.path.namespace()
35    }
36
37    /// Returns the fully-qualified path of the imported module.
38    pub fn path(&self) -> &LibraryPath {
39        &self.path
40    }
41
42    /// Returns true if this import has at least one use in its containing module.
43    pub fn is_used(&self) -> bool {
44        self.uses > 0
45    }
46}
47
48impl fmt::Debug for Import {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        f.debug_struct("Import")
51            .field("name", &self.name)
52            .field("path", &self.path)
53            .field("uses", &self.uses)
54            .finish()
55    }
56}
57
58impl crate::prettier::PrettyPrint for Import {
59    fn render(&self) -> crate::prettier::Document {
60        use crate::prettier::*;
61
62        let mut doc = const_text("use") + const_text(" ") + display(&self.path);
63        if self.is_aliased() {
64            doc += const_text("->") + display(&self.name);
65        }
66        doc
67    }
68}
69
70impl Eq for Import {}
71
72impl PartialEq for Import {
73    fn eq(&self, other: &Self) -> bool {
74        self.name == other.name && self.path == other.path
75    }
76}
77
78impl Spanned for Import {
79    fn span(&self) -> SourceSpan {
80        self.span
81    }
82}