1use crate::interning::StringId;
2use crate::types::TypeId;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum Attribute {
8 Integer(i64),
9 Float(f64),
10 String(StringId),
11 Bool(bool),
12 Type(TypeId),
13 Array(Vec<Attribute>),
14 Dict(HashMap<String, Attribute>),
15}
16
17#[derive(Debug, Clone, Default, Serialize, Deserialize)]
18pub struct Attributes {
19 entries: HashMap<String, Attribute>,
20}
21
22impl Attributes {
23 pub fn new() -> Self {
24 Self::default()
25 }
26
27 pub fn set(&mut self, key: impl Into<String>, value: Attribute) {
28 self.entries.insert(key.into(), value);
29 }
30
31 pub fn get(&self, key: &str) -> Option<&Attribute> {
32 self.entries.get(key)
33 }
34
35 pub fn get_integer(&self, key: &str) -> Option<i64> {
36 match self.entries.get(key)? {
37 Attribute::Integer(v) => Some(*v),
38 _ => None,
39 }
40 }
41
42 pub fn get_float(&self, key: &str) -> Option<f64> {
43 match self.entries.get(key)? {
44 Attribute::Float(v) => Some(*v),
45 _ => None,
46 }
47 }
48
49 pub fn get_bool(&self, key: &str) -> Option<bool> {
50 match self.entries.get(key)? {
51 Attribute::Bool(v) => Some(*v),
52 _ => None,
53 }
54 }
55
56 pub fn get_string_id(&self, key: &str) -> Option<StringId> {
57 match self.entries.get(key)? {
58 Attribute::String(v) => Some(*v),
59 _ => None,
60 }
61 }
62
63 pub fn remove(&mut self, key: &str) -> Option<Attribute> {
64 self.entries.remove(key)
65 }
66
67 pub fn contains(&self, key: &str) -> bool {
68 self.entries.contains_key(key)
69 }
70
71 pub fn iter(&self) -> impl Iterator<Item = (&String, &Attribute)> {
72 self.entries.iter()
73 }
74
75 pub fn len(&self) -> usize {
76 self.entries.len()
77 }
78
79 pub fn is_empty(&self) -> bool {
80 self.entries.is_empty()
81 }
82}
83
84impl PartialEq for Attributes {
85 fn eq(&self, other: &Self) -> bool {
86 self.entries == other.entries
87 }
88}