Skip to main content

microcad_lang/model/attribute/
export_command.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Export attribute.
5
6use crate::{
7    builtin::{ExportError, Exporter},
8    model::Model,
9    value::Value,
10};
11
12/// Export attribute, e.g. `#[export: "output.svg"]`.
13#[derive(Clone)]
14pub struct ExportCommand {
15    /// Filename.
16    pub filename: std::path::PathBuf,
17    /// Exporter.
18    pub exporter: std::rc::Rc<dyn Exporter>,
19}
20
21impl ExportCommand {
22    /// Export the model. By the settings in the attribute.
23    pub fn export(&self, model: &Model) -> Result<Value, ExportError> {
24        self.exporter.export(model, &self.filename)
25    }
26}
27
28impl From<ExportCommand> for Value {
29    fn from(export_attribute: ExportCommand) -> Self {
30        crate::create_tuple_value!(
31            filename = Value::String(String::from(
32                export_attribute.filename.to_str().expect("PathBuf"),
33            )),
34            id = Value::String(export_attribute.exporter.id().to_string())
35        )
36    }
37}
38
39impl std::fmt::Debug for ExportCommand {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(
42            f,
43            "Export: {id:?} => {filename}",
44            id = self.exporter.id(),
45            filename = self.filename.display()
46        )
47    }
48}
49
50impl std::fmt::Display for ExportCommand {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(
53            f,
54            "\"{filename}\" with exporter `{id}`",
55            filename = self.filename.display(),
56            id = self.exporter.id(),
57        )
58    }
59}