oximedia-clips 0.1.8

Professional clip management and logging system for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Clip search engine for querying clips by various fields.
//!
//! Provides `SearchField`, `ClipSearchQuery`, `ClipSearchEngine`, and `ClipSearchResult`.

#![allow(dead_code)]

/// Fields available for searching clips.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SearchField {
    /// Match against the clip name.
    Name,
    /// Match against the clip description or notes.
    Description,
    /// Match against keyword tags.
    Keywords,
    /// Match against the source file path.
    FilePath,
    /// Match against the clip rating (as a string like "5").
    Rating,
    /// Match against all text fields simultaneously.
    All,
}

impl SearchField {
    /// Human-readable label for this search field.
    pub fn label(&self) -> &'static str {
        match self {
            SearchField::Name => "Name",
            SearchField::Description => "Description",
            SearchField::Keywords => "Keywords",
            SearchField::FilePath => "File Path",
            SearchField::Rating => "Rating",
            SearchField::All => "All Fields",
        }
    }

    /// Returns `true` if this field targets metadata rather than content.
    pub fn is_metadata_field(&self) -> bool {
        matches!(self, SearchField::Rating | SearchField::FilePath)
    }
}

/// A single search criterion: a field combined with a query string.
#[derive(Debug, Clone)]
pub struct SearchCriterion {
    /// The field to search in.
    pub field: SearchField,
    /// The text to look for.
    pub value: String,
    /// Whether the match must be case-sensitive.
    pub case_sensitive: bool,
}

impl SearchCriterion {
    /// Create a new case-insensitive criterion.
    pub fn new(field: SearchField, value: impl Into<String>) -> Self {
        Self {
            field,
            value: value.into(),
            case_sensitive: false,
        }
    }

    /// Make this criterion case-sensitive.
    pub fn case_sensitive(mut self) -> Self {
        self.case_sensitive = true;
        self
    }
}

/// A composable query that aggregates multiple `SearchCriterion`s.
#[derive(Debug, Default, Clone)]
pub struct ClipSearchQuery {
    /// All criteria that must be satisfied (AND semantics).
    pub criteria: Vec<SearchCriterion>,
    /// Maximum number of results to return (0 = unlimited).
    pub limit: usize,
    /// Field to sort results by.
    pub sort_by: Option<SearchField>,
}

impl ClipSearchQuery {
    /// Create an empty query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a criterion to the query.
    pub fn add(mut self, criterion: SearchCriterion) -> Self {
        self.criteria.push(criterion);
        self
    }

    /// Set the result limit.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Set the sort field.
    pub fn sort_by(mut self, field: SearchField) -> Self {
        self.sort_by = Some(field);
        self
    }

    /// Returns `true` when at least one criterion has been added.
    pub fn has_criteria(&self) -> bool {
        !self.criteria.is_empty()
    }
}

/// A single clip record used by the search engine.
#[derive(Debug, Clone)]
pub struct ClipRecord {
    /// Identifier for the clip.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Optional description or notes text.
    pub description: String,
    /// Keywords / tags.
    pub keywords: Vec<String>,
    /// Source file path as a string.
    pub file_path: String,
    /// Rating as a numeric string (e.g. "4").
    pub rating: String,
}

impl ClipRecord {
    /// Create a minimal clip record.
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            description: String::new(),
            keywords: Vec::new(),
            file_path: String::new(),
            rating: "0".to_string(),
        }
    }

    /// Add a keyword tag.
    pub fn with_keyword(mut self, kw: impl Into<String>) -> Self {
        self.keywords.push(kw.into());
        self
    }

    /// Set description text.
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Set file path.
    pub fn with_file_path(mut self, path: impl Into<String>) -> Self {
        self.file_path = path.into();
        self
    }

    /// Set rating string.
    pub fn with_rating(mut self, rating: impl Into<String>) -> Self {
        self.rating = rating.into();
        self
    }
}

/// Search result container.
#[derive(Debug, Clone, Default)]
pub struct ClipSearchResult {
    /// Clips that matched the query.
    pub matches: Vec<ClipRecord>,
}

impl ClipSearchResult {
    /// Number of clips that matched.
    pub fn match_count(&self) -> usize {
        self.matches.len()
    }

    /// Returns `true` if no clips matched.
    pub fn is_empty(&self) -> bool {
        self.matches.is_empty()
    }
}

/// In-memory clip search engine.
#[derive(Debug, Default)]
pub struct ClipSearchEngine {
    records: Vec<ClipRecord>,
}

impl ClipSearchEngine {
    /// Create a new engine with no records.
    pub fn new() -> Self {
        Self::default()
    }

    /// Index a clip record for future searches.
    pub fn index(&mut self, record: ClipRecord) {
        self.records.push(record);
    }

