Skip to main content

squeeze/
mirror.rs

1//! Mirror finder - a passthrough finder for debugging.
2//!
3//! The [`Mirror`] finder simply returns the entire input as a match.
4//! This is useful for debugging or when you want to process all input lines.
5
6use super::Finder;
7use std::ops::Range;
8
9/// A passthrough finder that returns the entire input.
10///
11/// This finder always matches and returns the full input string.
12/// Useful for debugging or as a fallback.
13///
14/// # Example
15///
16/// ```
17/// use squeeze::{mirror::Mirror, Finder};
18///
19/// let finder = Mirror::default();
20/// let text = "hello world";
21///
22/// let range = finder.find(text).unwrap();
23/// assert_eq!(&text[range], "hello world");
24/// ```
25#[derive(Default)]
26pub struct Mirror {}
27
28impl Finder for Mirror {
29    fn id(&self) -> &'static str {
30        "mirror"
31    }
32
33    fn find(&self, s: &str) -> Option<Range<usize>> {
34        Some(0..s.len())
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn id_should_return_mirror() {
44        let finder = Mirror::default();
45        assert_eq!("mirror", finder.id());
46    }
47
48    #[test]
49    fn find_should_return_full_range_for_non_empty_string() {
50        let finder = Mirror::default();
51        let input = "hello world";
52        assert_eq!(Some(0..11), finder.find(input));
53    }
54
55    #[test]
56    fn find_should_return_empty_range_for_empty_string() {
57        let finder = Mirror::default();
58        assert_eq!(Some(0..0), finder.find(""));
59    }
60
61    #[test]
62    fn find_should_return_full_range_for_unicode() {
63        let finder = Mirror::default();
64        let input = "héllo 世界 🦀";
65        assert_eq!(Some(0..input.len()), finder.find(input));
66    }
67
68    #[test]
69    fn find_should_return_full_range_for_whitespace() {
70        let finder = Mirror::default();
71        assert_eq!(Some(0..3), finder.find("   "));
72        assert_eq!(Some(0..2), finder.find("\t\n"));
73    }
74}