odict 3.3.0

A blazingly-fast dictionary file format for human languages
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! Advanced dictionary lookup operations for ODict.
//!
//! This module provides sophisticated search capabilities over dictionaries with
//! configurable matching strategies, redirect following via see_also links, and
//! case-insensitive fallback options. It supports both single and batch lookups
//! with parallel processing for optimal performance.
//!
//! # Overview
//!
//! The lookup system offers multiple layers of functionality:
//!
//! ## Matching Strategies
//! - **Exact matching**: Direct term-to-entry mapping
//! - **Split strategy**: Progressive substring matching for compound terms
//!
//! ## Advanced Features
//! - **Redirect following**: Automatic traversal of see_also links with cycle protection
//! - **Case-insensitive fallback**: Automatic retry with lowercase when exact match fails
//! - **Parallel processing**: Concurrent lookup of multiple queries for performance
//! - **Configurable limits**: Control redirect depth and matching behavior
//!
//! ## Performance Characteristics
//! - Single lookups: O(1) average case for exact matches
//! - Split strategy: O(n²) worst case where n is query length
//! - Parallel lookups: Scales with available CPU cores
//! - Memory efficient: Zero-copy results with lifetime management
//!
//! # Examples
//!
//! ## Basic Exact Lookup
//!
//! ```rust
//! use odict::{OpenDictionary, LookupOptions};
//!
//! let dict = OpenDictionary::from_path("dictionary.odict")?;
//! let archived = dict.contents()?;
//!
//! let queries = vec!["hello"];
//! let results = archived.lookup(&queries, LookupOptions::default())?;
//!
//! for result in results {
//!     println!("Found: {}", result.entry.term.as_str());
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Advanced Lookup with Options
//!
//! ```rust
//! use odict::{OpenDictionary, LookupOptions, LookupStrategy};
//!
//! let dict = OpenDictionary::from_path("dictionary.odict")?;
//! let archived = dict.contents()?;
//!
//! let options = LookupOptions::default()
//!     .insensitive(true)           // Enable case-insensitive fallback
//!     .follow(3)                   // Follow up to 3 redirects
//!     .strategy(LookupStrategy::Split(2)); // Split to minimum 2 chars
//!
//! let queries = vec!["Hello", "compound-word"];
//! let results = archived.lookup(&queries, options)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Handling Redirects
//!
//! ```rust
//! use odict::{OpenDictionary, LookupOptions};
//!
//! let dict = OpenDictionary::from_path("dictionary.odict")?;
//! let archived = dict.contents()?;
//!
//! let options = LookupOptions::default().follow(5);
//! let queries = vec!["abbreviation"]; // Might redirect to full form
//! let results = archived.lookup(&queries, options)?;
//!
//! for result in results {
//!     if let Some(redirect_from) = result.directed_from {
//!         println!("'{}' redirected from '{}'",
//!                  result.entry.term.as_str(),
//!                  redirect_from.term.as_str());
//!     }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Split Strategy for Compound Terms
//!
//! ```rust
//! use odict::{OpenDictionary, LookupOptions, LookupStrategy};
//!
//! let dict = OpenDictionary::from_path("dictionary.odict")?;
//! let archived = dict.contents()?;
//!
//! // This will try "compound-word", then "compound", then "word"
//! let options = LookupOptions::default()
//!     .strategy(LookupStrategy::Split(3)); // Minimum 3 characters
//!
//! let queries = vec!["compound-word"];
//! let results = archived.lookup(&queries, options)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
use crate::schema::{ArchivedDictionary, ArchivedEntry, Dictionary, Entry};

use rayon::prelude::*;
use rkyv::option::ArchivedOption;
use std::marker::{Send, Sync};

/// Strategy for matching query terms against dictionary entries.
///
/// This enum defines the different approaches available for finding matches
/// when performing dictionary lookups. Each strategy has different performance
/// characteristics and use cases.
#[derive(Debug, PartialEq, Clone)]
pub enum LookupStrategy {
    /// Match queries exactly against entry terms.
    ///
    /// This is the fastest strategy, performing direct hash map lookups.
    /// It requires the query to exactly match an entry term (case-sensitive
    /// unless the `insensitive` option is enabled).
    ///
    /// **Performance**: O(1) average case
    /// **Use case**: When you know the exact term you're looking for
    ///
    /// # Examples
    ///
    /// ```rust
    /// use odict::{LookupStrategy, LookupOptions};
    ///
    /// let options = LookupOptions::default()
    ///     .strategy(LookupStrategy::Exact);
    /// ```
    Exact,

