use crate::commands::{
ApplyStyleCommand, CloneStyleCommand, CreateStyleCommand, DeleteStyleCommand, EditStyleCommand,
EditorCommand,
};
use crate::core::{EditorDocument, Result, StyleBuilder};
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
pub struct StyleOps<'a> {
document: &'a mut EditorDocument,
}
impl<'a> StyleOps<'a> {
pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
Self { document }
}
pub fn create(self, name: &str, builder: StyleBuilder) -> Result<&'a mut EditorDocument> {
let command = CreateStyleCommand::new(name.to_string(), builder);
command.execute(self.document)?;
Ok(self.document)
}
pub fn edit(self, name: &str) -> StyleEditor<'a> {
StyleEditor::new(self.document, name.to_string())
}
pub fn delete(self, name: &str) -> Result<&'a mut EditorDocument> {
let command = DeleteStyleCommand::new(name.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn clone(self, source: &str, target: &str) -> Result<&'a mut EditorDocument> {
let command = CloneStyleCommand::new(source.to_string(), target.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn apply(self, old_style: &str, new_style: &str) -> StyleApplicator<'a> {
StyleApplicator::new(self.document, old_style.to_string(), new_style.to_string())
}
}
pub struct StyleEditor<'a> {
document: &'a mut EditorDocument,
command: EditStyleCommand,
}
impl<'a> StyleEditor<'a> {
pub(crate) fn new(document: &'a mut EditorDocument, style_name: String) -> Self {
let command = EditStyleCommand::new(style_name);
Self { document, command }
}
pub fn font(mut self, font: &str) -> Self {
self.command = self.command.set_font(font);
self
}
pub fn size(mut self, size: u32) -> Self {
self.command = self.command.set_size(size);
self
}
pub fn color(mut self, color: &str) -> Self {
self.command = self.command.set_color(color);
self
}
pub fn bold(mut self, bold: bool) -> Self {
self.command = self.command.set_bold(bold);
self
}
pub fn italic(mut self, italic: bool) -> Self {
self.command = self.command.set_italic(italic);
self
}
pub fn alignment(mut self, alignment: u32) -> Self {
self.command = self.command.set_alignment(alignment);
self
}
pub fn field(mut self, name: &str, value: &str) -> Self {
self.command = self.command.set_field(name, value.to_string());
self
}
pub fn apply(self) -> Result<&'a mut EditorDocument> {
self.command.execute(self.document)?;
Ok(self.document)
}
}
pub struct StyleApplicator<'a> {
document: &'a mut EditorDocument,
command: ApplyStyleCommand,
}
impl<'a> StyleApplicator<'a> {
pub(crate) fn new(
document: &'a mut EditorDocument,
old_style: String,
new_style: String,
) -> Self {
let command = ApplyStyleCommand::new(old_style, new_style);
Self { document, command }
}
pub fn with_filter(mut self, filter: &str) -> Self {
self.command = self.command.with_filter(filter.to_string());
self
}
pub fn apply(self) -> Result<&'a mut EditorDocument> {
self.command.execute(self.document)?;
Ok(self.document)
}
}