rsword_chirho 0.3.0

Core SWORD module library in pure Rust
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// For God so loved the world that he gave his only begotten Son,
// that whoever believes in him should not perish but have eternal life.
// John 3:16

//! Search query parser for SWORD-compatible syntax.
//!
//! Supports:
//! - Boolean operators: AND, OR, NOT
//! - Phrase proximity: "word1 word2"~N
//! - Wildcards: lov*, ?ove
//! - Field-specific search: strongs:G26
//! - Fuzzy search: love~, love~2
//! - Grouping: (word1 OR word2) AND word3

use regex::Regex;
use std::sync::LazyLock;

/// Pattern for quoted phrases with optional proximity.
static PHRASE_PATTERN_CHIRHO: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#""([^"]+)"(?:~(\d+))?"#).unwrap()
});

/// Pattern for field-specific searches.
static FIELD_PATTERN_CHIRHO: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(\w+):(\S+)"#).unwrap()
});

/// Pattern for fuzzy searches.
static FUZZY_PATTERN_CHIRHO: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(\w+)~(\d*)"#).unwrap()
});

/// Pattern for wildcard searches.
static WILDCARD_PATTERN_CHIRHO: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"[\w]*[*?][\w]*"#).unwrap()
});

/// Parsed search query node.
#[derive(Debug, Clone, PartialEq)]
pub enum QueryNodeChirho {
    /// Simple term.
    TermChirho(String),
    /// Phrase (multiple words in order).
    PhraseChirho {
        /// The phrase words.
        words_chirho: Vec<String>,
        /// Optional proximity (slop).
        slop_chirho: Option<u32>,
    },
    /// Boolean AND.
    AndChirho(Box<QueryNodeChirho>, Box<QueryNodeChirho>),
    /// Boolean OR.
    OrChirho(Box<QueryNodeChirho>, Box<QueryNodeChirho>),
    /// Boolean NOT.
    NotChirho(Box<QueryNodeChirho>),
    /// Field-specific search.
    FieldChirho {
        /// Field name.
        field_chirho: String,
        /// Query for that field.
        query_chirho: Box<QueryNodeChirho>,
    },
    /// Fuzzy search.
    FuzzyChirho {
        /// The term.
        term_chirho: String,
        /// Edit distance (default 1-2).
        distance_chirho: u8,
    },
    /// Wildcard search.
    WildcardChirho(String),
    /// Grouped query.
    GroupChirho(Box<QueryNodeChirho>),
}

/// Search query parser.
#[derive(Debug, Default)]
pub struct QueryParserChirho {
    /// Default field for searches.
    #[allow(dead_code)]
    default_field_chirho: String,
}

impl QueryParserChirho {
    /// Create a new query parser.
    pub fn new_chirho() -> Self {
        Self {
            default_field_chirho: "text".to_string(),
        }
    }

    /// Create with a specific default field.
    pub fn with_default_field_chirho(field_chirho: &str) -> Self {
        Self {
            default_field_chirho: field_chirho.to_string(),
        }
    }

    /// Parse a query string into a query node.
    pub fn parse_chirho(&self, query_chirho: &str) -> Result<QueryNodeChirho, String> {
        let trimmed_chirho = query_chirho.trim();
        if trimmed_chirho.is_empty() {
            return Err("Empty query".to_string());
        }

        self.parse_or_chirho(trimmed_chirho)
    }

    /// Parse OR expressions (lowest precedence).
    fn parse_or_chirho(&self, query_chirho: &str) -> Result<QueryNodeChirho, String> {
        // Split on OR but not inside quotes
        let parts_chirho = self.split_operator_chirho(query_chirho, " OR ");
        if parts_chirho.len() > 1 {
            let mut result_chirho = self.parse_and_chirho(parts_chirho[0].trim())?;
            for part_chirho in &parts_chirho[1..] {
                let right_chirho = self.parse_and_chirho(part_chirho.trim())?;
                result_chirho = QueryNodeChirho::OrChirho(
                    Box::new(result_chirho),
                    Box::new(right_chirho),
                );
            }
            return Ok(result_chirho);
        }

        self.parse_and_chirho(query_chirho)
    }

