File: util.rs

package info (click to toggle)
rust-garde 0.22.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,619 bytes parent folder | download | duplicates (2)
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
#![allow(dead_code)]

use std::fmt::{Debug, Write};

use garde::Validate;
use owo_colors::OwoColorize;

pub fn check_ok<T: Validate + Debug>(cases: &[T], ctx: &T::Context) {
    let mut some_failed = false;
    for case in cases {
        if let Err(report) = case.validate_with(ctx) {
            eprintln!(
                "{} input: {case:?}, errors: [{}]",
                "FAIL".red(),
                report
                    .to_string()
                    .split('\n')
                    .collect::<Vec<_>>()
                    .join("; ")
            );
            some_failed = true;
        }
    }

    if some_failed {
        panic!("some cases failed, see error output");
    }
}

#[doc(hidden)]
pub fn __check_fail<T: Validate + Debug>(cases: &[T], ctx: &T::Context) -> String {
    let mut some_success = false;
    let mut snapshot = String::new();
    for case in cases {
        if let Err(report) = case.validate_with(ctx) {
            writeln!(&mut snapshot, "{case:#?}").unwrap();
            write!(&mut snapshot, "{report}").unwrap();
            writeln!(&mut snapshot).unwrap();
        } else {
            eprintln!("{} input: {case:?}", "SUCCESS".red());
            some_success = true;
        }
    }

    if some_success {
        panic!("some cases did not fail, see error output");
    }

    snapshot
}

#[doc(hidden)]
#[macro_export]
macro_rules! __check_fail {
    ($input:expr, $ctx:expr $(,)?) => {{
        let snapshot = $crate::rules::util::__check_fail($input, $ctx);
        ::insta::assert_snapshot!(snapshot);
    }};
}

pub use crate::__check_fail as check_fail;