Skip to main content

ass_editor/core/fluent/
event_query.rs

1//! Event query builder: filter and sort configuration.
2//!
3//! The [`EventQuery`] fields are `pub(super)` so the sibling execution
4//! ([`super::event_query_exec`]) and filtering ([`super::event_query_filter`])
5//! modules can read the configured filters, sort options, and document handle.
6
7use super::{EventFilter, EventSortCriteria, EventSortOptions};
8use crate::core::EditorDocument;
9use ass_core::parser::ast::EventType;
10
11#[cfg(not(feature = "std"))]
12use alloc::string::ToString;
13
14/// Main query builder for filtering and sorting events
15pub struct EventQuery<'a> {
16    pub(super) document: &'a mut EditorDocument,
17    pub(super) filters: EventFilter,
18    pub(super) sort_options: Option<EventSortOptions>,
19    pub(super) limit: Option<usize>,
20}
21
22impl<'a> EventQuery<'a> {
23    pub(crate) fn new(document: &'a mut EditorDocument) -> Self {
24        Self {
25            document,
26            filters: EventFilter::default(),
27            sort_options: None,
28            limit: None,
29        }
30    }
31
32    // Filter methods
33    pub fn filter(mut self, filter: EventFilter) -> Self {
34        self.filters = filter;
35        self
36    }
37
38    pub fn filter_by_type(mut self, event_type: EventType) -> Self {
39        self.filters.event_type = Some(event_type);
40        self
41    }
42
43    pub fn filter_by_style(mut self, pattern: &str) -> Self {
44        self.filters.style_pattern = Some(pattern.to_string());
45        self
46    }
47
48    pub fn filter_by_speaker(mut self, pattern: &str) -> Self {
49        self.filters.speaker_pattern = Some(pattern.to_string());
50        self
51    }
52
53    pub fn filter_by_text(mut self, pattern: &str) -> Self {
54        self.filters.text_pattern = Some(pattern.to_string());
55        self
56    }
57
58    pub fn filter_by_time_range(mut self, start_cs: u32, end_cs: u32) -> Self {
59        self.filters.time_range = Some((start_cs, end_cs));
60        self
61    }
62
63    pub fn filter_by_layer(mut self, layer: u32) -> Self {
64        self.filters.layer = Some(layer);
65        self
66    }
67
68    pub fn filter_by_effect(mut self, pattern: &str) -> Self {
69        self.filters.effect_pattern = Some(pattern.to_string());
70        self
71    }
72
73    pub fn with_regex(mut self, use_regex: bool) -> Self {
74        self.filters.use_regex = use_regex;
75        self
76    }
77
78    pub fn case_sensitive(mut self, case_sensitive: bool) -> Self {
79        self.filters.case_sensitive = case_sensitive;
80        self
81    }
82
83    // Sort methods
84    pub fn sort(mut self, criteria: EventSortCriteria) -> Self {
85        self.sort_options = Some(EventSortOptions {
86            criteria,
87            secondary: None,
88            ascending: true,
89        });
90        self
91    }
92
93    pub fn sort_by(mut self, options: EventSortOptions) -> Self {
94        self.sort_options = Some(options);
95        self
96    }
97
98    pub fn sort_by_time(self) -> Self {
99        self.sort(EventSortCriteria::StartTime)
100    }
101
102    pub fn sort_by_style(self) -> Self {
103        self.sort(EventSortCriteria::Style)
104    }
105
106    pub fn sort_by_duration(self) -> Self {
107        self.sort(EventSortCriteria::Duration)
108    }
109
110    pub fn descending(mut self) -> Self {
111        if let Some(ref mut options) = self.sort_options {
112            options.ascending = false;
113        }
114        self
115    }
116
117    pub fn then_by(mut self, criteria: EventSortCriteria) -> Self {
118        if let Some(ref mut options) = self.sort_options {
119            options.secondary = Some(criteria);
120        }
121        self
122    }
123
124    // Limit results
125    pub fn limit(mut self, count: usize) -> Self {
126        self.limit = Some(count);
127        self
128    }
129
130    pub fn take(self, count: usize) -> Self {
131        self.limit(count)
132    }
133}