    /// Parse AND expressions.
    fn parse_and_chirho(&self, query_chirho: &str) -> Result<QueryNodeChirho, String> {
        // Split on AND
        let parts_chirho = self.split_operator_chirho(query_chirho, " AND ");
        if parts_chirho.len() > 1 {
            let mut result_chirho = self.parse_not_chirho(parts_chirho[0].trim())?;
            for part_chirho in &parts_chirho[1..] {
                let right_chirho = self.parse_not_chirho(part_chirho.trim())?;
                result_chirho = QueryNodeChirho::AndChirho(
                    Box::new(result_chirho),
                    Box::new(right_chirho),
                );
            }
            return Ok(result_chirho);
        }

        self.parse_not_chirho(query_chirho)
    }

    /// Parse NOT expressions.
    fn parse_not_chirho(&self, query_chirho: &str) -> Result<QueryNodeChirho, String> {
        let trimmed_chirho = query_chirho.trim();

        if let Some(rest_chirho) = trimmed_chirho.strip_prefix("NOT ") {
            let parsed_chirho = self.parse_primary_chirho(rest_chirho.trim())?;
            return Ok(QueryNodeChirho::NotChirho(Box::new(parsed_chirho)));
        }

        if let Some(rest_chirho) = trimmed_chirho.strip_prefix('-') {
            let parsed_chirho = self.parse_primary_chirho(rest_chirho.trim())?;
            return Ok(QueryNodeChirho::NotChirho(Box::new(parsed_chirho)));
        }

        self.parse_primary_chirho(trimmed_chirho)
    }

    /// Parse primary expressions (terms, phrases, groups).
    fn parse_primary_chirho(&self, query_chirho: &str) -> Result<QueryNodeChirho, String> {
        let trimmed_chirho = query_chirho.trim();

        // Handle grouped expressions
        if trimmed_chirho.starts_with('(') && trimmed_chirho.ends_with(')') {
            let inner_chirho = &trimmed_chirho[1..trimmed_chirho.len() - 1];
            let parsed_chirho = self.parse_or_chirho(inner_chirho)?;
            return Ok(QueryNodeChirho::GroupChirho(Box::new(parsed_chirho)));
        }

        // Handle quoted phrases with optional proximity
        if let Some(captures_chirho) = PHRASE_PATTERN_CHIRHO.captures(trimmed_chirho) {
            let phrase_chirho = captures_chirho.get(1).unwrap().as_str();
            let slop_chirho = captures_chirho.get(2)
                .and_then(|m_chirho| m_chirho.as_str().parse::<u32>().ok());

            let words_chirho: Vec<String> = phrase_chirho
                .split_whitespace()
                .map(|s_chirho| s_chirho.to_string())
                .collect();

            return Ok(QueryNodeChirho::PhraseChirho {
                words_chirho,
                slop_chirho,
            });
        }

        // Handle field-specific search
        if let Some(captures_chirho) = FIELD_PATTERN_CHIRHO.captures(trimmed_chirho) {
            let field_chirho = captures_chirho.get(1).unwrap().as_str().to_string();
            let value_chirho = captures_chirho.get(2).unwrap().as_str();
            let inner_query_chirho = self.parse_primary_chirho(value_chirho)?;
            return Ok(QueryNodeChirho::FieldChirho {
                field_chirho,
                query_chirho: Box::new(inner_query_chirho),
            });
        }

        // Handle fuzzy search
        if let Some(captures_chirho) = FUZZY_PATTERN_CHIRHO.captures(trimmed_chirho) {
            if !WILDCARD_PATTERN_CHIRHO.is_match(trimmed_chirho) {
                let term_chirho = captures_chirho.get(1).unwrap().as_str().to_string();
                let distance_str_chirho = captures_chirho.get(2).unwrap().as_str();
                let distance_chirho = if distance_str_chirho.is_empty() {
                    2
                } else {
                    distance_str_chirho.parse::<u8>().unwrap_or(2).min(2)
                };
                return Ok(QueryNodeChirho::FuzzyChirho {
                    term_chirho,
                    distance_chirho,
                });
            }
        }

        // Handle wildcard search
        if WILDCARD_PATTERN_CHIRHO.is_match(trimmed_chirho) {
            return Ok(QueryNodeChirho::WildcardChirho(trimmed_chirho.to_string()));
        }

        // Handle implicit AND for multiple space-separated terms
        let words_chirho: Vec<&str> = trimmed_chirho.split_whitespace().collect();
        if words_chirho.len() > 1 {
            let mut result_chirho = QueryNodeChirho::TermChirho(words_chirho[0].to_string());
            for word_chirho in &words_chirho[1..] {
                result_chirho = QueryNodeChirho::AndChirho(
                    Box::new(result_chirho),
                    Box::new(QueryNodeChirho::TermChirho(word_chirho.to_string())),
                );
            }
            return Ok(result_chirho);
        }

        // Simple term
        Ok(QueryNodeChirho::TermChirho(trimmed_chirho.to_string()))
    }

