miden_assembly/ast/
imports.rs1use core::fmt;
2
3use crate::{LibraryNamespace, LibraryPath, SourceSpan, Spanned, ast::Ident};
4
5#[derive(Clone)]
10pub struct Import {
11 pub span: SourceSpan,
13 pub name: Ident,
18 pub path: LibraryPath,
20 pub uses: usize,
22}
23
24impl Import {
25 pub fn is_aliased(&self) -> bool {
27 self.name.as_ref() != self.path.last()
28 }
29
30 pub fn namespace(&self) -> &LibraryNamespace {
32 self.path.namespace()
33 }
34
35 pub fn path(&self) -> &LibraryPath {
37 &self.path
38 }
39
40 pub fn is_used(&self) -> bool {
42 self.uses > 0
43 }
44}
45
46impl fmt::Debug for Import {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 f.debug_struct("Import")
49 .field("name", &self.name)
50 .field("path", &self.path)
51 .field("uses", &self.uses)
52 .finish()
53 }
54}
55
56impl crate::prettier::PrettyPrint for Import {
57 fn render(&self) -> crate::prettier::Document {
58 use crate::prettier::*;
59
60 let mut doc = const_text("use") + const_text(".") + display(&self.path);
61 if self.is_aliased() {
62 doc += const_text("->") + display(&self.name);
63 }
64 doc
65 }
66}
67
68impl Eq for Import {}
69
70impl PartialEq for Import {
71 fn eq(&self, other: &Self) -> bool {
72 self.name == other.name && self.path == other.path
73 }
74}
75
76impl Spanned for Import {
77 fn span(&self) -> SourceSpan {
78 self.span
79 }
80}