    /// Split the query into progressively smaller substrings down to `min_length`,
    /// attempting to match each substring from left to right.
    ///
    /// This strategy is useful for compound words or when you want to find
    /// partial matches. It starts with the full query and progressively
    /// shortens it from the right until a match is found or the minimum
    /// length is reached.
    ///
    /// **Performance**: O(n²) worst case where n is query length
    /// **Use case**: Compound words, partial matching, morphological analysis
    ///
    /// # Algorithm
    ///
    /// For a query "compound-word" with min_length=3:
    /// 1. Try "compound-word" (full query)
    /// 2. Try "compound-wor", "compound-wo", etc.
    /// 3. Try "compound" (if found, move to next segment)
    /// 4. Try "word", "wor" (down to min_length)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use odict::{LookupStrategy, LookupOptions};
    ///
    /// // Split down to minimum 3 characters
    /// let options = LookupOptions::default()
    ///     .strategy(LookupStrategy::Split(3));
    /// ```
    Split(usize),
}

/// Configuration options for dictionary lookup operations.
///
/// This struct provides fine-grained control over lookup behavior, including
/// redirect following, matching strategies, and case sensitivity. All options
/// have sensible defaults for common use cases.
///
/// # Default Behavior
///
/// - **No redirect following**: Prevents infinite loops and improves performance
/// - **Exact matching**: Most predictable and fastest lookup strategy
/// - **Case-sensitive search**: Preserves linguistic distinctions
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust
/// use odict::LookupOptions;
///
/// // Use all defaults
/// let options = LookupOptions::default();
/// ```
///
/// ## Custom Configuration
///
/// ```rust
/// use odict::{LookupOptions, LookupStrategy};
///
/// let options = LookupOptions::default()
///     .follow(5)                           // Follow up to 5 redirects
///     .insensitive(true)                   // Enable case-insensitive fallback
///     .strategy(LookupStrategy::Split(2)); // Split strategy with min length 2
/// ```
#[derive(Debug, Clone)]
pub struct LookupOptions {
    /// Whether to follow see_also links until finding an entry with etymologies.
    pub follow: bool,
    pub strategy: LookupStrategy,

    /// Whether to fall back to case-insensitive search if exact match fails.
    ///
    /// When enabled, if an exact (case-sensitive) match fails, the system
    /// will automatically retry with a lowercase version of the query.
    /// This is useful for handling user input that may have incorrect
    /// capitalization.
    ///
    /// **Note**: The fallback only occurs if the lowercase version differs
    /// from the original query, preventing unnecessary duplicate lookups.
    pub insensitive: bool,
}

impl AsRef<LookupOptions> for LookupOptions {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl Default for LookupOptions {
    /// Construct default lookup options with safe, predictable settings.
    ///
    /// The default configuration prioritizes safety and performance:
    /// - **No redirect following**: Prevents infinite loops and improves performance
    /// - **Exact matching strategy**: Most predictable and fastest lookup method
    /// - **Case-sensitive search**: Preserves linguistic distinctions
    ///
    /// # Returns
    ///
    /// A new `LookupOptions` instance with default settings.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use odict::LookupOptions;
    ///
    /// let options = LookupOptions::default();
    /// assert_eq!(options.follow, None);
    /// assert_eq!(options.strategy, odict::LookupStrategy::Exact);
    /// assert_eq!(options.insensitive, false);
    /// ```
    fn default() -> Self {
        Self {
            follow: false,
            strategy: LookupStrategy::Exact,
            insensitive: false,
        }
    }
}

impl LookupOptions {
    pub fn follow(mut self, follow: bool) -> Self {
        self.follow = follow;
        self
    }

