microcad_lang/syntax/
body.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Body syntax element.
5
6use derive_more::Deref;
7
8use crate::{src_ref::*, syntax::*};
9
10/// [StatementList] from inside `{}` brackets.
11#[derive(Clone, Default, Deref)]
12pub struct Body {
13    /// Body statements.
14    #[deref]
15    pub statements: StatementList,
16    /// Source code reference.
17    pub src_ref: SrcRef,
18}
19
20impl SrcReferrer for Body {
21    fn src_ref(&self) -> SrcRef {
22        self.src_ref.clone()
23    }
24}
25
26impl std::fmt::Display for Body {
27    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
28        writeln!(f, " {{")?;
29        writeln!(f, "{}", self.statements)?;
30        writeln!(f, "}}")?;
31        Ok(())
32    }
33}
34
35impl std::fmt::Debug for Body {
36    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37        writeln!(f, " {{")?;
38        writeln!(f, "{:?}", self.statements)?;
39        writeln!(f, "}}")?;
40        Ok(())
41    }
42}
43
44impl TreeDisplay for Body {
45    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
46        writeln!(f, "{:depth$}Body:", "")?;
47        depth.indent();
48        self.statements
49            .iter()
50            .try_for_each(|s| s.tree_print(f, depth))
51    }
52}