microcad_lang/syntax/use/
use_statement.rs

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