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
use std::io;

/// An extension to types that implement [`Serialize`](serde::Serialize)
pub trait SerializeExt {
    /// Create a struct that implements [`Display`] that will
    /// create a json string from the object using its implementation
    /// of [`Serialize`]
    ///
    /// [`Display`]: std::fmt::Display
    /// [`Serialize`]: serde::Serialize
    fn json(&self) -> Jsonify<'_, Self>;
}

impl<T> SerializeExt for T
where
    T: serde::Serialize,
{
    fn json(&self) -> Jsonify<'_, Self> {
        Jsonify(self)
    }
}

pub struct Jsonify<'a, T: ?Sized>(&'a T);

impl<T> std::fmt::Display for Jsonify<'_, T>
where
    T: ?Sized + serde::Serialize,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        serde_json::to_writer(FmtWriter(f), self.0).map_err(|_| std::fmt::Error)
    }
}

#[cfg_attr(not(feature = "with-json"), allow(dead_code))]
struct FmtWriter<'a, 'b>(&'a mut std::fmt::Formatter<'b>);

impl<'a, 'b> std::io::Write for FmtWriter<'a, 'b> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let s = String::from_utf8_lossy(buf);
        self.0
            .write_str(&s)
            .map(|_| buf.len())
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "unable to format"))
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}