clippier 0.3.0

MoosicBox clippier package
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Tokenizer for filter expressions.
//!
//! This module tokenizes filter expression strings into tokens that can be parsed
//! into a filter expression AST. It handles:
//! * Quoted strings with escape sequences
//! * Logical operators (AND, OR, NOT)
//! * Grouping with parentheses
//! * Filter conditions
//! * Full Unicode support (multibyte characters, emoji, RTL text)

use super::parser::parse_filter;
use super::types::{FilterError, Token};
use std::iter::Peekable;
use std::str::CharIndices;

/// Generic helper to check if string starts with a keyword (case-insensitive, char-aware).
///
/// This uses character-aware iteration to avoid byte boundary panics with multibyte UTF-8.
/// The function checks that the string has at least as many characters as the keyword,
/// and that each character matches (case-insensitively).
///
/// # Examples
///
/// ```
/// # use clippier::package_filter::tokenizer::starts_with_keyword;
/// assert!(starts_with_keyword("AND", "AND"));
/// assert!(starts_with_keyword("and", "AND"));
/// assert!(starts_with_keyword("ANDROID", "AND")); // true - word boundaries checked by caller
/// assert!(!starts_with_keyword("AN", "AND")); // false - too short
/// ```
#[must_use]
pub fn starts_with_keyword(s: &str, keyword: &str) -> bool {
    s.chars()
        .zip(keyword.chars())
        .all(|(a, b)| a.eq_ignore_ascii_case(&b))
        && s.chars().count() >= keyword.len()
}

/// Check if string starts with "AND" (case-insensitive).
///
/// This is a convenience wrapper around `starts_with_keyword` for the AND keyword.
#[inline]
fn starts_with_and(s: &str) -> bool {
    starts_with_keyword(s, "AND")
}

/// Check if string starts with "OR" (case-insensitive).
///
/// This is a convenience wrapper around `starts_with_keyword` for the OR keyword.
#[inline]
fn starts_with_or(s: &str) -> bool {
    starts_with_keyword(s, "OR")
}

/// Check if string starts with "NOT" (case-insensitive).
///
/// This is a convenience wrapper around `starts_with_keyword` for the NOT keyword.
#[inline]
fn starts_with_not(s: &str) -> bool {
    starts_with_keyword(s, "NOT")
}

/// Tokenize a filter expression string.
///
/// # Examples
///
/// * `"package.publish=false"` → `[Filter("publish=false")]`
/// * `"package.publish=false AND package.version^=0.1"` → `[Filter("publish=false"), And, Filter("version^=0.1")]`
/// * `"(package.name=\"test pkg\" OR package.version^=\"0.1\")"` → `[LeftParen, Filter(...), Or, Filter(...), RightParen]`
///
/// # Errors
///
/// * Returns error if quotes are unclosed
/// * Returns error if filter syntax is invalid
pub fn tokenize(input: &str) -> Result<Vec<Token>, FilterError> {
    let mut tokens = Vec::new();
    let mut chars = input.char_indices().peekable();

    while let Some((start_idx, ch)) = chars.next() {
        match ch {
            // Skip whitespace
            ' ' | '\t' | '\n' | '\r' => {}

            // Grouping
            '(' => tokens.push(Token::LeftParen),
            ')' => tokens.push(Token::RightParen),

            // Check for keywords (AND, OR, NOT)
            'A' | 'a' | 'O' | 'o' | 'N' | 'n' => {
                if let Some((keyword_token, _)) = try_parse_keyword(input, start_idx, &mut chars) {
                    tokens.push(keyword_token);
                } else {
                    // Not a keyword - parse as filter
                    let filter = parse_filter_token(input, start_idx, &mut chars)?;
                    tokens.push(Token::Filter(filter));
                }
            }

            // Everything else starts a filter condition
            _ => {
                let filter = parse_filter_token(input, start_idx, &mut chars)?;
                tokens.push(Token::Filter(filter));
            }
        }
    }

    Ok(tokens)
}

