Skip to main content

refix_codegen/
warning.rs

1use refix_dictionary::{DataType, MemberContext};
2
3use crate::NameClash;
4
5/// Where the generated code departs from the dictionary.
6#[derive(Debug, Eq, PartialEq)]
7pub enum Warning {
8    /// A construct the generator recognised but does not generate code for yet.
9    UnsupportedGroup { context: MemberContext },
10    /// The second owner of a clashing name, given `name` in its place.
11    Renamed { clash: Box<NameClash>, name: String },
12    /// A field read as raw bytes, as the generator does not interpret its type.
13    UnclaimedType { field: String, data_type: DataType },
14}
15
16impl std::fmt::Display for Warning {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Warning::UnsupportedGroup { context } => {
20                write!(f, "{context} is not generated yet")
21            }
22            Warning::Renamed { clash, name } => write!(
23                f,
24                "`{}` is taken by {}, so {} is named `{name}`",
25                clash.name, clash.first, clash.second
26            ),
27            Warning::UnclaimedType { field, data_type } => write!(
28                f,
29                "field '{field}' has type {data_type}, which is not interpreted, so it reads as \
30                 raw bytes"
31            ),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::Warning;
39    use crate::{NameClash, Owner};
40    use refix_dictionary::DataType;
41
42    #[test]
43    fn a_rename_names_both_owners_and_the_new_name() {
44        let warning = Warning::Renamed {
45            clash: Box::new(NameClash {
46                name: "SecurityStatus".to_owned(),
47                first: Owner::Message("SecurityStatus".to_owned()),
48                second: Owner::Enum("SecurityStatus".to_owned()),
49            }),
50            name: "SecurityStatusEnum".to_owned(),
51        };
52        assert_eq!(
53            warning.to_string(),
54            "`SecurityStatus` is taken by message 'SecurityStatus', so the enum of field \
55             'SecurityStatus' is named `SecurityStatusEnum`"
56        );
57    }
58
59    #[test]
60    fn an_unclaimed_type_names_the_field_and_its_type() {
61        let warning = Warning::UnclaimedType {
62            field: "RatioQty".to_owned(),
63            data_type: DataType::Other("QUANTITY".to_owned()),
64        };
65        assert_eq!(
66            warning.to_string(),
67            "field 'RatioQty' has type QUANTITY, which is not interpreted, so it reads as raw bytes"
68        );
69    }
70}