Skip to main content

rust_yaml/serde_integration/
error.rs

1//! Bridge between rust-yaml's `Error` type and serde's error traits.
2
3use crate::{Error, Position};
4use std::fmt::Display;
5
6impl serde::ser::Error for Error {
7    fn custom<T: Display>(msg: T) -> Self {
8        Error::construction(Position::new(), msg.to_string())
9    }
10}
11
12impl serde::de::Error for Error {
13    fn custom<T: Display>(msg: T) -> Self {
14        Error::construction(Position::new(), msg.to_string())
15    }
16}
17
18#[cfg(test)]
19mod tests {
20    use crate::Error;
21    use serde::{de::Error as DeError, ser::Error as SerError};
22
23    #[test]
24    fn ser_error_custom_uses_construction_variant() {
25        let e: Error = SerError::custom("boom");
26        assert!(e.to_string().contains("boom"));
27    }
28
29    #[test]
30    fn de_error_custom_uses_construction_variant() {
31        let e: Error = DeError::custom("boom");
32        assert!(e.to_string().contains("boom"));
33    }
34}