Skip to main content

microcad_lang/model/attribute/
attributes.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Model attributes collection.
5
6use derive_more::{Deref, DerefMut};
7
8use crate::model::*;
9use microcad_lang_base::Identifier;
10
11/// Model attributes, from an evaluated attribute list.
12///
13/// The model attributes can be produced from:
14/// * outer attributes: `#[export = "test.svg"]`
15/// * inner attributes: `#![export = "test.svg"]`
16///
17#[derive(Clone, Debug, Default, Deref, DerefMut)]
18pub struct Attributes(pub Vec<Attribute>);
19
20impl AttributesAccess for Attributes {
21    fn get_attributes_by_id(&self, id: &Identifier) -> Vec<Attribute> {
22        self.iter()
23            .filter(|attribute| attribute.id() == *id)
24            .cloned()
25            .collect()
26    }
27}
28
29impl FromIterator<Attribute> for Attributes {
30    fn from_iter<T: IntoIterator<Item = Attribute>>(iter: T) -> Self {
31        Self(iter.into_iter().collect())
32    }
33}
34
35impl TreeDisplay for Attributes {
36    fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
37        self.iter()
38            .try_for_each(|attribute| writeln!(f, "{:depth$}{attribute}", ""))
39    }
40}