microcad_lang/syntax/use/
use_declaration.rs1use crate::{src_ref::*, syntax::*};
7use strum::IntoStaticStr;
8
9#[derive(Clone, IntoStaticStr)]
21pub enum UseDeclaration {
22 Use(QualifiedName),
24 UseAll(QualifiedName),
26 UseAs(QualifiedName, Identifier),
28}
29
30impl SrcReferrer for UseDeclaration {
31 fn src_ref(&self) -> SrcRef {
32 match self {
33 Self::Use(.., name) => name.src_ref(),
34 Self::UseAll(.., name) => name.src_ref(),
35 Self::UseAs(.., name, _) => name.src_ref(),
36 }
37 }
38}
39
40impl std::fmt::Display for UseDeclaration {
41 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42 match self {
43 UseDeclaration::Use(name) => write!(f, "{name}"),
44 UseDeclaration::UseAll(name) => write!(f, "{name}::*"),
45 UseDeclaration::UseAs(name, alias) => {
46 write!(f, "{name} as {alias}")
47 }
48 }
49 }
50}
51
52impl std::fmt::Debug for UseDeclaration {
53 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54 match self {
55 UseDeclaration::Use(name) => write!(f, "{name:?}"),
56 UseDeclaration::UseAll(name) => write!(f, "{name:?}::*"),
57 UseDeclaration::UseAs(name, alias) => {
58 write!(f, "{name:?} as {alias:?}")
59 }
60 }
61 }
62}
63
64impl TreeDisplay for UseDeclaration {
65 fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
66 match self {
68 UseDeclaration::Use(name) => {
69 writeln!(f, "{:depth$}use {name}", "")
70 }
71 UseDeclaration::UseAll(name) => {
72 writeln!(f, "{:depth$}use {name}::*", "")
73 }
74 UseDeclaration::UseAs(name, alias) => {
75 writeln!(f, "{:depth$}use {name} as {alias}", "")
76 }
77 }
78 }
79}