ass_editor/core/fluent/
media.rs1use 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
15pub struct FontsOps<'a> {
17 document: &'a mut EditorDocument,
18}
19
20impl<'a> FontsOps<'a> {
21 pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
23 Self { document }
24 }
25
26 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 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 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 pub fn list(&self) -> Result<Vec<String>> {
49 let command = ListFontsCommand::new();
50 command.list(self.document)
51 }
52
53 pub fn exists(&self, filename: &str) -> Result<bool> {
55 Ok(self.list()?.contains(&filename.to_string()))
56 }
57
58 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 pub fn count(&self) -> Result<usize> {
67 Ok(self.list()?.len())
68 }
69}
70
71pub struct GraphicsOps<'a> {
73 document: &'a mut EditorDocument,
74}
75
76impl<'a> GraphicsOps<'a> {
77 pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
79 Self { document }
80 }
81
82 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 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 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 pub fn list(&self) -> Result<Vec<String>> {
105 let command = ListGraphicsCommand::new();
106 command.list(self.document)
107 }
108
109 pub fn exists(&self, filename: &str) -> Result<bool> {
111 Ok(self.list()?.contains(&filename.to_string()))
112 }
113
114 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 pub fn count(&self) -> Result<usize> {
123 Ok(self.list()?.len())
124 }
125}