microcad_lang/model/attribute/
attributes.rs

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