use std::collections::BTreeMap;
pub type Dict = BTreeMap<String, Object>;
#[derive(Clone, Debug, PartialEq)]
pub enum Object {
Null,
Bool(bool),
Int(i64),
Real(f64),
Str(Vec<u8>),
Name(String),
Array(Vec<Object>),
Dict(Dict),
Stream(PdfStream),
Ref(u32, u16),
Keyword(Vec<u8>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct PdfStream {
pub dict: Dict,
pub rawdata: Vec<u8>,
pub objid: u32,
pub genno: u16,
}
impl Object {
pub fn as_int(&self) -> Option<i64> {
match self {
Object::Int(n) => Some(*n),
_ => None,
}
}
pub fn as_name(&self) -> Option<&str> {
match self {
Object::Name(s) => Some(s),
_ => None,
}
}
pub fn as_dict(&self) -> Option<&Dict> {
match self {
Object::Dict(d) => Some(d),
Object::Stream(s) => Some(&s.dict),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Object]> {
match self {
Object::Array(a) => Some(a),
_ => None,
}
}
pub fn as_stream(&self) -> Option<&PdfStream> {
match self {
Object::Stream(s) => Some(s),
_ => None,
}
}
pub fn as_str_bytes(&self) -> Option<&[u8]> {
match self {
Object::Str(b) => Some(b),
_ => None,
}
}
pub fn type_name(&self) -> Option<&str> {
self.as_dict()
.and_then(|d| d.get("Type"))
.and_then(Object::as_name)
}
}