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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use derive_more::Display;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// Generic Event reason information structure.
#[derive(Clone, Display, Debug, PartialEq, Serialize, Deserialize)]
#[display(fmt = "'{}'", message)]
pub struct Reason {
    pub message: String,
    #[serde(flatten)]
    pub extra: serde_json::Value,
}

impl Reason {
    /// Generic Event reason information structure.
    pub fn new(message: impl ToString) -> Reason {
        Reason {
            message: message.to_string(),
            extra: serde_json::json!({}),
        }
    }
}

impl<T: Into<String>> From<T> for Reason {
    fn from(m: T) -> Self {
        Reason::new(m.into())
    }
}

#[derive(thiserror::Error, Clone, Debug, PartialEq)]
#[error("Error converting `{0}` to Reason: {1}")]
pub struct ReasonConversionError(String, String);

impl Reason {
    pub fn from_value<T: Serialize + Debug>(value: &T) -> Result<Self, ReasonConversionError> {
        serde_json::to_value(value)
            .and_then(serde_json::from_value)
            .map_err(|e| ReasonConversionError(format!("{:?}", value), e.to_string()))
    }

    pub fn to_value<T: DeserializeOwned>(&self) -> Result<T, ReasonConversionError> {
        serde_json::to_value(self)
            .and_then(serde_json::from_value)
            .map_err(|e| ReasonConversionError(format!("{:?}", self), e.to_string()))
    }
}

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

    #[test]
    fn test_try_convert_self() {
        let reason = Reason::new("coś");
        assert_eq!(reason, Reason::from_value(&reason).unwrap());
    }

    #[test]
    // check if message field in extra will not overwrite top-level message
    fn test_try_convert_self_with_extra_message() {
        let reason = Reason {
            message: "coś".to_string(),
            extra: serde_json::json!({"ala":"ma kota","message": "coś innego"}),
        };
        assert_eq!(
            Reason {
                message: "coś innego".to_string(),
                extra: serde_json::json!({"ala":"ma kota"}),
            },
            Reason::from_value(&reason).unwrap()
        );

        assert_ne!(
            reason,
            Reason::from_value(&reason).unwrap().to_value().unwrap()
        )
    }

    #[test]
    fn test_try_convert_custom_reason_wo_message_field() {
        #[derive(Serialize, Deserialize, Debug)]
        struct CustomReason {}
        let custom = CustomReason {};

        assert_eq!(
            "Error converting `CustomReason` to Reason: missing field `message`",
            &Reason::from_value(&custom).unwrap_err().to_string()
        )
    }

    #[test]
    fn test_try_convert_custom_reason_with_message_field() {
        #[derive(Serialize, Deserialize, Debug, PartialEq)]
        struct CustomReason {
            message: String,
            other: bool,
        }
        let custom = CustomReason {
            message: "coś".to_string(),
            other: false,
        };

        assert_eq!(
            Reason {
                message: "coś".to_string(),
                extra: serde_json::json!({ "other": false }),
            },
            Reason::from_value(&custom).unwrap()
        );

        assert_eq!(
            custom,
            Reason::from_value(&custom).unwrap().to_value().unwrap()
        )
    }

    #[test]
    fn test_try_convert_custom_reason_wrong_message_type() {
        #[derive(Serialize, Deserialize, Debug)]
        struct CustomReason {
            message: u8,
        }
        let custom = CustomReason { message: 37 };

        assert_eq!(
            "Error converting `CustomReason { message: 37 }` to Reason: invalid \
            type: integer `37`, expected a string",
            &Reason::from_value(&custom).unwrap_err().to_string()
        )
    }

    #[test]
    fn test_try_convert_custom_reason_wrong_fancy_message_type() {
        #[derive(Serialize, Deserialize, Debug)]
        struct CustomReason {
            i: u8,
            b: bool,
            s: String,
            #[serde(flatten)]
            extra: serde_json::Value,
        }

        let custom = CustomReason {
            i: 0,
            b: false,
            s: "".to_string(),
            extra: serde_json::json!({"message": true}),
        };

        assert_eq!(
            format!(
                "Error converting `{:?}` to Reason: invalid type: \
                boolean `true`, expected a string",
                custom
            ),
            Reason::from_value(&custom).unwrap_err().to_string()
        )
    }

    #[test]
    fn test_try_convert_custom_reason_with_fancy_message() {
        #[derive(Serialize, Deserialize, Debug)]
        struct CustomReason {
            i: u8,
            b: bool,
            s: String,
            #[serde(flatten)]
            extra: serde_json::Value,
        }

        let custom = CustomReason {
            i: 0,
            b: false,
            s: "".to_string(),
            extra: serde_json::json!({"message": "coś"}),
        };

        assert_eq!(
            Reason {
                message: "coś".to_string(),
                extra: serde_json::json!({ "i": 0, "b": false, "s": "" }),
            },
            Reason::from_value(&custom).unwrap()
        )
    }

    #[test]
    fn test_try_convert_json_reason_with_message() {
        let json_reason = serde_json::json!({"message": "coś"});

        assert_eq!(
            Reason::new("coś"),
            Reason::from_value(&json_reason).unwrap()
        )
    }
}