/// Try to parse a keyword (AND, OR, NOT) at the current position.
///
/// Returns the token and how many characters were consumed, or None if not a keyword.
/// Does not consume from the chars iterator - caller must consume if successful.
fn try_parse_keyword(
    input: &str,
    start_idx: usize,
    chars: &mut Peekable<CharIndices>,
) -> Option<(Token, usize)> {
    // Look ahead without consuming
    let remaining = &input[start_idx..];

    // Try to match keywords using char-aware comparison
    let (keyword, keyword_len) = if starts_with_and(remaining) {
        (Some(Token::And), 3)
    } else if starts_with_not(remaining) {
        (Some(Token::Not), 3)
    } else if starts_with_or(remaining) {
        (Some(Token::Or), 2)
    } else {
        return None;
    };

    if let Some(token) = keyword {
        // Verify word boundaries
        let before_ok = start_idx == 0
            || input[..start_idx]
                .chars()
                .last()
                .is_none_or(|c| c.is_whitespace() || c == '(');

        // Check what comes after the keyword
        let after_ok = if remaining.len() == keyword_len {
            true // End of string
        } else {
            remaining
                .chars()
                .nth(keyword_len)
                .is_none_or(|c| c.is_whitespace() || c == '(' || c == ')')
        };

        if before_ok && after_ok {
            // Consume the keyword characters
            for _ in 0..keyword_len {
                chars.next();
            }
            return Some((token, keyword_len));
        }
    }

    None
}

/// Parse a filter condition token, handling quotes and escape sequences.
fn parse_filter_token(
    input: &str,
    start_idx: usize,
    chars: &mut Peekable<CharIndices>,
) -> Result<String, FilterError> {
    let mut filter_chars = Vec::new();
    let mut in_quotes = false;
    let mut escape_next = false;

    // First, collect the character at start_idx
    if let Some(ch) = input[start_idx..].chars().next() {
        filter_chars.push(ch);
        if ch == '"' {
            in_quotes = true;
        }
    }

    // Continue collecting characters
    while let Some(&(idx, ch)) = chars.peek() {
        if escape_next {
            // After backslash, take character literally
            filter_chars.push(ch);
            escape_next = false;
            chars.next();
            continue;
        }

        match ch {
            '\\' if in_quotes => {
                // Escape sequence in quoted string
                filter_chars.push(ch);
                escape_next = true;
                chars.next();
            }
            '"' => {
                // Toggle quote state
                filter_chars.push(ch);
                in_quotes = !in_quotes;
                chars.next();
            }
            '(' | ')' if !in_quotes => {
                // Grouping operators end the filter (unless quoted)
                break;
            }
            _ if !in_quotes && ch.is_whitespace() => {
                // Check if we're at a keyword boundary
                if looks_like_keyword_at(input, idx) {
                    break;
                }
                // Otherwise, whitespace might be part of the filter (before operator)
                filter_chars.push(ch);
                chars.next();
            }
            _ if !in_quotes && ch.is_alphabetic() => {
                // Check if we're at a keyword
                if looks_like_keyword_at(input, idx) {
                    break;
                }
                filter_chars.push(ch);
                chars.next();
            }
            _ => {
                // Inside quotes or regular characters, continue collecting
                filter_chars.push(ch);
                chars.next();
            }
        }
    }

    // Check for unclosed quotes
    if in_quotes {
        return Err(FilterError::UnclosedQuote(format!(
            "Unclosed quote in filter starting at position {start_idx}"
        )));
    }

    let filter_str: String = filter_chars.iter().collect();
    let filter_str = filter_str.trim();

    if filter_str.is_empty() {
        return Err(FilterError::InvalidSyntax(
            "Empty filter condition".to_string(),
        ));
    }

    // Validate it's a real filter by trying to parse it
    parse_filter(filter_str)?;

    Ok(filter_str.to_string())
}

