miden_assembly_syntax/ast/
imports.rs1use core::fmt;
2
3use miden_debug_types::{SourceSpan, Spanned};
4
5use crate::{LibraryNamespace, LibraryPath, ast::Ident};
6
7#[derive(Clone)]
12pub struct Import {
13 pub span: SourceSpan,
15 pub name: Ident,
20 pub path: LibraryPath,
22 pub uses: usize,
24}
25
26impl Import {
27 pub fn is_aliased(&self) -> bool {
29 self.name.as_ref() != self.path.last()
30 }
31
32 pub fn namespace(&self) -> &LibraryNamespace {
34 self.path.namespace()
35 }
36
37 pub fn path(&self) -> &LibraryPath {
39 &self.path
40 }
41
42 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}