Skip to main content

ass_editor/core/fluent/
media.rs

1//! Fluent API for managing embedded fonts and graphics.
2
3use crate::commands::{
4    AddFontCommand, AddGraphicCommand, ClearFontsCommand, ClearGraphicsCommand, EditorCommand,
5    ListFontsCommand, ListGraphicsCommand, RemoveFontCommand, RemoveGraphicCommand,
6};
7use crate::core::{EditorDocument, Result};
8
9#[cfg(not(feature = "std"))]
10use alloc::{
11    string::{String, ToString},
12    vec::Vec,
13};
14
15/// Fluent API for managing fonts
16pub struct FontsOps<'a> {
17    document: &'a mut EditorDocument,
18}
19
20impl<'a> FontsOps<'a> {
21    /// Create new fonts operations
22    pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
23        Self { document }
24    }
25
26    /// Add a font from UU-encoded data
27    pub fn add(self, filename: &str, data_lines: Vec<String>) -> Result<&'a mut EditorDocument> {
28        let command = AddFontCommand::new(filename.to_string(), data_lines);
29        command.execute(self.document)?;
30        Ok(self.document)
31    }
32
33    /// Add a font from binary data (will UU-encode it)
34    pub fn add_binary(self, filename: &str, data: &[u8]) -> Result<&'a mut EditorDocument> {
35        let command = AddFontCommand::from_binary(filename.to_string(), data);
36        command.execute(self.document)?;
37        Ok(self.document)
38    }
39
40    /// Remove a font by filename
41    pub fn remove(self, filename: &str) -> Result<&'a mut EditorDocument> {
42        let command = RemoveFontCommand::new(filename.to_string());
43        command.execute(self.document)?;
44        Ok(self.document)
45    }
46
47    /// List all font filenames
48    pub fn list(&self) -> Result<Vec<String>> {
49        let command = ListFontsCommand::new();
50        command.list(self.document)
51    }
52
53    /// Check if a font exists
54    pub fn exists(&self, filename: &str) -> Result<bool> {
55        Ok(self.list()?.contains(&filename.to_string()))
56    }
57
58    /// Clear all fonts
59    pub fn clear(self) -> Result<&'a mut EditorDocument> {
60        let command = ClearFontsCommand::new();
61        command.execute(self.document)?;
62        Ok(self.document)
63    }
64
65    /// Get count of fonts
66    pub fn count(&self) -> Result<usize> {
67        Ok(self.list()?.len())
68    }
69}
70
71/// Fluent API for managing graphics
72pub struct GraphicsOps<'a> {
73    document: &'a mut EditorDocument,
74}
75
76impl<'a> GraphicsOps<'a> {
77    /// Create new graphics operations
78    pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
79        Self { document }
80    }
81
82    /// Add a graphic from UU-encoded data
83    pub fn add(self, filename: &str, data_lines: Vec<String>) -> Result<&'a mut EditorDocument> {
84        let command = AddGraphicCommand::new(filename.to_string(), data_lines);
85        command.execute(self.document)?;
86        Ok(self.document)
87    }
88
89    /// Add a graphic from binary data (will UU-encode it)
90    pub fn add_binary(self, filename: &str, data: &[u8]) -> Result<&'a mut EditorDocument> {
91        let command = AddGraphicCommand::from_binary(filename.to_string(), data);
92        command.execute(self.document)?;
93        Ok(self.document)
94    }
95
96    /// Remove a graphic by filename
97    pub fn remove(self, filename: &str) -> Result<&'a mut EditorDocument> {
98        let command = RemoveGraphicCommand::new(filename.to_string());
99        command.execute(self.document)?;
100        Ok(self.document)
101    }
102
103    /// List all graphic filenames
104    pub fn list(&self) -> Result<Vec<String>> {
105        let command = ListGraphicsCommand::new();
106        command.list(self.document)
107    }
108
109    /// Check if a graphic exists
110    pub fn exists(&self, filename: &str) -> Result<bool> {
111        Ok(self.list()?.contains(&filename.to_string()))
112    }
113
114    /// Clear all graphics
115    pub fn clear(self) -> Result<&'a mut EditorDocument> {
116        let command = ClearGraphicsCommand::new();
117        command.execute(self.document)?;
118        Ok(self.document)
119    }
120
121    /// Get count of graphics
122    pub fn count(&self) -> Result<usize> {
123        Ok(self.list()?.len())
124    }
125}