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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use primitive::{Primitive, Dictionary, Stream, PdfString};
use err::{Result, ErrorKind};
use std::io;
use std::fmt;
use std::str;
use std::marker::PhantomData;
use types::write_list;
pub type ObjNr = u64;
pub type GenNr = u16;
pub trait Resolve: {
fn resolve(&self, r: PlainRef) -> Result<Primitive>;
}
impl<F> Resolve for F where F: Fn(PlainRef) -> Result<Primitive> {
fn resolve(&self, r: PlainRef) -> Result<Primitive> {
self(r)
}
}
pub struct NoResolve {}
impl Resolve for NoResolve {
fn resolve(&self, _: PlainRef) -> Result<Primitive> {
Err(ErrorKind::FollowReference.into())
}
}
pub const NO_RESOLVE: &'static Resolve = &NoResolve {} as &Resolve;
pub trait Object {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()>;
}
pub trait FromPrimitive: Sized {
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self>;
}
pub trait FromDict: Sized {
fn from_dict(dict: Dictionary, resolve: &Resolve) -> Result<Self>;
}
pub trait FromStream: Sized {
fn from_stream(dict: Stream, resolve: &Resolve) -> Result<Self>;
}
#[derive(Copy, Clone, Debug)]
pub struct PlainRef {
pub id: ObjNr,
pub gen: GenNr,
}
impl Object for PlainRef {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "{} {} R", self.id, self.gen)
}
}
pub struct Ref<T> {
inner: PlainRef,
_marker: PhantomData<T>
}
impl<T> Ref<T> {
pub fn new(inner: PlainRef) -> Ref<T> {
Ref {
inner: inner,
_marker: PhantomData::default(),
}
}
pub fn from_id(id: ObjNr) -> Ref<T> {
Ref {
inner: PlainRef {id: id, gen: 0},
_marker: PhantomData::default(),
}
}
pub fn get_inner(&self) -> PlainRef {
self.inner
}
}
impl<T: Object> Object for Ref<T> {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
self.inner.serialize(out)
}
}
impl<T> FromPrimitive for Ref<T> {
fn from_primitive(p: Primitive, _: &Resolve) -> Result<Self> {
Ok(Ref::new(p.as_reference()?))
}
}
impl<T> Copy for Ref<T> { }
impl<T> Clone for Ref<T> {
fn clone(&self) -> Ref<T> {
*self
}
}
impl<T> fmt::Debug for Ref<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Ref({})", self.inner.id)
}
}
impl Object for i32 {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "{}", self)
}
}
impl Object for f32 {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "{}", self)
}
}
impl Object for bool {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "{}", self)
}
}
impl Object for Dictionary {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "<<")?;
for (key, val) in self.iter() {
write!(out, "/{} ", key)?;
val.serialize(out)?;
}
write!(out, ">>")
}
}
impl Object for str {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
for b in self.chars() {
match b {
'\\' | '(' | ')' => write!(out, r"\")?,
c if c > '~' => panic!("only ASCII"),
_ => ()
}
write!(out, "{}", b)?;
}
Ok(())
}
}
impl Object for String {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
(self as &str).serialize(out)
}
}
impl<T: Object> Object for Vec<T> {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write_list(out, self.iter())
}
}
impl Object for PdfString {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write!(out, "({})", str::from_utf8(self.as_bytes()).unwrap())
}
}
impl<T: Object> Object for [T] {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
write_list(out, self.iter())
}
}
impl Object for Primitive {
fn serialize<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
match *self {
Primitive::Null => write!(out, "null"),
Primitive::Integer (ref x) => x.serialize(out),
Primitive::Number (ref x) => x.serialize(out),
Primitive::Boolean (ref x) => x.serialize(out),
Primitive::String (_) => unimplemented!(),
Primitive::Stream (_) => unimplemented!(),
Primitive::Dictionary (ref x) => x.serialize(out),
Primitive::Array (ref x) => x.serialize(out),
Primitive::Reference (ref x) => x.serialize(out),
Primitive::Name (ref x) => x.serialize(out),
}
}
}
impl<'a, T> Object for &'a T where T: Object {
fn serialize<W: io::Write>(&self, _: &mut W) -> io::Result<()> {
unimplemented!();
}
}