    /// Set the matching strategy for query processing.
    ///
    /// The strategy determines how queries are matched against dictionary entries.
    /// Different strategies have different performance characteristics and use cases.
    ///
    /// # Arguments
    ///
    /// * `strategy` - The [`LookupStrategy`] to use for matching
    ///
    /// # Examples
    ///
    /// ```rust
    /// use odict::{LookupOptions, LookupStrategy};
    ///
    /// // Use exact matching (fastest)
    /// let exact = LookupOptions::default()
    ///     .strategy(LookupStrategy::Exact);
    ///
    /// // Use split strategy for compound words
    /// let split = LookupOptions::default()
    ///     .strategy(LookupStrategy::Split(3));
    /// ```
    pub fn strategy(mut self, strategy: LookupStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Enable or disable case-insensitive fallback matching.
    ///
    /// When enabled, if an exact (case-sensitive) match fails, the system
    /// automatically retries with a lowercase version of the query. This is
    /// useful for handling user input with incorrect capitalization.
    ///
    /// # Arguments
    ///
    /// * `insensitive` - Whether to enable case-insensitive fallback
    ///
    /// # Performance Impact
    ///
    /// - Minimal impact when exact matches succeed
    /// - Adds one additional lookup when exact match fails and query contains uppercase
    /// - No additional lookup if the query is already lowercase
    ///
    /// # Examples
    ///
    /// ```rust
    /// use odict::LookupOptions;
    ///
    /// // Enable case-insensitive fallback
    /// let options = LookupOptions::default().insensitive(true);
    ///
    /// // This will try "Hello" first, then "hello" if not found
    /// // let results = dict.lookup(&["Hello"], options)?;
    /// ```
    pub fn insensitive(mut self, insensitive: bool) -> Self {
        self.insensitive = insensitive;
        self
    }
}

/// Result of a dictionary lookup operation.
///
/// This struct encapsulates the result of a successful lookup, including
/// the matched entry and optional redirect information. It provides context
/// about how the match was found, which is useful for understanding the
/// lookup path and handling redirects.
///
/// # Generic Parameter
///
/// * `E` - The entry type (either `&Entry` or `&ArchivedEntry`)
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust
/// use odict::{OpenDictionary, LookupOptions};
///
/// let dict = OpenDictionary::from_path("dictionary.odict")?;
/// let archived = dict.contents()?;
/// let queries = vec!["hello"];
/// let results = archived.lookup(&queries, LookupOptions::default())?;
///
/// for result in results {
///     println!("Found: {}", result.entry.term.as_str());
///
///     if let Some(redirect_from) = result.directed_from {
///         println!("  (redirected from: {})", redirect_from.term.as_str());
///     }
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Checking for Redirects
///
/// ```rust
/// use odict::{OpenDictionary, LookupOptions};
///
/// # fn example(results: Vec<odict::LookupResult<&odict::ArchivedEntry>>) {
/// for result in results {
///     match result.directed_from {
///         Some(original) => {
///             println!("'{}' is an alias for '{}'",
///                      original.term.as_str(),
///                      result.entry.term.as_str());
///         }
///         None => {
///             println!("Direct match: {}", result.entry.term.as_str());
///         }
///     }
/// }
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LookupResult<E> {
    /// The matched dictionary entry.
    ///
    /// This is the final entry that was found, either through direct matching
    /// or by following redirects. It contains all the linguistic data
    /// (definitions, etymologies, pronunciations, etc.) for the term.
    pub entry: E,

    /// The entry that originally directed to this match via see_also links.
    ///
    /// This field is `Some(entry)` when the result was found by following
    /// a redirect chain, containing the entry that started the redirect.
    /// It's `None` for direct matches without any redirects.
    ///
    /// # Use Cases
    ///
    /// - Displaying "redirected from" information to users
    /// - Understanding alias relationships in the dictionary
    /// - Debugging lookup paths and redirect chains
    /// - Analytics on which redirects are commonly followed
    pub directed_from: Option<E>,
}

/* -------------------------------------------------------------------------- */
/*                                   Methods                                  */
/* -------------------------------------------------------------------------- */

macro_rules! lookup {
    ($tys:ident, $ret:ident, $opt:ident) => {
        impl $tys {
            #[doc = r#"Attempt to find a single entry by term.

This helper supports optional redirect following and an optional
case-insensitive retry (lowercasing the query) when configured.

Returns Some(LookupResult) on a match, or None if not found."#]
            fn find_entry<'a>(
                &'a self,
                follow: &bool,
                insensitive: &bool,
                query: &str,
                directed_from: Option<&'a $ret>,
                path: &mut Vec<String>,
            ) -> crate::Result<$opt<LookupResult<&'a $ret>>> {
                // Check for redirect loop
                if path.contains(&query.to_string()) {
                    // Build the loop chain from the path in order
                    let mut chain = path.clone();
                    chain.push(query.to_string());
                    return Err(crate::Error::RedirectLoop(chain.join(" -> ")));
                }

                // Add current query to path
                path.push(query.to_string());

                // Try exact match first
                if let Some(entry) = self.entries.get(query) {
                    // Follow an alias if follow is true, entry has no etymologies, and see_also exists
                    if *follow && entry.etymologies.is_empty() {
                        if let Option::Some(also) = &entry.see_also.as_ref() {
                            if also.len() > 0 {
                                // Recursively follow the redirect
                                let result = self.find_entry(
                                    follow,
                                    insensitive,
                                    also,
                                    directed_from.or(Some(entry)),
                                    path,
                                );

                                path.pop();

                                return result;
                            }
                        }
                    }

                    path.pop();

                    return Ok($opt::Some(LookupResult {
                        entry,
                        directed_from,
                    }));
                }

                // If insensitive flag is true and exact match failed, try with lowercase
                if *insensitive {
                    let query_lower = query.to_lowercase();

                    // Only try lowercase if it's different from the original query
                    if query_lower != query {
                        if let Ok($opt::Some(result)) =
                            self.find_entry(follow, insensitive, &query_lower, directed_from, path)
                        {
                            path.pop();
                            return Ok($opt::Some(result));
                        }
                    }
                }

                // Remove from path since we're not following any redirects
                path.pop();

                Ok($opt::None)
            }

            fn perform_split<'a>(
                &'a self,
                query: &str,
                options: &crate::split::SplitOptions,
            ) -> crate::Result<Vec<LookupResult<&'a $ret>>> {
                let crate::split::SplitOptions {
                    threshold,
                    follow,
                    insensitive,
                } = options;

