Skip to main content

microcad_lang/parse/
function.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*, syntax::*};
5use microcad_syntax::ast;
6
7impl FromAst for FunctionDefinition {
8    type AstNode = ast::FunctionDefinition;
9
10    fn from_ast(node: &Self::AstNode, context: &ParseContext) -> Result<Self, ParseError> {
11        Ok(FunctionDefinition {
12            keyword_ref: context.src_ref(&node.keyword_span),
13            doc: node
14                .doc
15                .as_ref()
16                .map(|doc| DocBlock::from_ast(doc, context))
17                .transpose()?,
18            visibility: node
19                .visibility
20                .as_ref()
21                .map(|vis| Visibility::from_ast(vis, context))
22                .transpose()?
23                .unwrap_or_default(),
24            id: Identifier::from_ast(&node.name, context)?,
25            body: Body::from_ast(&node.body, context)?,
26            signature: FunctionSignature {
27                src_ref: context.src_ref(&node.span),
28                parameters: ParameterList::from_ast(&node.arguments, context)?,
29                return_type: node
30                    .return_type
31                    .as_ref()
32                    .map(|ty| TypeAnnotation::from_ast(ty, context))
33                    .transpose()?,
34            },
35        })
36    }
37}