| 1 |
#![deny(unreachable_code)] |
| 2 |
|
| 3 |
use { |
| 4 |
lazy_regex::*, |
| 5 |
std::num::ParseIntError, |
| 6 |
}; |
| 7 |
|
| 8 |
#[test] |
| 9 |
fn test_regex_switch() { |
| 10 |
#[derive(Debug, PartialEq, Eq)] |
| 11 |
enum Color { |
| 12 |
Grey(u8), |
| 13 |
Pink, |
| 14 |
Rgb(u8, u8, u8), |
| 15 |
} |
| 16 |
fn read(s: &str) -> Option<Color> { |
| 17 |
regex_switch!(s, |
| 18 |
r"^gr(a|e)y\((?<level>\d{1,2})\)$"i => { |
| 19 |
Color::Grey(level.parse().unwrap()) |
| 20 |
} |
| 21 |
"^pink"i => Color::Pink, |
| 22 |
r"^rgb\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+),?\)$"i => Color::Rgb ( |
| 23 |
r.parse().unwrap(), |
| 24 |
g.parse().unwrap(), |
| 25 |
b.parse().unwrap(), |
| 26 |
), |
| 27 |
) |
| 28 |
} |
| 29 |
assert_eq!(read("gray(15)"), Some(Color::Grey(15))); |
| 30 |
assert_eq!(read("pInk"), Some(Color::Pink)); |
| 31 |
assert_eq!(read("pinkie"), Some(Color::Pink)); |
| 32 |
assert_eq!(read("red"), None); |
| 33 |
assert_eq!(read("rgb(1,2,3)"), Some(Color::Rgb(1, 2, 3))); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn test_regex_switch_with_error_handling() -> Result<(), ParseIntError> { |
| 38 |
#[derive(Debug, PartialEq)] |
| 39 |
enum Color { |
| 40 |
Grey(u8), |
| 41 |
Pink, |
| 42 |
Rgb(u8, u8, u8), |
| 43 |
} |
| 44 |
let input = "RGB(1, 2, 3)"; |
| 45 |
let color = regex_switch!(input, |
| 46 |
r#"^gr(a|e)y\((?<level>\d{1,2})\)$"#i => { |
| 47 |
Color::Grey(level.parse()?) |
| 48 |
} |
| 49 |
"^pink"i => Color::Pink, |
| 50 |
r#"^rgb\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+),?\)$"#i => Color::Rgb ( |
| 51 |
r.parse()?, |
| 52 |
g.parse()?, |
| 53 |
b.parse()?, |
| 54 |
), |
| 55 |
); |
| 56 |
assert_eq!(color, Some(Color::Rgb(1, 2, 3))); |
| 57 |
Ok(()) |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
#[cfg(not(feature = "lite"))] |
| 62 |
fn test_bytes_regex_switch() { |
| 63 |
#[derive(Debug, PartialEq, Eq)] |
| 64 |
enum Color { |
| 65 |
Grey(u8), |
| 66 |
Pink, |
| 67 |
Rgb(u8, u8, u8), |
| 68 |
} |
| 69 |
fn read(s: &[u8]) -> Option<Color> { |
| 70 |
lazy_regex::bytes_regex_switch!(s, |
| 71 |
r#"^gr(a|e)y\((?<level>\d{1,2})\)$"#i => { |
| 72 |
Color::Grey(std::str::from_utf8(level).unwrap().parse().unwrap()) |
| 73 |
} |
| 74 |
"^pink"i => Color::Pink, |
| 75 |
r#"^rgb\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+),?\)$"#i => Color::Rgb ( |
| 76 |
std::str::from_utf8(r).unwrap().parse().unwrap(), |
| 77 |
std::str::from_utf8(g).unwrap().parse().unwrap(), |
| 78 |
std::str::from_utf8(b).unwrap().parse().unwrap(), |
| 79 |
), |
| 80 |
) |
| 81 |
} |
| 82 |
assert_eq!(read(b"gray(15)"), Some(Color::Grey(15))); |
| 83 |
assert_eq!(read(b"pInk"), Some(Color::Pink)); |
| 84 |
assert_eq!(read(b"pinkie"), Some(Color::Pink)); |
| 85 |
assert_eq!(read(b"red"), None); |
| 86 |
assert_eq!(read(b"rgb(1,2,3)"), Some(Color::Rgb(1, 2, 3))); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn test_regex_switch_with_early_return() { |
| 91 |
#[allow(clippy::diverging_sub_expression)] |
| 92 |
fn read(s: &str) -> Result<Option<&'static str>, ()> { |
| 93 |
Ok(regex_switch!(s, |
| 94 |
"^error$" => return Err(()), |
| 95 |
"^ok$" => "ok", |
| 96 |
)) |
| 97 |
} |
| 98 |
|
| 99 |
assert_eq!(read("error"), Err(())); |
| 100 |
assert_eq!(read("ok"), Ok(Some("ok"))); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
#[cfg(not(feature = "lite"))] |
| 105 |
fn test_bytes_regex_switch_with_early_return() { |
| 106 |
#[allow(clippy::diverging_sub_expression)] |
| 107 |
fn read(s: &[u8]) -> Result<Option<&'static [u8]>, ()> { |
| 108 |
Ok(bytes_regex_switch!(s, |
| 109 |
"^error$" => return Err(()), |
| 110 |
"^ok$" => &b"ok"[..], |
| 111 |
)) |
| 112 |
} |
| 113 |
|
| 114 |
assert_eq!(read(b"error"), Err(())); |
| 115 |
assert_eq!(read(b"ok"), Ok(Some(&b"ok"[..]))); |
| 116 |
} |