ccs2/ast/
property.rs

1use std::fmt::Display;
2
3use crate::ast::{Origin, PersistentStr, PropDef};
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
6pub struct PropertyValue {
7    pub value: PersistentStr,
8    pub origin: Origin,
9    pub override_level: u32,
10}
11impl PropertyValue {
12    pub fn new(value: impl ToString, origin: Origin, override_level: u32) -> Self {
13        Self {
14            value: value.to_string().into(),
15            origin,
16            override_level,
17        }
18    }
19}
20
21#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub struct Property(pub PersistentStr, pub PropertyValue);
23
24impl Property {
25    pub fn new(
26        name: impl ToString,
27        value: impl ToString,
28        origin: Origin,
29        should_override: bool,
30    ) -> Self {
31        Self(
32            name.to_string().into(),
33            PropertyValue::new(value, origin, if should_override { 1 } else { 0 }),
34        )
35    }
36}
37
38impl From<PropDef> for Property {
39    fn from(def: PropDef) -> Self {
40        Self::new(def.name, def.value, def.origin, def.should_override)
41    }
42}
43impl Display for Property {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{} = {}", self.0, self.1.value)
46    }
47}