aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/src/asserts.rs
blob: cdd00288cbddd214cc996a2649c8ebe42a0ac13d (plain)
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
use regex::Regex;

lazy_static! {
    pub static ref COW_TYPE: Regex = Regex::new(r"Cow<'[a-z]+,str>").unwrap();
}

pub static NUMBER_TYPES: [&'static str; 36] = [
    "usize", "u8", "u16", "u32", "u64",
    "isize", "i8", "i16", "i32", "i64",
    "f32", "f64",

    "Option<usize>", "Option<u8>", "Option<u16>", "Option<u32>", "Option<u64>",
    "Option<isize>", "Option<i8>", "Option<i16>", "Option<i32>", "Option<i64>",
    "Option<f32>", "Option<f64>",

    "Option<Option<usize>>", "Option<Option<u8>>", "Option<Option<u16>>", "Option<Option<u32>>", "Option<Option<u64>>",
    "Option<Option<isize>>", "Option<Option<i8>>", "Option<Option<i16>>", "Option<Option<i32>>", "Option<Option<i64>>",
    "Option<Option<f32>>", "Option<Option<f64>>",
];


pub fn assert_string_type(name: &str, field_type: &String) {
    if field_type != "String"
        && field_type != "&str"
        && !COW_TYPE.is_match(field_type)
        && field_type != "Option<String>"
        && field_type != "Option<Option<String>>"
        && !(field_type.starts_with("Option<") && field_type.ends_with("str>"))
        && !(field_type.starts_with("Option<Option<") && field_type.ends_with("str>>")) {
        panic!("`{}` validator can only be used on String, &str, Cow<'_,str> or an Option of those", name);
    }
}

pub fn assert_type_matches(field_name: String, field_type: &String, field_type2: Option<&String>) {
    if let Some(t2) = field_type2 {
        if field_type != t2 {
            panic!("Invalid argument for `must_match` validator of field `{}`: types of field can't match", field_name);
        }
    } else {
        panic!("Invalid argument for `must_match` validator of field `{}`: the other field doesn't exist in struct", field_name);
    }
}

pub fn assert_has_len(field_name: String, field_type: &String) {
    if field_type != "String"
        && !field_type.starts_with("Vec<")
        && !field_type.starts_with("Option<Vec<")
        && !field_type.starts_with("Option<Option<Vec<")
        && field_type != "Option<String>"
        && field_type != "Option<Option<String>>"
        // a bit ugly
        && !(field_type.starts_with("Option<") && field_type.ends_with("str>"))
        && !(field_type.starts_with("Option<Option<") && field_type.ends_with("str>>"))
        && !COW_TYPE.is_match(field_type)
        && field_type != "&str" {
            panic!(
                "Validator `length` can only be used on types `String`, `&str`, Cow<'_,str> or `Vec` but found `{}` for field `{}`",
                field_type, field_name
            );
    }
}

pub fn assert_has_range(field_name: String, field_type: &String) {
    if !NUMBER_TYPES.contains(&field_type.as_ref()) {
        panic!(
            "Validator `range` can only be used on number types but found `{}` for field `{}`",
            field_type, field_name
        );
    }
}