Skip to main content

refix_dictionary/dictionary/
warning.rs

1use crate::{MemberContext, Tag};
2use std::fmt;
3
4/// Something the resolver dropped from a [`crate::Spec`] to keep the
5/// [`crate::Dictionary`] sound.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum Warning {
8    /// A tag that one list names twice.
9    ///
10    /// A tag of the header or trailer that a message names itself is one
11    /// as well.
12    DuplicateField {
13        tag: Tag,
14        kept: MemberContext,
15        dropped: MemberContext,
16    },
17}
18
19impl fmt::Display for Warning {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Warning::DuplicateField { tag, kept, dropped } if kept == dropped => {
23                write!(
24                    f,
25                    "tag {tag} is dropped from {dropped}, which lists it more than once"
26                )
27            }
28            Warning::DuplicateField { tag, kept, dropped } => {
29                write!(
30                    f,
31                    "tag {tag} in {dropped} is dropped, as it already appears in {kept}"
32                )
33            }
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn a_duplicate_in_one_place_reads_as_listed_more_than_once() {
44        let context = MemberContext::Component("Top".to_owned());
45        let warning = Warning::DuplicateField {
46            tag: Tag(58),
47            kept: context.clone(),
48            dropped: context,
49        };
50
51        assert_eq!(
52            warning.to_string(),
53            "tag 58 is dropped from component 'Top', which lists it more than once"
54        );
55    }
56
57    #[test]
58    fn a_duplicate_in_two_places_names_both() {
59        let warning = Warning::DuplicateField {
60            tag: Tag(49),
61            kept: MemberContext::Header,
62            dropped: MemberContext::Message("NewOrderSingle".to_owned()),
63        };
64
65        assert_eq!(
66            warning.to_string(),
67            "tag 49 in message 'NewOrderSingle' is dropped, as it already appears in the header"
68        );
69    }
70}