oxidize_pdf/objects/
dictionary.rs

1use crate::objects::Object;
2use std::collections::HashMap;
3
4#[derive(Debug, Clone)]
5pub struct Dictionary {
6    entries: HashMap<String, Object>,
7}
8
9impl Dictionary {
10    pub fn new() -> Self {
11        Self {
12            entries: HashMap::new(),
13        }
14    }
15
16    pub fn with_capacity(capacity: usize) -> Self {
17        Self {
18            entries: HashMap::with_capacity(capacity),
19        }
20    }
21
22    pub fn set(&mut self, key: impl Into<String>, value: impl Into<Object>) {
23        self.entries.insert(key.into(), value.into());
24    }
25
26    pub fn get(&self, key: &str) -> Option<&Object> {
27        self.entries.get(key)
28    }
29
30    pub fn get_mut(&mut self, key: &str) -> Option<&mut Object> {
31        self.entries.get_mut(key)
32    }
33
34    pub fn remove(&mut self, key: &str) -> Option<Object> {
35        self.entries.remove(key)
36    }
37
38    pub fn contains_key(&self, key: &str) -> bool {
39        self.entries.contains_key(key)
40    }
41
42    pub fn len(&self) -> usize {
43        self.entries.len()
44    }
45
46    pub fn is_empty(&self) -> bool {
47        self.entries.is_empty()
48    }
49
50    pub fn clear(&mut self) {
51        self.entries.clear();
52    }
53
54    pub fn keys(&self) -> impl Iterator<Item = &String> {
55        self.entries.keys()
56    }
57
58    pub fn values(&self) -> impl Iterator<Item = &Object> {
59        self.entries.values()
60    }
61
62    pub fn entries(&self) -> impl Iterator<Item = (&String, &Object)> {
63        self.entries.iter()
64    }
65
66    pub fn entries_mut(&mut self) -> impl Iterator<Item = (&String, &mut Object)> {
67        self.entries.iter_mut()
68    }
69
70    pub fn iter(&self) -> impl Iterator<Item = (&String, &Object)> {
71        self.entries.iter()
72    }
73
74    pub fn get_dict(&self, key: &str) -> Option<&Dictionary> {
75        self.get(key).and_then(|obj| {
76            if let Object::Dictionary(dict) = obj {
77                Some(dict)
78            } else {
79                None
80            }
81        })
82    }
83}
84
85impl Default for Dictionary {
86    fn default() -> Self {
87        Self::new()
88    }
89}
90
91impl FromIterator<(String, Object)> for Dictionary {
92    fn from_iter<T: IntoIterator<Item = (String, Object)>>(iter: T) -> Self {
93        let mut dict = Dictionary::new();
94        for (key, value) in iter {
95            dict.set(key, value);
96        }
97        dict
98    }
99}