                let chars: Vec<_> = query.chars().collect();
                let mut results: Vec<LookupResult<&'a $ret>> = Vec::new();
                let mut start = 0;
                let mut end = chars.len();

                while start < end {
                    let substr: String = chars[start..end].iter().collect();
                    let mut path = Vec::new();

                    match self.find_entry(follow, insensitive, substr.as_str(), None, &mut path) {
                        Ok($opt::Some(result)) => {
                            results.push(result);
                            start = end;
                            end = chars.len();
                        }
                        Ok($opt::None) => {
                            if end - start <= *threshold {
                                start = end;
                                end = chars.len();
                            } else {
                                end -= 1;
                            }
                        }
                        Err(e) => return Err(e),
                    }
                }

                Ok(results)
            }

            #[doc = r#"Perform lookup for a single query using the provided options.

Depending on the strategy, this may return zero or more results."#]
            fn perform_lookup<'a, Options>(
                &'a self,
                query: &str,
                options: Options,
            ) -> crate::Result<Vec<LookupResult<&'a $ret>>>
            where
                Options: AsRef<LookupOptions> + Send + Sync,
            {
                let LookupOptions {
                    follow,
                    strategy,
                    insensitive,
                } = options.as_ref();

                let mut path = Vec::new();

                if let $opt::Some(result) =
                    self.find_entry(follow, insensitive, query, None, &mut path)?
                {
                    return Ok(vec![result]);
                }

                if let LookupStrategy::Split(min_length) = strategy {
                    let split_opts = crate::split::SplitOptions::default()
                        .threshold(*min_length)
                        .follow(*follow)
                        .insensitive(*insensitive);

                    return self.perform_split(query, &split_opts);
                }

                Ok(vec![])
            }

            pub fn split<'a, Query, Options>(
                &'a self,
                queries: &Vec<Query>,
                options: Options,
            ) -> crate::Result<Vec<LookupResult<&'a $ret>>>
            where
                Query: AsRef<str> + Send + Sync,
                Options: AsRef<crate::split::SplitOptions> + Send + Sync,
            {
                queries
                    .par_iter()
                    .map(|q| self.perform_split(q.as_ref(), options.as_ref()))
                    .collect::<crate::Result<Vec<_>>>()
                    .map(|v| v.into_iter().flatten().collect())
            }

            #[doc = r#"Lookup multiple queries in parallel.

Each query is processed independently with the provided options.

Returns all matches without a guaranteed order.

Examples
--------
```rust
use odict::{OpenDictionary, LookupOptions, LookupStrategy};
# fn demo(dict: &odict::OpenDictionary) -> odict::Result<()> {
let archived = dict.contents()?;
let queries = vec!["hello", "world"];
let options = LookupOptions::default()
    .insensitive(true)
    .strategy(LookupStrategy::Exact);
let results = archived.lookup(&queries, options)?;
# Ok(())
# }
```"#]
            pub fn lookup<'a, 'b, Query, Options>(
                &'a self,
                queries: &'b Vec<Query>,
                options: Options,
            ) -> crate::Result<Vec<LookupResult<&'a $ret>>>
            where
                Query: AsRef<str> + Send + Sync,
                Options: AsRef<LookupOptions> + Send + Sync,
            {
                let results = queries
                    .par_iter()
                    .map(|query| self.perform_lookup(query.as_ref(), &options))
                    .collect::<crate::Result<Vec<_>>>()?
                    .into_iter()
                    .flatten()
                    .collect::<Vec<LookupResult<&'a $ret>>>();

                Ok(results)
            }
        }
    };
}

