microcad_lang/parse/
function.rs1use 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 doc: node
13 .doc
14 .as_ref()
15 .map(|doc| DocBlock::from_ast(doc, context))
16 .transpose()?,
17 visibility: node
18 .visibility
19 .as_ref()
20 .map(|vis| Visibility::from_ast(vis, context))
21 .transpose()?
22 .unwrap_or_default(),
23 id: Identifier::from_ast(&node.name, context)?,
24 body: Body::from_ast(&node.body, context)?,
25 signature: FunctionSignature {
26 src_ref: context.src_ref(&node.span),
27 parameters: ParameterList::from_ast(&node.arguments, context)?,
28 return_type: node
29 .return_type
30 .as_ref()
31 .map(|ty| TypeAnnotation::from_ast(ty, context))
32 .transpose()?,
33 },
34 })
35 }
36}