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)]
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 | Visibility::PrivateUse(_) => write!(f, "use ")?,
34            Visibility::Public => write!(f, "pub use ")?,
35            Visibility::Deleted => unreachable!(),
36        }
37        write!(f, "{}", self.decl)?;
38        Ok(())
39    }
40}
41
42impl std::fmt::Debug for UseStatement {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        match &self.visibility {
45            Visibility::Private | Visibility::PrivateUse(_) => write!(f, "use ")?,
46            Visibility::Public => write!(f, "pub use ")?,
47            Visibility::Deleted => unreachable!(),
48        }
49        write!(f, "{:?}", self.decl)?;
50        Ok(())
51    }
52}
53
54impl TreeDisplay for UseStatement {
55    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
56        writeln!(f, "{:depth$}UseStatement", "")?;
57        depth.indent();
58        self.decl.tree_print(f, depth)
59    }
60}