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
use super::InvalidValue;
use super::Value;
use crate::var::{Var, VarId};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-support", derive(serde::Serialize))]
pub struct ValidVal {
val: Box<dyn Value>,
validated_by: VarId,
}
impl ValidVal {
pub fn try_new(val: Box<dyn Value>, validate_with: &Box<dyn Var + Send + Sync>) -> Result<Self, InvalidValue> {
match validate_with.validate_val_type(&val) {
Ok(_) => Ok(Self {
val: val,
validated_by: validate_with.id().clone()
}),
Err(e) => Err(e),
}
}
pub fn get_val(&self) -> &Box<dyn Value> {
&self.val
}
}
impl PartialEq for ValidVal {
fn eq(&self, other: &Self) -> bool {
self.val.eq_box(&other.val) && self.validated_by == other.validated_by
}
}
#[cfg(test)]
mod tests {
use stepflow_test_util::test_id;
use crate::{var::{Var, VarId, EmailVar, StringVar}, value::{EmailValue, StringValue}};
use super::ValidVal;
#[test]
fn partial_eq() {
const EMAIL1: &str = "a@b.com";
const EMAIL2: &str = "ab@bc.com";
let email_var: Box<dyn Var + Send + Sync + 'static> = Box::new(EmailVar::new(test_id!(VarId)));
let email_val1 = EmailValue::try_new(EMAIL1).unwrap();
let valid_email = ValidVal::try_new(Box::new(email_val1.clone()), &email_var).unwrap();
let valid_email_same = ValidVal::try_new(Box::new(email_val1), &email_var).unwrap();
let email_val2 = EmailValue::try_new(EMAIL2).unwrap();
let valid_email_different = ValidVal::try_new(Box::new(email_val2), &email_var).unwrap();
let string_var: Box<dyn Var + Send + Sync + 'static> = Box::new(StringVar::new(test_id!(VarId)));
let string_val = StringValue::try_new(EMAIL1).unwrap();
let valid_string = ValidVal::try_new(Box::new(string_val), &string_var).unwrap();
assert_eq!(valid_email, valid_email_same);
assert_ne!(valid_email, valid_email_different);
assert_ne!(valid_email, valid_string);
}
}