Skip to main content

refix_codegen/
warning.rs

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