ass_editor/core/fluent/
style.rs1use crate::commands::{
4 ApplyStyleCommand, CloneStyleCommand, CreateStyleCommand, DeleteStyleCommand, EditStyleCommand,
5 EditorCommand,
6};
7use crate::core::{EditorDocument, Result, StyleBuilder};
8
9#[cfg(not(feature = "std"))]
10use alloc::string::{String, ToString};
11
12pub struct StyleOps<'a> {
14 document: &'a mut EditorDocument,
15}
16
17impl<'a> StyleOps<'a> {
18 pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
20 Self { document }
21 }
22
23 pub fn create(self, name: &str, builder: StyleBuilder) -> Result<&'a mut EditorDocument> {
25 let command = CreateStyleCommand::new(name.to_string(), builder);
26 command.execute(self.document)?;
27 Ok(self.document)
28 }
29
30 pub fn edit(self, name: &str) -> StyleEditor<'a> {
32 StyleEditor::new(self.document, name.to_string())
33 }
34
35 pub fn delete(self, name: &str) -> Result<&'a mut EditorDocument> {
37 let command = DeleteStyleCommand::new(name.to_string());
38 command.execute(self.document)?;
39 Ok(self.document)
40 }
41
42 pub fn clone(self, source: &str, target: &str) -> Result<&'a mut EditorDocument> {
44 let command = CloneStyleCommand::new(source.to_string(), target.to_string());
45 command.execute(self.document)?;
46 Ok(self.document)
47 }
48
49 pub fn apply(self, old_style: &str, new_style: &str) -> StyleApplicator<'a> {
51 StyleApplicator::new(self.document, old_style.to_string(), new_style.to_string())
52 }
53}
54
55pub struct StyleEditor<'a> {
57 document: &'a mut EditorDocument,
58 command: EditStyleCommand,
59}
60
61impl<'a> StyleEditor<'a> {
62 pub(crate) fn new(document: &'a mut EditorDocument, style_name: String) -> Self {
64 let command = EditStyleCommand::new(style_name);
65 Self { document, command }
66 }
67
68 pub fn font(mut self, font: &str) -> Self {
70 self.command = self.command.set_font(font);
71 self
72 }
73
74 pub fn size(mut self, size: u32) -> Self {
76 self.command = self.command.set_size(size);
77 self
78 }
79
80 pub fn color(mut self, color: &str) -> Self {
82 self.command = self.command.set_color(color);
83 self
84 }
85
86 pub fn bold(mut self, bold: bool) -> Self {
88 self.command = self.command.set_bold(bold);
89 self
90 }
91
92 pub fn italic(mut self, italic: bool) -> Self {
94 self.command = self.command.set_italic(italic);
95 self
96 }
97
98 pub fn alignment(mut self, alignment: u32) -> Self {
100 self.command = self.command.set_alignment(alignment);
101 self
102 }
103
104 pub fn field(mut self, name: &str, value: &str) -> Self {
106 self.command = self.command.set_field(name, value.to_string());
107 self
108 }
109
110 pub fn apply(self) -> Result<&'a mut EditorDocument> {
112 self.command.execute(self.document)?;
113 Ok(self.document)
114 }
115}
116
117pub struct StyleApplicator<'a> {
119 document: &'a mut EditorDocument,
120 command: ApplyStyleCommand,
121}
122
123impl<'a> StyleApplicator<'a> {
124 pub(crate) fn new(
126 document: &'a mut EditorDocument,
127 old_style: String,
128 new_style: String,
129 ) -> Self {
130 let command = ApplyStyleCommand::new(old_style, new_style);
131 Self { document, command }
132 }
133
134 pub fn with_filter(mut self, filter: &str) -> Self {
136 self.command = self.command.with_filter(filter.to_string());
137 self
138 }
139
140 pub fn apply(self) -> Result<&'a mut EditorDocument> {
142 self.command.execute(self.document)?;
143 Ok(self.document)
144 }
145}