easydoc_writer/style/
auto_width.rs1#[derive(Debug, Clone, Default)]
9pub struct AutoWidthStrategy {
10 pub min_width: u32,
12 pub max_width: u32,
14}
15
16impl AutoWidthStrategy {
17 #[must_use]
19 pub fn new() -> Self {
20 Self {
21 min_width: 240, max_width: 9600, }
24 }
25
26 #[must_use]
28 pub fn calculate_width(&self, max_content_length: usize) -> u32 {
29 let char_width = 240u32; let clamped_len = u32::try_from(max_content_length).unwrap_or(u32::MAX);
32 let width = clamped_len.saturating_mul(char_width);
33 width.clamp(self.min_width, self.max_width)
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
42 fn default_strategy() {
43 let s = AutoWidthStrategy::default();
44 assert_eq!(s.min_width, 0);
45 assert_eq!(s.max_width, 0);
46 }
47
48 #[test]
49 fn new_has_correct_defaults() {
50 let s = AutoWidthStrategy::new();
51 assert_eq!(s.min_width, 240);
52 assert_eq!(s.max_width, 9600);
53 }
54
55 #[test]
56 fn calculate_width_short_content() {
57 let s = AutoWidthStrategy::new();
58 let w = s.calculate_width(1);
59 assert_eq!(w, 240); }
61
62 #[test]
63 fn calculate_width_medium_content() {
64 let s = AutoWidthStrategy::new();
65 let w = s.calculate_width(10);
66 assert_eq!(w, 2400); }
68
69 #[test]
70 fn calculate_width_long_content() {
71 let s = AutoWidthStrategy::new();
72 let w = s.calculate_width(100);
73 assert_eq!(w, 9600); }
75
76 #[test]
77 fn calculate_width_zero_content() {
78 let s = AutoWidthStrategy::new();
79 let w = s.calculate_width(0);
80 assert_eq!(w, 240); }
82
83 #[test]
84 fn custom_min_max() {
85 let s = AutoWidthStrategy {
86 min_width: 100,
87 max_width: 5000,
88 };
89 assert_eq!(s.calculate_width(0), 100);
90 assert_eq!(s.calculate_width(100), 5000);
91 }
92}