Skip to main content

ass_editor/utils/search/
options.rs

1//! Search option, result, statistics, and error types
2//!
3//! Defines the configuration and data types used by the
4//! [`DocumentSearch`](super::DocumentSearch) trait for document queries.
5
6use crate::core::{Position, Range};
7
8#[cfg(feature = "std")]
9use std::borrow::Cow;
10
11#[cfg(not(feature = "std"))]
12use alloc::borrow::Cow;
13
14#[cfg(not(feature = "std"))]
15use alloc::{string::String, vec::Vec};
16
17/// Search options for document queries
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct SearchOptions {
20    /// Whether to perform case-sensitive search
21    pub case_sensitive: bool,
22
23    /// Whether to match whole words only
24    pub whole_words: bool,
25
26    /// Maximum number of results to return (0 = unlimited)
27    pub max_results: usize,
28
29    /// Whether to use regular expressions
30    pub use_regex: bool,
31
32    /// Search scope (e.g., specific sections or lines)
33    pub scope: SearchScope,
34}
35
36impl Default for SearchOptions {
37    fn default() -> Self {
38        Self {
39            case_sensitive: false,
40            whole_words: false,
41            max_results: 100,
42            use_regex: false,
43            scope: SearchScope::All,
44        }
45    }
46}
47
48/// Defines the scope of a search operation
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum SearchScope {
51    /// Search entire document
52    All,
53
54    /// Search within specific line range
55    Lines { start: usize, end: usize },
56
57    /// Search within specific sections (e.g., Events, Styles)
58    Sections(Vec<String>),
59
60    /// Search within a character range
61    Range(Range),
62}
63
64/// A single search result
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct SearchResult<'a> {
67    /// Position where the match starts
68    pub start: Position,
69
70    /// Position where the match ends
71    pub end: Position,
72
73    /// The matched text
74    pub text: Cow<'a, str>,
75
76    /// Context around the match (for display purposes)
77    pub context: Cow<'a, str>,
78
79    /// Line number where the match occurs (0-based)
80    pub line: usize,
81
82    /// Column where the match starts (0-based)
83    pub column: usize,
84}
85
86/// Statistics about search operations
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct SearchStats {
89    /// Number of matches found
90    pub match_count: usize,
91
92    /// Time taken for the search in microseconds
93    pub search_time_us: u64,
94
95    /// Whether the search hit the result limit
96    pub hit_limit: bool,
97
98    /// Size of the search index in bytes
99    pub index_size: usize,
100}
101
102/// Error types specific to search operations
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub enum SearchError {
105    /// Invalid regular expression
106    InvalidRegex { pattern: String, error: String },
107
108    /// Search index is corrupted or outdated
109    IndexCorrupted,
110
111    /// Feature not available (e.g., FST not compiled)
112    FeatureNotAvailable { feature: String },
113
114    /// Search operation timed out
115    Timeout { duration_ms: u64 },
116}