Skip to main content

ass_editor/core/fluent/
style.rs

1//! Fluent API builders for style operations.
2
3use 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
12/// Fluent API builder for style operations
13pub struct StyleOps<'a> {
14    document: &'a mut EditorDocument,
15}
16
17impl<'a> StyleOps<'a> {
18    /// Create a new style operations builder
19    pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
20        Self { document }
21    }
22
23    /// Create a new style
24    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    /// Edit an existing style
31    pub fn edit(self, name: &str) -> StyleEditor<'a> {
32        StyleEditor::new(self.document, name.to_string())
33    }
34
35    /// Delete a style
36    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    /// Clone a style
43    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    /// Apply a style to events
50    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
55/// Fluent API builder for editing a specific style
56pub struct StyleEditor<'a> {
57    document: &'a mut EditorDocument,
58    command: EditStyleCommand,
59}
60
61impl<'a> StyleEditor<'a> {
62    /// Create a new style editor
63    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    /// Set font name
69    pub fn font(mut self, font: &str) -> Self {
70        self.command = self.command.set_font(font);
71        self
72    }
73
74    /// Set font size
75    pub fn size(mut self, size: u32) -> Self {
76        self.command = self.command.set_size(size);
77        self
78    }
79
80    /// Set primary color
81    pub fn color(mut self, color: &str) -> Self {
82        self.command = self.command.set_color(color);
83        self
84    }
85
86    /// Set bold
87    pub fn bold(mut self, bold: bool) -> Self {
88        self.command = self.command.set_bold(bold);
89        self
90    }
91
92    /// Set italic
93    pub fn italic(mut self, italic: bool) -> Self {
94        self.command = self.command.set_italic(italic);
95        self
96    }
97
98    /// Set alignment
99    pub fn alignment(mut self, alignment: u32) -> Self {
100        self.command = self.command.set_alignment(alignment);
101        self
102    }
103
104    /// Set a custom field
105    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    /// Apply the changes
111    pub fn apply(self) -> Result<&'a mut EditorDocument> {
112        self.command.execute(self.document)?;
113        Ok(self.document)
114    }
115}
116
117/// Fluent API builder for applying styles to events
118pub struct StyleApplicator<'a> {
119    document: &'a mut EditorDocument,
120    command: ApplyStyleCommand,
121}
122
123impl<'a> StyleApplicator<'a> {
124    /// Create a new style applicator
125    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    /// Only apply to events containing specific text
135    pub fn with_filter(mut self, filter: &str) -> Self {
136        self.command = self.command.with_filter(filter.to_string());
137        self
138    }
139
140    /// Apply the style changes
141    pub fn apply(self) -> Result<&'a mut EditorDocument> {
142        self.command.execute(self.document)?;
143        Ok(self.document)
144    }
145}