oxidize_pdf/objects/
primitive.rs

1use crate::objects::Dictionary;
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct ObjectId {
6    number: u32,
7    generation: u16,
8}
9
10impl ObjectId {
11    pub fn new(number: u32, generation: u16) -> Self {
12        Self { number, generation }
13    }
14
15    pub fn number(&self) -> u32 {
16        self.number
17    }
18
19    pub fn generation(&self) -> u16 {
20        self.generation
21    }
22}
23
24impl fmt::Display for ObjectId {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{} {} R", self.number, self.generation)
27    }
28}
29
30#[derive(Debug, Clone)]
31pub enum Object {
32    Null,
33    Boolean(bool),
34    Integer(i64),
35    Real(f64),
36    String(String),
37    Name(String),
38    Array(Vec<Object>),
39    Dictionary(Dictionary),
40    Stream(Dictionary, Vec<u8>),
41    Reference(ObjectId),
42}
43
44impl Object {
45    pub fn is_null(&self) -> bool {
46        matches!(self, Object::Null)
47    }
48
49    pub fn as_bool(&self) -> Option<bool> {
50        match self {
51            Object::Boolean(b) => Some(*b),
52            _ => None,
53        }
54    }
55
56    pub fn as_integer(&self) -> Option<i64> {
57        match self {
58            Object::Integer(i) => Some(*i),
59            _ => None,
60        }
61    }
62
63    pub fn as_real(&self) -> Option<f64> {
64        match self {
65            Object::Real(f) => Some(*f),
66            Object::Integer(i) => Some(*i as f64),
67            _ => None,
68        }
69    }
70
71    pub fn as_string(&self) -> Option<&str> {
72        match self {
73            Object::String(s) => Some(s),
74            _ => None,
75        }
76    }
77
78    pub fn as_name(&self) -> Option<&str> {
79        match self {
80            Object::Name(n) => Some(n),
81            _ => None,
82        }
83    }
84
85    pub fn as_array(&self) -> Option<&Vec<Object>> {
86        match self {
87            Object::Array(arr) => Some(arr),
88            _ => None,
89        }
90    }
91
92    pub fn as_dict(&self) -> Option<&Dictionary> {
93        match self {
94            Object::Dictionary(dict) => Some(dict),
95            _ => None,
96        }
97    }
98}
99
100impl From<bool> for Object {
101    fn from(b: bool) -> Self {
102        Object::Boolean(b)
103    }
104}
105
106impl From<i32> for Object {
107    fn from(i: i32) -> Self {
108        Object::Integer(i as i64)
109    }
110}
111
112impl From<i64> for Object {
113    fn from(i: i64) -> Self {
114        Object::Integer(i)
115    }
116}
117
118impl From<f32> for Object {
119    fn from(f: f32) -> Self {
120        Object::Real(f as f64)
121    }
122}
123
124impl From<f64> for Object {
125    fn from(f: f64) -> Self {
126        Object::Real(f)
127    }
128}
129
130impl From<String> for Object {
131    fn from(s: String) -> Self {
132        Object::String(s)
133    }
134}
135
136impl From<&str> for Object {
137    fn from(s: &str) -> Self {
138        Object::String(s.to_string())
139    }
140}
141
142impl From<Vec<Object>> for Object {
143    fn from(v: Vec<Object>) -> Self {
144        Object::Array(v)
145    }
146}
147
148impl From<Dictionary> for Object {
149    fn from(d: Dictionary) -> Self {
150        Object::Dictionary(d)
151    }
152}