lookup!(Dictionary, Entry, Option);
lookup!(ArchivedDictionary, ArchivedEntry, ArchivedOption);

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::{lookup::LookupOptions, schema::Dictionary};

    #[test]
    fn test_lookup_follow_limit() {
        // Create a dictionary with a chain of redirects: alias1 -> alias2 -> target
        let xml = r#"
        <dictionary>
            <entry term="target">
                <ety>
                    <sense pos="n">
                        <definition value="The final destination" />
                    </sense>
                </ety>
            </entry>
            <entry term="alias2" see="target" />
            <entry term="alias1" see="alias2" />
        </dictionary>
        "#;

        let dict = Dictionary::from_str(xml).unwrap();

        // Test with follow=false (no following)
        let result = dict
            .lookup(&vec!["alias1"], LookupOptions::default().follow(false))
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "alias1");
        assert!(result[0].directed_from.is_none());

        // Test with follow=true (follows until entry with etymologies found)
        // Should follow alias1 -> alias2 -> target and stop at target since it has etymologies
        let result = dict
            .lookup(&vec!["alias1"], LookupOptions::default().follow(true))
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "target");
        assert!(result[0].directed_from.is_some());
        assert_eq!(result[0].directed_from.unwrap().term, "alias1");

        // Test starting from alias2 should also reach target
        let result = dict
            .lookup(&vec!["alias2"], LookupOptions::default().follow(true))
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "target");
        assert!(result[0].directed_from.is_some());
        assert_eq!(result[0].directed_from.unwrap().term, "alias2");
    }

    #[test]
    fn test_lookup_redirect_loop_detection() {
        // Create a dictionary with circular redirects: loop1 -> loop2 -> loop1
        let xml = r#"
        <dictionary>
            <entry term="loop1" see="loop2" />
            <entry term="loop2" see="loop1" />
        </dictionary>
        "#;

        let dict = Dictionary::from_str(xml).unwrap();

        // Test that circular redirects are detected and return an error
        let result = dict.lookup(&vec!["loop1"], LookupOptions::default().follow(true));

        assert!(result.is_err());

        let error_message = result.unwrap_err().to_string();

        assert_eq!(
            error_message,
            "Redirect loop detected: loop1 -> loop2 -> loop1"
        );

        assert!(error_message.contains("loop1"));
        assert!(error_message.contains("loop2"));
    }

    #[test]
    fn test_lookup_redirect_case_insensitive() {
        // Create a dictionary with redirects where case differs in queries
        let xml = r#"
        <dictionary>
            <entry term="target">
                <ety>
                    <sense pos="n">
                        <definition value="The final destination" />
                    </sense>
                </ety>
            </entry>
            <entry term="alias" see="target" />
        </dictionary>
        "#;

        let dict = Dictionary::from_str(xml).unwrap();

        // Test case insensitive redirect following with uppercase query
        let result = dict
            .lookup(
                &vec!["ALIAS"],
                LookupOptions::default().follow(true).insensitive(true),
            )
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "target");
        assert!(result[0].directed_from.is_some());
        assert_eq!(result[0].directed_from.unwrap().term, "alias");

        // Test case insensitive redirect following with mixed case query
        let result = dict
            .lookup(
                &vec!["Alias"],
                LookupOptions::default().follow(true).insensitive(true),
            )
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "target");
        assert!(result[0].directed_from.is_some());
        assert_eq!(result[0].directed_from.unwrap().term, "alias");

        // Test that case sensitive mode doesn't find mismatched case
        let result = dict
            .lookup(
                &vec!["ALIAS"],
                LookupOptions::default().follow(true).insensitive(false),
            )
            .unwrap();

        assert_eq!(result.len(), 0);

        // Test that exact case match works in case sensitive mode
        let result = dict
            .lookup(
                &vec!["alias"],
                LookupOptions::default().follow(true).insensitive(false),
            )
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entry.term, "target");
    }
}