use laminapdf_cos::{CosMut, Error, Object, Result};
pub(crate) fn set_catalog_entry(cos: &mut CosMut<'_>, key: &str, value: Option<Object>) -> Result<()> {
let root = cos.trailer().get("Root").cloned();
match root {
Some(Object::Ref(r)) => {
let current = cos.get(r)?.ok_or(Error::MissingRoot)?;
let Object::Dict(mut catalog) = (*current).clone() else {
return Err(Error::Type {
expected: "dictionary",
context: "/Root",
});
};
put(&mut catalog, key, value);
cos.set(r, Object::Dict(catalog))
}
Some(Object::Dict(mut catalog)) => {
put(&mut catalog, key, value);
let mut trailer = cos.trailer().clone();
trailer.insert("Root", Object::Dict(catalog));
cos.set_trailer(trailer);
Ok(())
}
_ => Err(Error::MissingRoot),
}
}
fn put(dict: &mut laminapdf_cos::Dict, key: &str, value: Option<Object>) {
match value {
Some(value) => dict.insert(key, value),
None => {
dict.remove(key);
}
}
}