easyofd_reader/
page_info.rs1use easyofd_core::{ST_Box, ST_ID, ST_Loc};
6
7#[derive(Debug, Clone)]
11pub struct PageInfo {
12 pub size: ST_Box,
14 pub id: Option<ST_ID>,
16 pub index: usize,
18 pub page_abs_loc: Option<ST_Loc>,
20 pub page_n: usize,
22 pub template_ids: Vec<String>,
24}
25
26impl PageInfo {
27 #[must_use]
29 pub fn new(index: usize, size: ST_Box) -> Self {
30 Self {
31 size,
32 id: None,
33 index,
34 page_abs_loc: None,
35 page_n: index.saturating_sub(1),
36 template_ids: Vec::new(),
37 }
38 }
39
40 #[must_use]
42 pub fn with_id(mut self, id: ST_ID) -> Self {
43 self.id = Some(id);
44 self
45 }
46
47 #[must_use]
49 pub fn with_abs_loc(mut self, loc: ST_Loc) -> Self {
50 self.page_abs_loc = Some(loc);
51 self
52 }
53
54 #[must_use]
56 pub fn with_page_n(mut self, n: usize) -> Self {
57 self.page_n = n;
58 self
59 }
60
61 pub fn add_template(&mut self, template_id: String) {
63 self.template_ids.push(template_id);
64 }
65
66 #[must_use]
68 pub fn width(&self) -> f64 {
69 self.size.width
70 }
71
72 #[must_use]
74 pub fn height(&self) -> f64 {
75 self.size.height
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_page_info_new() {
85 let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
86 let info = PageInfo::new(1, size);
87 assert_eq!(info.index, 1);
88 assert!((info.width() - 210.0).abs() < f64::EPSILON);
89 assert!((info.height() - 297.0).abs() < f64::EPSILON);
90 assert!(info.id.is_none());
91 assert!(info.template_ids.is_empty());
92 }
93
94 #[test]
95 fn test_page_info_with_id() {
96 let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
97 let info = PageInfo::new(1, size).with_id(ST_ID::new(42).unwrap());
98 assert_eq!(info.id.map(|id| id.get()), Some(42));
99 }
100
101 #[test]
102 fn test_page_info_page_n_default() {
103 let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
104 let info = PageInfo::new(3, size);
105 assert_eq!(info.page_n, 2);
106 }
107
108 #[test]
109 fn test_page_info_add_template() {
110 let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
111 let mut info = PageInfo::new(1, size);
112 info.add_template("tpl_0".into());
113 info.add_template("tpl_1".into());
114 assert_eq!(info.template_ids.len(), 2);
115 assert_eq!(info.template_ids[0], "tpl_0");
116 }
117
118 #[test]
119 fn test_page_info_with_page_n() {
120 let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
121 let info = PageInfo::new(1, size).with_page_n(5);
122 assert_eq!(info.page_n, 5);
123 }
124}