use std::fmt;
use laminapdf_syntax::Limits;
use zeroize::Zeroizing;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct OpenOptions {
limits: Limits,
strict: bool,
password: Option<Zeroizing<Vec<u8>>>,
}
impl OpenOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn with_strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
#[must_use]
pub fn with_password(mut self, password: impl Into<Vec<u8>>) -> Self {
self.password = Some(Zeroizing::new(password.into()));
self
}
#[must_use]
pub const fn limits(&self) -> &Limits {
&self.limits
}
#[must_use]
pub const fn strict(&self) -> bool {
self.strict
}
#[must_use]
pub fn password(&self) -> Option<&[u8]> {
self.password.as_deref().map(Vec::as_slice)
}
}
impl fmt::Debug for OpenOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OpenOptions")
.field("limits", &self.limits)
.field("strict", &self.strict)
.field("password", &self.password.as_ref().map(|_| "<redacted>"))
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_redacts_the_password() {
let options = OpenOptions::default().with_password("hunter2");
assert!(!format!("{options:?}").contains("hunter2"));
}
#[test]
fn the_password_is_held_in_a_buffer_wiped_on_drop() {
let options = OpenOptions::default().with_password("hunter2");
let held: Option<&Zeroizing<Vec<u8>>> = options.password.as_ref();
assert_eq!(held.map(|p| p.as_slice()), Some(b"hunter2".as_slice()));
}
#[test]
fn default_is_tolerant_with_default_limits() {
let options = OpenOptions::default();
assert_eq!((options.strict(), *options.limits()), (false, Limits::default()));
}
}