snitch_detective/
lib.rs

1#![feature(test)]
2use crate::error::CustomError;
3use gjson::Value;
4
5pub mod detective;
6pub mod error;
7pub mod matcher_core;
8pub mod matcher_numeric;
9pub mod matcher_pii;
10
11#[cfg(test)]
12#[path = "matcher_numeric_tests.rs"]
13mod matcher_numeric_tests;
14
15#[cfg(test)]
16#[path = "matcher_core_tests.rs"]
17mod matcher_core_tests;
18
19#[cfg(test)]
20#[path = "matcher_pii_tests.rs"]
21mod matcher_pii_tests;
22
23#[cfg(test)]
24#[path = "test_utils.rs"]
25mod test_utils;
26
27#[cfg(test)]
28#[path = "test_bench.rs"]
29mod test_bench;
30
31pub trait FromValue<'a>
32where
33    Self: Sized,
34{
35    fn from_value(value: Value<'a>) -> Result<Self, CustomError>;
36}
37
38impl FromValue<'_> for bool {
39    fn from_value(value: Value) -> Result<Self, CustomError> {
40        if value.kind() != gjson::Kind::True && value.kind() != gjson::Kind::False {
41            return Err(CustomError::Error("not a boolean".to_string()));
42        }
43
44        Ok(value.bool())
45    }
46}
47
48impl FromValue<'_> for f64 {
49    fn from_value(value: Value) -> Result<Self, CustomError> {
50        if value.kind() != gjson::Kind::Number {
51            return Err(CustomError::Error("not a number".to_string()));
52        }
53
54        Ok(value.f64())
55    }
56}
57
58impl<'a> FromValue<'a> for Value<'a> {
59    fn from_value(value: Value<'a>) -> Result<Value<'a>, CustomError> {
60        Ok(value)
61    }
62}
63
64impl FromValue<'_> for String {
65    fn from_value(value: Value) -> Result<Self, CustomError> {
66        Ok(value.to_string())
67    }
68}