    /// Split on an operator, respecting quoted strings.
    fn split_operator_chirho<'a>(&self, query_chirho: &'a str, operator_chirho: &str) -> Vec<&'a str> {
        let mut parts_chirho = Vec::new();
        let mut in_quote_chirho = false;
        let mut paren_depth_chirho: u32 = 0;
        let mut last_split_chirho = 0;

        let chars_chirho: Vec<char> = query_chirho.chars().collect();
        let op_len_chirho = operator_chirho.len();

        for (i_chirho, ch_chirho) in chars_chirho.iter().enumerate() {
            match ch_chirho {
                '"' => in_quote_chirho = !in_quote_chirho,
                '(' if !in_quote_chirho => paren_depth_chirho += 1,
                ')' if !in_quote_chirho => paren_depth_chirho = paren_depth_chirho.saturating_sub(1),
                _ => {}
            }

            if !in_quote_chirho && paren_depth_chirho == 0
                && query_chirho[i_chirho..].starts_with(operator_chirho)
            {
                parts_chirho.push(&query_chirho[last_split_chirho..i_chirho]);
                last_split_chirho = i_chirho + op_len_chirho;
            }
        }

        parts_chirho.push(&query_chirho[last_split_chirho..]);
        parts_chirho
    }
}

/// Convert a query node to tantivy query syntax.
///
/// This function is only available when the `native` feature is enabled,
/// as it generates query strings for the Tantivy search engine.
#[cfg(feature = "native")]
pub fn to_tantivy_query_chirho(node_chirho: &QueryNodeChirho) -> String {
    match node_chirho {
        QueryNodeChirho::TermChirho(term_chirho) => {
            term_chirho.to_lowercase()
        }
        QueryNodeChirho::PhraseChirho { words_chirho, slop_chirho } => {
            let phrase_chirho = words_chirho.join(" ");
            if let Some(slop_chirho) = slop_chirho {
                format!("\"{}\"~{}", phrase_chirho, slop_chirho)
            } else {
                format!("\"{}\"", phrase_chirho)
            }
        }
        QueryNodeChirho::AndChirho(left_chirho, right_chirho) => {
            format!(
                "({} AND {})",
                to_tantivy_query_chirho(left_chirho),
                to_tantivy_query_chirho(right_chirho)
            )
        }
        QueryNodeChirho::OrChirho(left_chirho, right_chirho) => {
            format!(
                "({} OR {})",
                to_tantivy_query_chirho(left_chirho),
                to_tantivy_query_chirho(right_chirho)
            )
        }
        QueryNodeChirho::NotChirho(inner_chirho) => {
            format!("-{}", to_tantivy_query_chirho(inner_chirho))
        }
        QueryNodeChirho::FieldChirho { field_chirho, query_chirho } => {
            format!("{}:{}", field_chirho, to_tantivy_query_chirho(query_chirho))
        }
        QueryNodeChirho::FuzzyChirho { term_chirho, distance_chirho } => {
            format!("{}~{}", term_chirho.to_lowercase(), distance_chirho)
        }
        QueryNodeChirho::WildcardChirho(pattern_chirho) => {
            pattern_chirho.to_lowercase()
        }
        QueryNodeChirho::GroupChirho(inner_chirho) => {
            format!("({})", to_tantivy_query_chirho(inner_chirho))
        }
    }
}

