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(Clone)]
10pub struct FunctionDefinition {
11    /// Documentation.
12    pub doc: Option<DocBlock>,
13    /// Visibility
14    pub visibility: Visibility,
15    /// Name of the function
16    pub id: Identifier,
17    /// Function signature
18    pub signature: FunctionSignature,
19    /// Function body
20    pub body: Body,
21}
22
23impl SrcReferrer for FunctionDefinition {
24    fn src_ref(&self) -> SrcRef {
25        self.id.src_ref()
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        if let Some(doc) = &self.doc {
34            doc.tree_print(f, depth)?;
35        }
36        writeln!(f, "{:depth$}Signature:", "")?;
37        self.signature.tree_print(f, depth)?;
38        writeln!(f, "{:depth$}Body:", "")?;
39        self.body.tree_print(f, depth)
40    }
41}
42
43impl std::fmt::Display for FunctionDefinition {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "fn {}{}", self.id, self.signature)
46    }
47}
48
49impl std::fmt::Debug for FunctionDefinition {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "fn {:?}{:?}", self.id, self.signature)
52    }
53}
54
55impl Doc for FunctionDefinition {
56    fn doc(&self) -> Option<DocBlock> {
57        self.doc.clone()
58    }
59}