-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresolve.rs
165 lines (150 loc) · 5.31 KB
/
resolve.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::{
error::PdfResult,
objects::{Dictionary, Object, Reference},
stream::Stream,
};
pub trait Resolve<'a> {
fn lex_object_from_reference(&mut self, reference: Reference) -> PdfResult<Object<'a>>;
/// Whether or not the reference points to an existing object
fn reference_exists(&mut self, reference: Reference) -> PdfResult<bool>;
fn assert_integer(&mut self, obj: Object) -> PdfResult<i32> {
match obj {
Object::Integer(i) => Ok(i),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_integer(obj)
}
obj => anyhow::bail!("expected integer, found {:?}", obj),
}
}
fn assert_unsigned_integer(&mut self, obj: Object) -> PdfResult<u32> {
match obj {
Object::Integer(i) => Ok(u32::try_from(i)?),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_unsigned_integer(obj)
}
obj => anyhow::bail!("expected unsigned integer, found {:?}", obj),
}
}
/// Either an integer, or a real
fn assert_number(&mut self, obj: Object) -> PdfResult<f32> {
match obj {
Object::Integer(i) => Ok(i as f32),
Object::Real(i) => Ok(i),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_number(obj)
}
obj => anyhow::bail!("expected real, found {:?}", obj),
}
}
fn assert_dict(&mut self, obj: Object<'a>) -> PdfResult<Dictionary<'a>> {
match obj {
Object::Dictionary(d) => Ok(d),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_dict(obj)
}
obj => anyhow::bail!("expected dictionary, found {:?}", obj),
}
}
fn assert_name(&mut self, obj: Object) -> PdfResult<String> {
match obj {
Object::Name(n) => Ok(n),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_name(obj)
}
obj => anyhow::bail!("expected name, found {:?}", obj),
}
}
fn assert_string(&mut self, obj: Object) -> PdfResult<String> {
match obj {
Object::String(s) => Ok(s),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_string(obj)
}
obj => anyhow::bail!("expected string, found {:?}", obj),
}
}
fn assert_arr(&mut self, obj: Object<'a>) -> PdfResult<Vec<Object<'a>>> {
match obj {
Object::Array(a) => Ok(a),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_arr(obj)
}
obj => anyhow::bail!("expected array, found {:?}", obj),
}
}
fn assert_bool(&mut self, obj: Object) -> PdfResult<bool> {
match obj {
Object::True => Ok(true),
Object::False => Ok(false),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_bool(obj)
}
obj => anyhow::bail!("expected boolean, found {:?}", obj),
}
}
fn assert_stream(&mut self, obj: Object<'a>) -> PdfResult<Stream<'a>> {
match obj {
Object::Stream(s) => Ok(s),
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_stream(obj)
}
obj => anyhow::bail!("expected stream, found {:?}", obj),
}
}
/// Resolve all references
fn resolve(&mut self, obj: Object<'a>) -> PdfResult<Object<'a>> {
match obj {
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.resolve(obj)
}
obj => Ok(obj),
}
}
fn assert_dict_or_null(&mut self, obj: Object<'a>) -> PdfResult<Option<Dictionary<'a>>> {
match obj {
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_dict_or_null(obj)
}
Object::Null => Ok(None),
obj => Some(self.assert_dict(obj)).transpose(),
}
}
fn assert_number_or_null(&mut self, obj: Object) -> PdfResult<Option<f32>> {
match obj {
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_number_or_null(obj)
}
Object::Null => Ok(None),
obj => Some(self.assert_number(obj)).transpose(),
}
}
fn assert_or_null<T>(
&mut self,
obj: Object,
convert: impl Fn(&mut Self, Object) -> PdfResult<T>,
) -> PdfResult<Option<T>>
where
Self: Sized,
{
match obj {
Object::Reference(r) => {
let obj = self.lex_object_from_reference(r)?;
self.assert_or_null(obj, convert)
}
Object::Null => Ok(None),
obj => Some(convert(self, obj)).transpose(),
}
}
}