microcad_lang/syntax/function/
function_definition.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Function definition syntax element
5
6use crate::{src_ref::*, syntax::*};
7
8/// Function definition
9#[derive(Debug, Clone)]
10pub struct FunctionDefinition {
11    /// Visibility
12    pub visibility: Visibility,
13    /// Name of the function
14    pub id: Identifier,
15    /// Function signature
16    pub signature: FunctionSignature,
17    /// Function body
18    pub body: Body,
19    /// Source code reference
20    pub src_ref: SrcRef,
21}
22
23impl SrcReferrer for FunctionDefinition {
24    fn src_ref(&self) -> SrcRef {
25        self.src_ref.clone()
26    }
27}
28
29impl TreeDisplay for FunctionDefinition {
30    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
31        writeln!(f, "{:depth$}FunctionDefinition '{}':", "", self.id)?;
32        depth.indent();
33        writeln!(f, "{:depth$}Signature:", "")?;
34        self.signature.tree_print(f, depth)?;
35        writeln!(f, "{:depth$}Body:", "")?;
36        self.body.tree_print(f, depth)
37    }
38}