Skip to main content

microcad_lang/parse/
attribute.rs

1// Copyright © 2025-2026 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*};
5use microcad_syntax::ast;
6
7impl FromAst for AttributeCommand {
8    type AstNode = ast::AttributeCommand;
9
10    fn from_ast(node: &Self::AstNode, context: &ParseContext) -> Result<Self, ParseError> {
11        Ok(match node {
12            ast::AttributeCommand::Ident(i) => {
13                AttributeCommand::Ident(Identifier::from_ast(i, context)?)
14            }
15            ast::AttributeCommand::Assignment(a) => AttributeCommand::Assigment {
16                name: Identifier::from_ast(&a.name, context)?,
17                value: Expression::from_ast(&a.value, context)?,
18                src_ref: context.src_ref(&a.span),
19            },
20            ast::AttributeCommand::Call(c) => AttributeCommand::Call(Call::from_ast(c, context)?),
21        })
22    }
23}
24
25impl FromAst for Attribute {
26    type AstNode = ast::Attribute;
27
28    fn from_ast(node: &Self::AstNode, context: &ParseContext) -> Result<Self, ParseError> {
29        Ok(Attribute {
30            src_ref: context.src_ref(&node.span),
31            is_inner: node.is_inner,
32            commands: node
33                .commands
34                .iter()
35                .map(|c| AttributeCommand::from_ast(c, context))
36                .collect::<Result<Vec<_>, ParseError>>()?,
37        })
38    }
39}
40
41impl FromAst for AttributeList {
42    type AstNode = Vec<ast::Attribute>;
43
44    fn from_ast(node: &Self::AstNode, context: &ParseContext) -> Result<Self, ParseError> {
45        node.iter()
46            .map(|a| Attribute::from_ast(a, context))
47            .collect::<Result<Vec<_>, _>>()
48            .map(AttributeList::from)
49    }
50}