Skip to main content

ass_editor/commands/fonts_graphics_commands/
list.rs

1//! Commands for listing embedded fonts and graphics in ASS documents
2
3use crate::core::{EditorDocument, Result};
4
5#[cfg(not(feature = "std"))]
6use alloc::{
7    string::{String, ToString},
8    vec::Vec,
9};
10
11/// Command to list all embedded fonts in the ASS document
12///
13/// Returns a list of font filenames from the `[Fonts]` section.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct ListFontsCommand;
16
17impl ListFontsCommand {
18    /// Create a new list fonts command
19    pub fn new() -> Self {
20        Self
21    }
22
23    /// Execute and return list of font filenames
24    pub fn list(&self, document: &EditorDocument) -> Result<Vec<String>> {
25        let content = document.text();
26
27        // Find [Fonts] section
28        let fonts_section = content.find("[Fonts]");
29        if fonts_section.is_none() {
30            return Ok(Vec::new());
31        }
32
33        let section_start = fonts_section.unwrap();
34        let section_end = content[section_start..]
35            .find("\n[")
36            .map(|pos| section_start + pos)
37            .unwrap_or(content.len());
38
39        let section_content = &content[section_start..section_end];
40        let mut fonts = Vec::new();
41
42        for line in section_content.lines() {
43            if let Some(filename) = line.strip_prefix("fontname: ") {
44                fonts.push(filename.trim().to_string());
45            }
46        }
47
48        Ok(fonts)
49    }
50}
51
52impl Default for ListFontsCommand {
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58/// Command to list all embedded graphics in the ASS document
59///
60/// Returns a list of graphic filenames from the `[Graphics]` section.
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct ListGraphicsCommand;
63
64impl ListGraphicsCommand {
65    /// Create a new list graphics command
66    pub fn new() -> Self {
67        Self
68    }
69
70    /// Execute and return list of graphic filenames
71    pub fn list(&self, document: &EditorDocument) -> Result<Vec<String>> {
72        let content = document.text();
73
74        // Find [Graphics] section
75        let graphics_section = content.find("[Graphics]");
76        if graphics_section.is_none() {
77            return Ok(Vec::new());
78        }
79
80        let section_start = graphics_section.unwrap();
81        let section_end = content[section_start..]
82            .find("\n[")
83            .map(|pos| section_start + pos)
84            .unwrap_or(content.len());
85
86        let section_content = &content[section_start..section_end];
87        let mut graphics = Vec::new();
88
89        for line in section_content.lines() {
90            if let Some(filename) = line.strip_prefix("filename: ") {
91                graphics.push(filename.trim().to_string());
92            }
93        }
94
95        Ok(graphics)
96    }
97}
98
99impl Default for ListGraphicsCommand {
100    fn default() -> Self {
101        Self::new()
102    }
103}