// Copyright © 2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Body syntax element.

use derive_more::Deref;

use crate::{src_ref::*, syntax::*};

/// [StatementList] from inside `{}` brackets.
#[derive(Clone, Default, Deref)]
pub struct Body {
    /// Body statements.
    #[deref]
    pub statements: StatementList,
    /// Source code reference.
    pub src_ref: SrcRef,
}

impl SrcReferrer for Body {
    fn src_ref(&self) -> SrcRef {
        self.src_ref.clone()
    }
}

impl std::fmt::Display for Body {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        writeln!(f, " {{")?;
        writeln!(f, "{}", self.statements)?;
        writeln!(f, "}}")?;
        Ok(())
    }
}

impl std::fmt::Debug for Body {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        writeln!(f, " {{")?;
        writeln!(f, "{:?}", self.statements)?;
        writeln!(f, "}}")?;
        Ok(())
    }
}

impl TreeDisplay for Body {
    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
        writeln!(f, "{:depth$}Body:", "")?;
        depth.indent();
        self.statements
            .iter()
            .try_for_each(|s| s.tree_print(f, depth))
    }
}
