Skip to main content

ass_editor/core/fluent/
event_deleter.rs

1//! Fluent API for deleting events based on queries.
2
3use super::EventQuery;
4use crate::commands::{BatchDeleteEventsCommand, EditorCommand};
5use crate::core::{EditorDocument, Result};
6use ass_core::parser::ast::EventType;
7
8#[cfg(not(feature = "std"))]
9use alloc::vec::Vec;
10
11/// Fluent API for deleting events based on queries
12pub struct EventDeleter<'a> {
13    document: &'a mut EditorDocument,
14    indices: Vec<usize>,
15}
16
17impl<'a> EventDeleter<'a> {
18    /// Create a new event deleter
19    pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
20        Self {
21            document,
22            indices: Vec::new(),
23        }
24    }
25
26    /// Delete events by their indices
27    pub fn by_indices(mut self, indices: Vec<usize>) -> Self {
28        self.indices = indices;
29        self
30    }
31
32    /// Delete all dialogue events
33    pub fn dialogues(self) -> Result<&'a mut EditorDocument> {
34        let indices = EventQuery::new(self.document)
35            .filter_by_type(EventType::Dialogue)
36            .indices()?;
37        let command = BatchDeleteEventsCommand::new(indices);
38        command.execute(self.document)?;
39        Ok(self.document)
40    }
41
42    /// Delete all comment events
43    pub fn comments(self) -> Result<&'a mut EditorDocument> {
44        let indices = EventQuery::new(self.document)
45            .filter_by_type(EventType::Comment)
46            .indices()?;
47        let command = BatchDeleteEventsCommand::new(indices);
48        command.execute(self.document)?;
49        Ok(self.document)
50    }
51
52    /// Delete events in a time range
53    pub fn in_time_range(self, start_cs: u32, end_cs: u32) -> Result<&'a mut EditorDocument> {
54        let indices = EventQuery::new(self.document)
55            .filter_by_time_range(start_cs, end_cs)
56            .indices()?;
57        let command = BatchDeleteEventsCommand::new(indices);
58        command.execute(self.document)?;
59        Ok(self.document)
60    }
61
62    /// Delete events with a specific style
63    pub fn with_style(self, style: &str) -> Result<&'a mut EditorDocument> {
64        let indices = EventQuery::new(self.document)
65            .filter_by_style(style)
66            .indices()?;
67        let command = BatchDeleteEventsCommand::new(indices);
68        command.execute(self.document)?;
69        Ok(self.document)
70    }
71
72    /// Delete events containing specific text
73    pub fn containing(self, text: &str) -> Result<&'a mut EditorDocument> {
74        let indices = EventQuery::new(self.document)
75            .filter_by_text(text)
76            .indices()?;
77        let command = BatchDeleteEventsCommand::new(indices);
78        command.execute(self.document)?;
79        Ok(self.document)
80    }
81
82    /// Delete all events
83    pub fn all(self) -> Result<&'a mut EditorDocument> {
84        let indices = EventQuery::new(self.document).indices()?;
85        let command = BatchDeleteEventsCommand::new(indices);
86        command.execute(self.document)?;
87        Ok(self.document)
88    }
89
90    /// Execute deletion with the configured indices
91    pub fn execute(self) -> Result<&'a mut EditorDocument> {
92        if self.indices.is_empty() {
93            return Ok(self.document);
94        }
95        let command = BatchDeleteEventsCommand::new(self.indices);
96        command.execute(self.document)?;
97        Ok(self.document)
98    }
99}