#[derive(Debug, Clone)]
pub struct OfdGraphicsDocument {
pub width: f64,
pub height: f64,
pub title: Option<String>,
pub author: Option<String>,
pub page_count: u32,
}
impl OfdGraphicsDocument {
#[must_use]
pub fn new() -> Self {
Self {
width: 210.0,
height: 297.0,
title: None,
author: None,
page_count: 0,
}
}
#[must_use]
pub fn with_size(width: f64, height: f64) -> Self {
Self {
width,
height,
title: None,
author: None,
page_count: 0,
}
}
#[must_use]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
#[must_use]
pub fn author(mut self, author: impl Into<String>) -> Self {
self.author = Some(author.into());
self
}
#[must_use]
pub fn width(&self) -> f64 {
self.width
}
#[must_use]
pub fn height(&self) -> f64 {
self.height
}
#[must_use]
pub fn page_count(&self) -> u32 {
self.page_count
}
pub fn new_page(&mut self) -> u32 {
self.page_count += 1;
self.page_count
}
}
impl Default for OfdGraphicsDocument {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_default_a4() {
let doc = OfdGraphicsDocument::new();
assert!((doc.width - 210.0).abs() < f64::EPSILON);
assert!((doc.height - 297.0).abs() < f64::EPSILON);
assert!(doc.title.is_none());
assert_eq!(doc.page_count(), 0);
}
#[test]
fn test_with_size() {
let doc = OfdGraphicsDocument::with_size(100.0, 200.0);
assert!((doc.width() - 100.0).abs() < f64::EPSILON);
assert!((doc.height() - 200.0).abs() < f64::EPSILON);
}
#[test]
fn test_builder() {
let doc = OfdGraphicsDocument::new()
.title("测试文档")
.author("easyofd");
assert_eq!(doc.title.as_deref(), Some("测试文档"));
assert_eq!(doc.author.as_deref(), Some("easyofd"));
}
#[test]
fn test_new_page() {
let mut doc = OfdGraphicsDocument::new();
assert_eq!(doc.new_page(), 1);
assert_eq!(doc.new_page(), 2);
assert_eq!(doc.page_count(), 2);
}
#[test]
fn test_default() {
let doc = OfdGraphicsDocument::default();
assert_eq!(doc.page_count(), 0);
}
#[test]
fn test_clone_debug() {
let doc = OfdGraphicsDocument::new().title("x");
let doc2 = doc.clone();
assert_eq!(doc2.title.as_deref(), Some("x"));
assert!(format!("{doc:?}").contains("OfdGraphicsDocument"));
}
}