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 listed again in a message, at any depth, after `kept`.
9    DuplicateField {
10        tag: Tag,
11        kept: MemberContext,
12        dropped: MemberContext,
13    },
14}
15
16impl fmt::Display for Warning {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Warning::DuplicateField { tag, kept, dropped } if kept == dropped => {
20                write!(
21                    f,
22                    "tag {tag} is dropped from {dropped}, which lists it more than once"
23                )
24            }
25            Warning::DuplicateField { tag, kept, dropped } => {
26                write!(
27                    f,
28                    "tag {tag} in {dropped} is dropped, as it already appears in {kept}"
29                )
30            }
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn a_duplicate_in_one_place_reads_as_listed_more_than_once() {
41        let context = MemberContext::Component("Top".to_owned());
42        let warning = Warning::DuplicateField {
43            tag: Tag(58),
44            kept: context.clone(),
45            dropped: context,
46        };
47
48        assert_eq!(
49            warning.to_string(),
50            "tag 58 is dropped from component 'Top', which lists it more than once"
51        );
52    }
53
54    #[test]
55    fn a_duplicate_in_two_places_names_both() {
56        let message = MemberContext::Message("NewOrderSingle".to_owned());
57        let warning = Warning::DuplicateField {
58            tag: Tag(448),
59            kept: message.clone(),
60            dropped: message.group("NoPartyIDs"),
61        };
62
63        assert_eq!(
64            warning.to_string(),
65            "tag 448 in group 'NoPartyIDs' in message 'NewOrderSingle' is dropped, as it already \
66             appears in message 'NewOrderSingle'"
67        );
68    }
69}