microcad_lang/eval/
attribute.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use std::str::FromStr;
5
6use crate::{
7    Id,
8    builtin::ExporterAccess,
9    eval::{self, *},
10    model::{Attributes, CustomCommand, ExportCommand, MeasureCommand, ResolutionAttribute},
11    parameter,
12    syntax::{self, *},
13};
14
15use microcad_core::{Color, Length, RenderResolution, Size2};
16use thiserror::Error;
17
18/// Error type for attributes.
19#[derive(Debug, Error)]
20pub enum AttributeError {
21    /// Unknown attribute.
22    #[error("Attribute not supported: {0}")]
23    NotSupported(Identifier),
24
25    /// Attribute cannot be assigned to an expression.
26    #[error("Cannot assign attribute to expression `{0}`")]
27    CannotAssignAttribute(String),
28
29    /// The attribute was not found.
30    #[error("Not found: {0}")]
31    NotFound(Identifier),
32
33    /// Invalid command.
34    #[error("Invalid command list for attribute `{0}`")]
35    InvalidCommand(Identifier),
36}
37
38impl Eval<Option<ExportCommand>> for syntax::AttributeCommand {
39    fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<ExportCommand>> {
40        match self {
41            AttributeCommand::Call(_, Some(argument_list)) => {
42                match ArgumentMatch::find_match(
43                    &argument_list.eval(context)?,
44                    &[
45                        parameter!(filename: String),
46                        parameter!(resolution: Length = Length::mm(0.1)),
47                        (
48                            Identifier::no_ref("size"),
49                            eval::ParameterValue {
50                                specified_type: Some(Type::Tuple(Box::new(TupleType::new_size2()))),
51                                default_value: Some(Value::Tuple(Box::new(Size2::A4.into()))),
52                                src_ref: SrcRef(None),
53                            },
54                        ),
55                    ]
56                    .into_iter()
57                    .collect(),
58                ) {
59                    Ok(arguments) => {
60                        let filename: std::path::PathBuf =
61                            arguments.get::<String>("filename").into();
62                        let id: Option<Id> = if let Ok(id) = arguments.by_str::<String>("id") {
63                            Some(id.into())
64                        } else {
65                            None
66                        };
67                        let resolution = RenderResolution::new(
68                            arguments.get::<&Value>("resolution").try_scalar()?,
69                        );
70
71                        match context.find_exporter(&filename, &id) {
72                            Ok(exporter) => Ok(Some(ExportCommand {
73                                filename,
74                                exporter,
75                                resolution,
76                            })),
77                            Err(err) => {
78                                context.warning(self, err)?;
79                                Ok(None)
80                            }
81                        }
82                    }
83                    Err(err) => {
84                        context.warning(self, err)?;
85                        Ok(None)
86                    }
87                }
88            }
89            AttributeCommand::Expression(expression) => {
90                let value: Value = expression.eval(context)?;
91                match value {
92                    Value::String(filename) => {
93                        let filename = std::path::PathBuf::from(filename);
94                        match context.find_exporter(&filename, &None) {
95                            Ok(exporter) => Ok(Some(ExportCommand {
96                                filename,
97                                resolution: RenderResolution::default(),
98                                exporter,
99                            })),
100                            Err(err) => {
101                                context.warning(self, err)?;
102                                Ok(None)
103                            }
104                        }
105                    }
106                    _ => unimplemented!(),
107                }
108            }
109            _ => Ok(None),
110        }
111    }
112}
113
114impl Eval<Vec<ExportCommand>> for syntax::Attribute {
115    fn eval(&self, context: &mut EvalContext) -> EvalResult<Vec<ExportCommand>> {
116        assert_eq!(self.id.id().as_str(), "export");
117
118        self.commands
119            .iter()
120            .try_fold(Vec::new(), |mut commands, attribute| {
121                if let Some(export_command) = attribute.eval(context)? {
122                    commands.push(export_command)
123                }
124                Ok(commands)
125            })
126    }
127}
128
129impl Eval<Vec<MeasureCommand>> for syntax::Attribute {
130    fn eval(&self, context: &mut EvalContext) -> EvalResult<Vec<MeasureCommand>> {
131        let mut commands = Vec::new();
132
133        for command in &self.commands {
134            match command {
135                AttributeCommand::Call(Some(id), _) => match id.id().as_str() {
136                    "width" => commands.push(MeasureCommand::Width),
137                    "height" => commands.push(MeasureCommand::Height),
138                    "size" => commands.push(MeasureCommand::Size),
139                    _ => context.warning(self, AttributeError::InvalidCommand(id.clone()))?,
140                },
141                _ => unimplemented!(),
142            }
143        }
144
145        Ok(commands)
146    }
147}
148
149impl Eval<Vec<CustomCommand>> for syntax::Attribute {
150    fn eval(&self, context: &mut EvalContext) -> EvalResult<Vec<CustomCommand>> {
151        match context.exporters().exporter_by_id(self.id.id()) {
152            Ok(exporter) => {
153                let mut commands = Vec::new();
154                for command in &self.commands {
155                    match command {
156                        AttributeCommand::Call(None, Some(argument_list)) => {
157                            match ArgumentMatch::find_match(
158                                &argument_list.eval(context)?,
159                                &exporter.model_parameters(),
160                            ) {
161                                Ok(tuple) => commands.push(CustomCommand {
162                                    id: self.id.clone(),
163                                    arguments: Box::new(tuple),
164                                }),
165                                Err(err) => {
166                                    context.warning(self, err)?;
167                                }
168                            }
169                        }
170                        _ => unimplemented!(),
171                    }
172                }
173
174                Ok(commands)
175            }
176            Err(err) => {
177                context.warning(self, err)?;
178                Ok(Vec::default())
179            }
180        }
181    }
182}
183
184impl Eval<Option<Color>> for syntax::AttributeCommand {
185    fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<Color>> {
186        match self {
187            // Get color from a tuple or string.
188            AttributeCommand::Expression(expression) => {
189                let value: Value = expression.eval(context)?;
190                match value {
191                    // Color from string: color = "red"
192                    Value::String(s) => match Color::from_str(&s) {
193                        Ok(color) => Ok(Some(color)),
194                        Err(err) => {
195                            context.warning(self, err)?;
196                            Ok(None)
197                        }
198                    },
199                    // Color from tuple: color = (r = 1.0, g = 1.0, b = 1.0, a = 1.0)
200                    Value::Tuple(tuple) => match Color::try_from(tuple.as_ref()) {
201                        Ok(color) => Ok(Some(color)),
202                        Err(err) => {
203                            context.warning(self, err)?;
204                            Ok(None)
205                        }
206                    },
207                    _ => {
208                        context.warning(
209                            self,
210                            AttributeError::InvalidCommand(Identifier::no_ref("color")),
211                        )?;
212                        Ok(None)
213                    }
214                }
215            }
216            AttributeCommand::Call(_, _) => todo!(),
217        }
218    }
219}
220
221impl Eval<Option<ResolutionAttribute>> for syntax::AttributeCommand {
222    fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<ResolutionAttribute>> {
223        match self {
224            AttributeCommand::Expression(expression) => {
225                let value: Value = expression.eval(context)?;
226                match value {
227                    Value::Quantity(qty) => match qty.quantity_type {
228                        QuantityType::Scalar => Ok(Some(ResolutionAttribute::Relative(qty.value))),
229                        QuantityType::Length => Ok(Some(ResolutionAttribute::Absolute(qty.value))),
230                        _ => unimplemented!(),
231                    },
232                    _ => todo!("Error handling"),
233                }
234            }
235            AttributeCommand::Call(_, _) => {
236                context.warning(
237                    self,
238                    AttributeError::InvalidCommand(Identifier::no_ref("resolution")),
239                )?;
240                Ok(None)
241            }
242        }
243    }
244}
245
246impl Eval<Option<Size2>> for syntax::AttributeCommand {
247    fn eval(&self, _: &mut EvalContext) -> EvalResult<Option<Size2>> {
248        todo!("Get Size2, e.g. `size = (width = 10mm, height = 10mm) from AttributeCommand")
249    }
250}
251
252macro_rules! eval_to_attribute {
253    ($id:ident: $ty:ty) => {
254        impl Eval<Option<$ty>> for syntax::Attribute {
255            fn eval(&self, context: &mut EvalContext) -> EvalResult<Option<$ty>> {
256                assert_eq!(self.id.id().as_str(), stringify!($id));
257                match self.single_command() {
258                    Some(command) => Ok(command.eval(context)?),
259                    None => {
260                        context.warning(self, AttributeError::InvalidCommand(self.id.clone()))?;
261                        Ok(None)
262                    }
263                }
264            }
265        }
266    };
267}
268
269eval_to_attribute!(color: Color);
270eval_to_attribute!(resolution: ResolutionAttribute);
271eval_to_attribute!(size: Size2);
272
273impl Eval<Vec<crate::model::Attribute>> for syntax::Attribute {
274    fn eval(&self, context: &mut EvalContext) -> EvalResult<Vec<crate::model::Attribute>> {
275        let id = self.id.id().as_str();
276        use crate::model::Attribute as Attr;
277        Ok(match id {
278            "color" => match self.eval(context)? {
279                Some(color) => vec![Attr::Color(color)],
280                None => Default::default(),
281            },
282            "resolution" => match self.eval(context)? {
283                Some(resolution) => vec![Attr::Resolution(resolution)],
284                None => Default::default(),
285            },
286            "size" => match self.eval(context)? {
287                Some(size) => vec![Attr::Size(size)],
288                None => Default::default(),
289            },
290            "export" => {
291                let exports: Vec<ExportCommand> = self.eval(context)?;
292                exports.iter().cloned().map(Attr::Export).collect()
293            }
294            "measure" => {
295                let measures: Vec<MeasureCommand> = self.eval(context)?;
296                measures.iter().cloned().map(Attr::Measure).collect()
297            }
298            _ => {
299                let commands: Vec<CustomCommand> = self.eval(context)?;
300                commands.iter().cloned().map(Attr::Custom).collect()
301            }
302        })
303    }
304}
305
306impl Eval<crate::model::Attributes> for AttributeList {
307    fn eval(&self, context: &mut EvalContext) -> EvalResult<crate::model::Attributes> {
308        Ok(Attributes(self.iter().try_fold(
309            Vec::new(),
310            |mut attributes, attribute| -> EvalResult<_> {
311                attributes.append(&mut attribute.eval(context)?);
312                Ok(attributes)
313            },
314        )?))
315    }
316}