Skip to main content

microcad_lang/syntax/use/
use_statement.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Use statement syntax element.
5
6use microcad_lang_base::{SrcRef, SrcReferrer, TreeDisplay, TreeState};
7
8use crate::syntax::*;
9
10/// Use statement.
11///
12/// # Example
13/// ```ucad
14/// use std::*;
15/// ```
16#[derive(Clone)]
17pub struct UseStatement {
18    /// SrcRef of the `use` keyword
19    pub keyword_ref: SrcRef,
20    /// export of use
21    pub visibility: Visibility,
22    /// Use declaration
23    pub decl: UseDeclaration,
24    /// source code reference
25    pub src_ref: SrcRef,
26}
27
28impl SrcReferrer for UseStatement {
29    fn src_ref(&self) -> SrcRef {
30        self.src_ref.clone()
31    }
32}
33
34impl std::fmt::Display for UseStatement {
35    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36        match &self.visibility {
37            Visibility::Private | Visibility::PrivateUse(_) => write!(f, "use ")?,
38            Visibility::Public => write!(f, "pub use ")?,
39            Visibility::Deleted => unreachable!(),
40        }
41        write!(f, "{}", self.decl)?;
42        Ok(())
43    }
44}
45
46impl std::fmt::Debug for UseStatement {
47    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
48        match &self.visibility {
49            Visibility::Private | Visibility::PrivateUse(_) => write!(f, "use ")?,
50            Visibility::Public => write!(f, "pub use ")?,
51            Visibility::Deleted => unreachable!(),
52        }
53        write!(f, "{:?}", self.decl)?;
54        Ok(())
55    }
56}
57
58impl TreeDisplay for UseStatement {
59    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
60        writeln!(f, "{:depth$}UseStatement", "")?;
61        depth.indent();
62        self.decl.tree_print(f, depth)
63    }
64}