1use easydoc_core::{Color, FontConfig};
6
7#[derive(Clone)]
11pub struct Run {
12 text: String,
13 font: Option<FontConfig>,
14}
15
16impl Run {
17 #[must_use]
19 pub fn new(text: impl Into<String>) -> Self {
20 Self {
21 text: text.into(),
22 font: None,
23 }
24 }
25
26 #[must_use]
28 pub fn text(text: impl Into<String>) -> Self {
29 Self::new(text)
30 }
31
32 #[must_use]
34 pub fn bold(mut self) -> Self {
35 self.font.get_or_insert_default().bold = true;
36 self
37 }
38
39 #[must_use]
41 pub fn italic(mut self) -> Self {
42 self.font.get_or_insert_default().italic = true;
43 self
44 }
45
46 #[must_use]
48 pub fn size(mut self, size: u32) -> Self {
49 self.font.get_or_insert_default().size = Some(size);
50 self
51 }
52
53 #[must_use]
55 pub fn color(mut self, hex: u32) -> Self {
56 self.font.get_or_insert_default().color = Some(Color::from_hex(hex));
57 self
58 }
59
60 #[must_use]
62 pub fn font(mut self, name: impl Into<String>) -> Self {
63 self.font.get_or_insert_default().name = Some(name.into());
64 self
65 }
66
67 #[must_use]
69 pub fn underline(mut self) -> Self {
70 self.font.get_or_insert_default().underline = true;
71 self
72 }
73
74 pub(crate) fn run_text(&self) -> &str {
75 &self.text
76 }
77
78 pub(crate) fn font_config(&self) -> Option<&FontConfig> {
79 self.font.as_ref()
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn run_text_constructor() {
89 let r = Run::text("test");
90 assert_eq!(r.run_text(), "test");
91 assert!(r.font_config().is_none());
92 }
93
94 #[test]
95 fn run_builder_chain() {
96 let r = Run::new("styled")
97 .bold()
98 .italic()
99 .size(28)
100 .color(0xFF0000)
101 .font("Arial")
102 .underline();
103 let font = r.font_config().unwrap();
104 assert!(font.bold);
105 assert!(font.italic);
106 assert_eq!(font.size, Some(28));
107 assert_eq!(font.color, Some(Color::from_hex(0xFF0000)));
108 assert_eq!(font.name.as_deref(), Some("Arial"));
109 assert!(font.underline);
110 }
111}