microcad_lang/syntax/function/
function_signature.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Function signature syntax element
5
6use crate::{src_ref::*, syntax::*};
7
8/// Parameters and return type of a function
9#[derive(Clone, Debug)]
10pub struct FunctionSignature {
11    /// Function's parameters
12    pub parameters: ParameterList,
13    /// Function's return type
14    pub return_type: Option<TypeAnnotation>,
15    /// Source code reference
16    pub src_ref: SrcRef,
17}
18
19impl SrcReferrer for FunctionSignature {
20    fn src_ref(&self) -> SrcRef {
21        self.src_ref.clone()
22    }
23}
24
25impl FunctionSignature {
26    /// Get parameter by name
27    pub fn parameter_by_name(&self, name: &Identifier) -> Option<&Parameter> {
28        self.parameters.iter().find(|arg| arg.id == *name)
29    }
30}
31
32impl TreeDisplay for FunctionSignature {
33    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
34        writeln!(f, "{:depth$}Parameters:", "")?;
35        depth.indent();
36        self.parameters.tree_print(f, depth)?;
37        if let Some(return_type) = &self.return_type {
38            writeln!(f, "{:depth$}Return:", "")?;
39            return_type.tree_print(f, depth)?;
40        };
41        Ok(())
42    }
43}