use crate::commands::{
AddFontCommand, AddGraphicCommand, ClearFontsCommand, ClearGraphicsCommand, EditorCommand,
ListFontsCommand, ListGraphicsCommand, RemoveFontCommand, RemoveGraphicCommand,
};
use crate::core::{EditorDocument, Result};
#[cfg(not(feature = "std"))]
use alloc::{
string::{String, ToString},
vec::Vec,
};
pub struct FontsOps<'a> {
document: &'a mut EditorDocument,
}
impl<'a> FontsOps<'a> {
pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
Self { document }
}
pub fn add(self, filename: &str, data_lines: Vec<String>) -> Result<&'a mut EditorDocument> {
let command = AddFontCommand::new(filename.to_string(), data_lines);
command.execute(self.document)?;
Ok(self.document)
}
pub fn add_binary(self, filename: &str, data: &[u8]) -> Result<&'a mut EditorDocument> {
let command = AddFontCommand::from_binary(filename.to_string(), data);
command.execute(self.document)?;
Ok(self.document)
}
pub fn remove(self, filename: &str) -> Result<&'a mut EditorDocument> {
let command = RemoveFontCommand::new(filename.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn list(&self) -> Result<Vec<String>> {
let command = ListFontsCommand::new();
command.list(self.document)
}
pub fn exists(&self, filename: &str) -> Result<bool> {
Ok(self.list()?.contains(&filename.to_string()))
}
pub fn clear(self) -> Result<&'a mut EditorDocument> {
let command = ClearFontsCommand::new();
command.execute(self.document)?;
Ok(self.document)
}
pub fn count(&self) -> Result<usize> {
Ok(self.list()?.len())
}
}
pub struct GraphicsOps<'a> {
document: &'a mut EditorDocument,
}
impl<'a> GraphicsOps<'a> {
pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
Self { document }
}
pub fn add(self, filename: &str, data_lines: Vec<String>) -> Result<&'a mut EditorDocument> {
let command = AddGraphicCommand::new(filename.to_string(), data_lines);
command.execute(self.document)?;
Ok(self.document)
}
pub fn add_binary(self, filename: &str, data: &[u8]) -> Result<&'a mut EditorDocument> {
let command = AddGraphicCommand::from_binary(filename.to_string(), data);
command.execute(self.document)?;
Ok(self.document)
}
pub fn remove(self, filename: &str) -> Result<&'a mut EditorDocument> {
let command = RemoveGraphicCommand::new(filename.to_string());
command.execute(self.document)?;
Ok(self.document)
}
pub fn list(&self) -> Result<Vec<String>> {
let command = ListGraphicsCommand::new();
command.list(self.document)
}
pub fn exists(&self, filename: &str) -> Result<bool> {
Ok(self.list()?.contains(&filename.to_string()))
}
pub fn clear(self) -> Result<&'a mut EditorDocument> {
let command = ClearGraphicsCommand::new();
command.execute(self.document)?;
Ok(self.document)
}
pub fn count(&self) -> Result<usize> {
Ok(self.list()?.len())
}
}