/// Search scope for limiting search range.
#[derive(Debug, Clone, PartialEq)]
pub enum SearchScopeChirho {
    /// Search entire module.
    AllChirho,
    /// Search Old Testament only.
    OldTestamentChirho,
    /// Search New Testament only.
    NewTestamentChirho,
    /// Search a specific book.
    BookChirho(String),
    /// Search a range of books.
    BookRangeChirho(String, String),
    /// Search a specific chapter.
    ChapterChirho(String, u32),
    /// Custom range (e.g., "Gen 1-3").
    CustomChirho(String),
}

impl SearchScopeChirho {
    /// Parse a scope string.
    pub fn parse_chirho(scope_str_chirho: &str) -> Self {
        let trimmed_chirho = scope_str_chirho.trim();

        if trimmed_chirho.is_empty() || trimmed_chirho.eq_ignore_ascii_case("all") {
            return SearchScopeChirho::AllChirho;
        }

        if trimmed_chirho.eq_ignore_ascii_case("ot") {
            return SearchScopeChirho::OldTestamentChirho;
        }

        if trimmed_chirho.eq_ignore_ascii_case("nt") {
            return SearchScopeChirho::NewTestamentChirho;
        }

        // Check for range (e.g., "Gen-Exod")
        if trimmed_chirho.contains('-') && !trimmed_chirho.contains(' ') {
            let parts_chirho: Vec<&str> = trimmed_chirho.split('-').collect();
            if parts_chirho.len() == 2 {
                return SearchScopeChirho::BookRangeChirho(
                    parts_chirho[0].to_string(),
                    parts_chirho[1].to_string(),
                );
            }
        }

        // Check for chapter (e.g., "Gen 1")
        if let Some(space_pos_chirho) = trimmed_chirho.find(' ') {
            let book_chirho = &trimmed_chirho[..space_pos_chirho];
            let chapter_str_chirho = &trimmed_chirho[space_pos_chirho + 1..];
            if let Ok(chapter_chirho) = chapter_str_chirho.parse::<u32>() {
                return SearchScopeChirho::ChapterChirho(book_chirho.to_string(), chapter_chirho);
            }
        }

        // Assume single book
        SearchScopeChirho::BookChirho(trimmed_chirho.to_string())
    }
}

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

    #[test]
    fn test_parse_simple_term_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("love").unwrap();
        assert_eq!(result_chirho, QueryNodeChirho::TermChirho("love".to_string()));
    }

    #[test]
    fn test_parse_phrase_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("\"God is love\"").unwrap();

        if let QueryNodeChirho::PhraseChirho { words_chirho, slop_chirho } = result_chirho {
            assert_eq!(words_chirho, vec!["God", "is", "love"]);
            assert_eq!(slop_chirho, None);
        } else {
            panic!("Expected phrase node");
        }
    }

    #[test]
    fn test_parse_phrase_with_slop_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("\"God love\"~5").unwrap();

        if let QueryNodeChirho::PhraseChirho { words_chirho, slop_chirho } = result_chirho {
            assert_eq!(words_chirho, vec!["God", "love"]);
            assert_eq!(slop_chirho, Some(5));
        } else {
            panic!("Expected phrase node");
        }
    }

    #[test]
    fn test_parse_and_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("God AND love").unwrap();

        if let QueryNodeChirho::AndChirho(left_chirho, right_chirho) = result_chirho {
            assert_eq!(*left_chirho, QueryNodeChirho::TermChirho("God".to_string()));
            assert_eq!(*right_chirho, QueryNodeChirho::TermChirho("love".to_string()));
        } else {
            panic!("Expected AND node");
        }
    }

    #[test]
    fn test_parse_or_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("God OR Lord").unwrap();

        if let QueryNodeChirho::OrChirho(left_chirho, right_chirho) = result_chirho {
            assert_eq!(*left_chirho, QueryNodeChirho::TermChirho("God".to_string()));
            assert_eq!(*right_chirho, QueryNodeChirho::TermChirho("Lord".to_string()));
        } else {
            panic!("Expected OR node");
        }
    }

    #[test]
    fn test_parse_not_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("NOT evil").unwrap();

        if let QueryNodeChirho::NotChirho(inner_chirho) = result_chirho {
            assert_eq!(*inner_chirho, QueryNodeChirho::TermChirho("evil".to_string()));
        } else {
            panic!("Expected NOT node");
        }
    }

    #[test]
    fn test_parse_field_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("strongs:G26").unwrap();

        if let QueryNodeChirho::FieldChirho { field_chirho, query_chirho } = result_chirho {
            assert_eq!(field_chirho, "strongs");
            assert_eq!(*query_chirho, QueryNodeChirho::TermChirho("G26".to_string()));
        } else {
            panic!("Expected Field node");
        }
    }

    #[test]
    fn test_parse_fuzzy_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("love~").unwrap();

        if let QueryNodeChirho::FuzzyChirho { term_chirho, distance_chirho } = result_chirho {
            assert_eq!(term_chirho, "love");
            assert_eq!(distance_chirho, 2);
        } else {
            panic!("Expected Fuzzy node");
        }
    }

    #[test]
    fn test_parse_fuzzy_with_distance_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("love~1").unwrap();

        if let QueryNodeChirho::FuzzyChirho { term_chirho, distance_chirho } = result_chirho {
            assert_eq!(term_chirho, "love");
            assert_eq!(distance_chirho, 1);
        } else {
            panic!("Expected Fuzzy node");
        }
    }

    #[test]
    fn test_parse_wildcard_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("lov*").unwrap();

        if let QueryNodeChirho::WildcardChirho(pattern_chirho) = result_chirho {
            assert_eq!(pattern_chirho, "lov*");
        } else {
            panic!("Expected Wildcard node");
        }
    }

    #[test]
    fn test_parse_complex_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("(God OR Lord) AND love").unwrap();

        // Should be AND with grouped OR on left
        if let QueryNodeChirho::AndChirho(left_chirho, right_chirho) = result_chirho {
            assert!(matches!(*left_chirho, QueryNodeChirho::GroupChirho(_)));
            assert_eq!(*right_chirho, QueryNodeChirho::TermChirho("love".to_string()));
        } else {
            panic!("Expected AND node");
        }
    }

    #[test]
    #[cfg(feature = "native")]
    fn test_to_tantivy_query_chirho() {
        let node_chirho = QueryNodeChirho::AndChirho(
            Box::new(QueryNodeChirho::TermChirho("God".to_string())),
            Box::new(QueryNodeChirho::TermChirho("love".to_string())),
        );
        let tantivy_query_chirho = to_tantivy_query_chirho(&node_chirho);
        assert_eq!(tantivy_query_chirho, "(god AND love)");
    }

    #[test]
    fn test_scope_parse_chirho() {
        assert_eq!(SearchScopeChirho::parse_chirho(""), SearchScopeChirho::AllChirho);
        assert_eq!(SearchScopeChirho::parse_chirho("OT"), SearchScopeChirho::OldTestamentChirho);
        assert_eq!(SearchScopeChirho::parse_chirho("NT"), SearchScopeChirho::NewTestamentChirho);
        assert_eq!(
            SearchScopeChirho::parse_chirho("Gen-Exod"),
            SearchScopeChirho::BookRangeChirho("Gen".to_string(), "Exod".to_string())
        );
        assert_eq!(
            SearchScopeChirho::parse_chirho("John"),
            SearchScopeChirho::BookChirho("John".to_string())
        );
        assert_eq!(
            SearchScopeChirho::parse_chirho("Gen 1"),
            SearchScopeChirho::ChapterChirho("Gen".to_string(), 1)
        );
    }

    #[test]
    fn test_implicit_and_chirho() {
        let parser_chirho = QueryParserChirho::new_chirho();
        let result_chirho = parser_chirho.parse_chirho("God love world").unwrap();

        // Should be nested ANDs
        if let QueryNodeChirho::AndChirho(_, right_chirho) = result_chirho {
            assert_eq!(*right_chirho, QueryNodeChirho::TermChirho("world".to_string()));
        } else {
            panic!("Expected AND node for implicit AND");
        }
    }
}