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
use crate::std::string::String;
use crate::std::{
    fmt,
    io::Write,
};

/**
Write a [`sval::Value`] to a string.
*/
pub fn to_string(v: impl sval::Value) -> Result<String, sval::Error> {
    let mut out = String::new();

    crate::to_fmt(&mut out, v)?;

    Ok(out)
}

/**
Write a [`sval::Value`] to a writer.
*/
pub fn to_writer(writer: impl Write, v: impl sval::Value) -> Result<(), sval::Error> {
    crate::to_fmt(Writer(writer), v)
}

struct Writer<W>(W);

impl<W> fmt::Write for Writer<W>
where
    W: Write,
{
    fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
        self.0.write(s.as_bytes()).map_err(|_| fmt::Error)?;

        Ok(())
    }
}