    /// Run a search query against all indexed records.
    pub fn search(&self, query: &ClipSearchQuery) -> ClipSearchResult {
        if !query.has_criteria() {
            return ClipSearchResult {
                matches: self.records.clone(),
            };
        }

        let mut matches: Vec<ClipRecord> = self
            .records
            .iter()
            .filter(|r| self.record_matches(r, query))
            .cloned()
            .collect();

        if let Some(ref sort_field) = query.sort_by {
            match sort_field {
                SearchField::Name => matches.sort_by(|a, b| a.name.cmp(&b.name)),
                SearchField::Rating => matches.sort_by(|a, b| a.rating.cmp(&b.rating)),
                _ => {}
            }
        }

        if query.limit > 0 {
            matches.truncate(query.limit);
        }

        ClipSearchResult { matches }
    }

    fn record_matches(&self, record: &ClipRecord, query: &ClipSearchQuery) -> bool {
        query
            .criteria
            .iter()
            .all(|c| self.criterion_matches(record, c))
    }

    fn criterion_matches(&self, record: &ClipRecord, criterion: &SearchCriterion) -> bool {
        let needle = if criterion.case_sensitive {
            criterion.value.clone()
        } else {
            criterion.value.to_lowercase()
        };

        let haystack_contains = |s: &str| -> bool {
            if criterion.case_sensitive {
                s.contains(&needle)
            } else {
                s.to_lowercase().contains(&needle)
            }
        };

        match &criterion.field {
            SearchField::Name => haystack_contains(&record.name),
            SearchField::Description => haystack_contains(&record.description),
            SearchField::Keywords => record.keywords.iter().any(|k| haystack_contains(k)),
            SearchField::FilePath => haystack_contains(&record.file_path),
            SearchField::Rating => haystack_contains(&record.rating),
            SearchField::All => {
                haystack_contains(&record.name)
                    || haystack_contains(&record.description)
                    || record.keywords.iter().any(|k| haystack_contains(k))
                    || haystack_contains(&record.file_path)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_engine() -> ClipSearchEngine {
        let mut engine = ClipSearchEngine::new();
        engine.index(
            ClipRecord::new("c1", "Interview Alpha")
                .with_description("Office interview")
                .with_keyword("interview")
                .with_keyword("indoor")
                .with_rating("5"),
        );
        engine.index(
            ClipRecord::new("c2", "Outdoor Landscape")
                .with_description("Mountain sunset")
                .with_keyword("outdoor")
                .with_keyword("nature")
                .with_rating("4")
                .with_file_path("/footage/landscape.mov"),
        );
        engine.index(
            ClipRecord::new("c3", "Interview Beta")
                .with_keyword("interview")
                .with_rating("3"),
        );
        engine
    }

    #[test]
    fn search_field_label_name() {
        assert_eq!(SearchField::Name.label(), "Name");
    }

    #[test]
    fn search_field_label_all() {
        assert_eq!(SearchField::All.label(), "All Fields");
    }

    #[test]
    fn search_field_is_metadata() {
        assert!(SearchField::Rating.is_metadata_field());
        assert!(!SearchField::Name.is_metadata_field());
    }

    #[test]
    fn query_has_criteria_empty() {
        let q = ClipSearchQuery::new();
        assert!(!q.has_criteria());
    }

    #[test]
    fn query_has_criteria_with_one() {
        let q = ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Name, "test"));
        assert!(q.has_criteria());
    }

    #[test]
    fn search_by_name_finds_matches() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Name, "Interview"));
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 2);
    }

    #[test]
    fn search_by_keyword_finds_matches() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Keywords, "outdoor"));
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 1);
        assert_eq!(result.matches[0].id, "c2");
    }

    #[test]
    fn search_by_description() {
        let engine = sample_engine();
        let q =
            ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Description, "sunset"));
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 1);
    }

    #[test]
    fn search_by_file_path() {
        let engine = sample_engine();
        let q =
            ClipSearchQuery::new().add(SearchCriterion::new(SearchField::FilePath, "landscape"));
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 1);
    }

    #[test]
    fn search_no_match_returns_empty() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Name, "zzznomatch"));
        let result = engine.search(&q);
        assert!(result.is_empty());
    }

    #[test]
    fn search_with_limit() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new()
            .add(SearchCriterion::new(SearchField::All, "i"))
            .with_limit(1);
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 1);
    }

    #[test]
    fn search_empty_query_returns_all() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new();
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 3);
    }

    #[test]
    fn search_case_insensitive_by_default() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new().add(SearchCriterion::new(SearchField::Name, "INTERVIEW"));
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 2);
    }

    #[test]
    fn search_case_sensitive_no_match() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new()
            .add(SearchCriterion::new(SearchField::Name, "INTERVIEW").case_sensitive());
        let result = engine.search(&q);
        assert!(result.is_empty());
    }

    #[test]
    fn search_sorted_by_name() {
        let engine = sample_engine();
        let q = ClipSearchQuery::new()
            .add(SearchCriterion::new(SearchField::Keywords, "interview"))
            .sort_by(SearchField::Name);
        let result = engine.search(&q);
        assert_eq!(result.match_count(), 2);
        assert_eq!(result.matches[0].name, "Interview Alpha");
        assert_eq!(result.matches[1].name, "Interview Beta");
    }
}