/// Check if the position in the input looks like the start of a keyword.
fn looks_like_keyword_at(input: &str, pos: usize) -> bool {
    let remaining = &input[pos..];

    // Skip leading whitespace
    let remaining = remaining.trim_start();
    if remaining.is_empty() {
        return false;
    }

    // Check if it starts with a keyword using char-aware comparison
    let (keyword, keyword_len) = if starts_with_and(remaining) || starts_with_not(remaining) {
        (true, 3)
    } else if starts_with_or(remaining) {
        (true, 2)
    } else {
        return false;
    };

    if !keyword {
        return false;
    }

    // Check word boundary after keyword
    if remaining.len() == keyword_len {
        return true; // End of string
    }

    remaining
        .chars()
        .nth(keyword_len)
        .is_none_or(|c| c.is_whitespace() || c == '(' || c == ')')
}

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

    #[test]
    fn test_tokenize_simple_filter() {
        let tokens = tokenize("publish=false").unwrap();
        assert_eq!(tokens, vec![Token::Filter("publish=false".to_string())]);
    }

    #[test]
    fn test_tokenize_simple_filter_with_nested_path() {
        let tokens = tokenize("package.publish=false").unwrap();
        assert_eq!(
            tokens,
            vec![Token::Filter("package.publish=false".to_string())]
        );
    }

    #[test]
    fn test_tokenize_and_expression() {
        let tokens = tokenize("package.publish=false AND package.version^=0.1").unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::Filter("package.publish=false".to_string()),
                Token::And,
                Token::Filter("package.version^=0.1".to_string()),
            ]
        );
    }

    #[test]
    fn test_tokenize_or_expression() {
        let tokens = tokenize("package.publish=false OR package.version^=0.1").unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::Filter("package.publish=false".to_string()),
                Token::Or,
                Token::Filter("package.version^=0.1".to_string()),
            ]
        );
    }

    #[test]
    fn test_tokenize_not_expression() {
        let tokens = tokenize("NOT package.publish=false").unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::Not,
                Token::Filter("package.publish=false".to_string()),
            ]
        );
    }

    #[test]
    fn test_tokenize_parentheses() {
        let tokens = tokenize("(package.publish=false OR package.version^=0.1)").unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::LeftParen,
                Token::Filter("package.publish=false".to_string()),
                Token::Or,
                Token::Filter("package.version^=0.1".to_string()),
                Token::RightParen,
            ]
        );
    }

    #[test]
    fn test_tokenize_complex_expression() {
        let tokens = tokenize(
            "(package.publish=false OR package.name$=_example) AND package.categories@=audio",
        )
        .unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::LeftParen,
                Token::Filter("package.publish=false".to_string()),
                Token::Or,
                Token::Filter("package.name$=_example".to_string()),
                Token::RightParen,
                Token::And,
                Token::Filter("package.categories@=audio".to_string()),
            ]
        );
    }

    #[test]
    fn test_tokenize_quoted_value() {
        let tokens = tokenize(r#"name="test package""#).unwrap();
        assert_eq!(
            tokens,
            vec![Token::Filter(r#"name="test package""#.to_string())]
        );
    }

    #[test]
    fn test_tokenize_quoted_with_keyword() {
        let tokens = tokenize(r#"description="This AND that""#).unwrap();
        assert_eq!(
            tokens,
            vec![Token::Filter(r#"description="This AND that""#.to_string())]
        );
    }

    #[test]
    fn test_tokenize_quoted_with_escaped_quote() {
        let tokens = tokenize(r#"title="Quote: \"test\"""#).unwrap();
        assert_eq!(
            tokens,
            vec![Token::Filter(r#"title="Quote: \"test\"""#.to_string())]
        );
    }

    #[test]
    fn test_tokenize_case_insensitive_keywords() {
        let tokens =
            tokenize("package.publish=false and package.version^=0.1 OR package.name=test")
                .unwrap();
        assert_eq!(
            tokens,
            vec![
                Token::Filter("package.publish=false".to_string()),
                Token::And,
                Token::Filter("package.version^=0.1".to_string()),
                Token::Or,
                Token::Filter("package.name=test".to_string()),
            ]
        );
    }

    #[test]
    fn test_tokenize_keyword_in_value() {
        // "ANDROID" should not be treated as AND + OID
        let tokens = tokenize("brand=ANDROID").unwrap();
        assert_eq!(tokens, vec![Token::Filter("brand=ANDROID".to_string())]);
    }

    #[test]
    fn test_tokenize_unclosed_quote() {
        let result = tokenize(r#"name="unclosed"#);
        assert!(matches!(result, Err(FilterError::UnclosedQuote(_))));
    }

    #[test]
    fn test_tokenize_standalone_keyword() {
        use super::super::expression_parser::parse_expression;

        // Tokenization should succeed, but parsing should fail
        let tokens = tokenize("AND").unwrap();
        assert_eq!(tokens, vec![Token::And]);

        // Parsing the expression should fail (UnexpectedToken, not ExpectedToken)
        let result = parse_expression("AND");
        assert!(matches!(result, Err(FilterError::UnexpectedToken(_))));
    }

    #[test]
    fn test_starts_with_keyword_generic() {
        // Test the generic helper directly
        assert!(starts_with_keyword("AND", "AND"));
        assert!(starts_with_keyword("and", "AND"));
        assert!(starts_with_keyword("AnD", "AND"));
        assert!(starts_with_keyword("ANDROID", "AND")); // Matches prefix

        assert!(!starts_with_keyword("AN", "AND")); // Too short
        assert!(!starts_with_keyword("", "AND"));
        assert!(!starts_with_keyword("OR", "AND"));

        // Test with different keywords
        assert!(starts_with_keyword("OR", "OR"));
        assert!(starts_with_keyword("NOT", "NOT"));

        // Unicode doesn't match ASCII keywords
        assert!(!starts_with_keyword("AND", "AND"));
        assert!(!starts_with_keyword("名前", "AND"));
    }

    #[test]
    fn test_starts_with_and() {
        assert!(starts_with_and("AND"));
        assert!(starts_with_and("AND "));
        assert!(starts_with_and("and"));
        assert!(starts_with_and("AnD"));
        assert!(starts_with_and("AND rest of string"));

        assert!(!starts_with_and("AN")); // Too short
        assert!(starts_with_and("ANDROID")); // This DOES match AND prefix - boundaries checked by caller
        assert!(!starts_with_and("OR"));
        assert!(!starts_with_and(""));
        assert!(!starts_with_and("名前")); // Unicode chars
    }

    #[test]
    fn test_starts_with_or() {
        assert!(starts_with_or("OR"));
        assert!(starts_with_or("or"));
        assert!(starts_with_or("Or"));
        assert!(starts_with_or("OR more"));

        assert!(!starts_with_or("O")); // Too short
        assert!(!starts_with_or("AND"));
        assert!(!starts_with_or(""));
    }

    #[test]
    fn test_starts_with_not() {
        assert!(starts_with_not("NOT"));
        assert!(starts_with_not("not"));
        assert!(starts_with_not("NoT"));
        assert!(starts_with_not("NOT more"));

        assert!(!starts_with_not("NO")); // Too short
        assert!(!starts_with_not("AND"));
        assert!(!starts_with_not(""));
    }

    #[test]
    fn test_unicode_does_not_match_keywords() {
        // Full-width characters that look like AND/OR/NOT
        assert!(!starts_with_and("AND"));
        assert!(!starts_with_or("OR"));
        assert!(!starts_with_not("NOT"));

        // Unicode that starts with similar letters
        assert!(!starts_with_and("名前"));
        assert!(!starts_with_or("おはよう"));
    }
}