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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::message::Message;

/// Feedback struct contains target field name and multiple Message objects if
/// the context is negative (otherwise it will be an empty vector).
#[derive(Clone, Debug)]
pub struct Feedback {
    pub field: String,
    pub messages: Vec<Message>,
}

impl Feedback {
    pub fn is_negative(&self) -> bool {
        !self.messages.is_empty()
    }
}

impl PartialEq for Feedback {
    fn eq(&self, other: &Self) -> bool {
        if self.field != other.field {
            return false;
        }
        self.messages == other.messages
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_is_negative() {
        let f = Feedback {
            field: "name".to_string(),
            messages: Vec::new(),
        };
        assert!(!f.is_negative());

        let m = Message {
            text: "lorem ipsum".to_string(),
            args: Vec::new(),
        };
        let f = Feedback {
            field: "name".to_string(),
            messages: vec![m],
        };
        assert!(f.is_negative());
    }

    #[test]
    fn test_eq() {
        let a = Feedback {
            field: "name".to_string(),
            messages: vec![Message {
                text: "lorem ipsum".to_string(),
                args: Vec::new(),
            }],
        };
        assert!(a.eq(&a));

        let b = Feedback {
            field: "name".to_string(),
            messages: vec![Message {
                text: "lorem ipsum".to_string(),
                args: Vec::new(),
            }],
        };
        assert!(a.eq(&b));

        let c = Feedback {
            field: "description".to_string(),
            messages: vec![Message {
                text: "lorem ipsum".to_string(),
                args: Vec::new(),
            }],
        };
        assert!(!a.eq(&c));

        let d = Feedback {
            field: "name".to_string(),
            messages: vec![Message {
                text: "lorem ipsum {0}".to_string(),
                args: vec!["dolor sit amet".to_string()],
            }],
        };
        assert!(!a.eq(&d));
    }
}