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
198
199
use sval::stream::{
    self,
    Stream,
};

use crate::std::{
    error::Error,
    string::String,
};
use crate::{
    fmt::Formatter,
    std::{
        fmt,
        io::Write,
    },
    End,
};

impl<T> Error for End<T> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.err.source()
    }
}

/**
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(FmtToIo(writer), v)
}

struct FmtToIo<W>(W);

impl<W> fmt::Write for FmtToIo<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(())
    }
}

/**
A stream for writing structured data as json.

The stream internally wraps a [`std::io::Write`].

# Examples

Create an owned json stream:

```
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# use std::str;
use sval::stream::OwnedStream;
use sval_json::Writer;

let mut stream = OwnedStream::new(Writer::new(Vec::<u8>::new()));
stream.any(42)?;
let json = stream.into_inner().end()?;

assert_eq!(Some("42"), str::from_utf8(&json).ok());
# Ok(())
# }
```
*/
pub struct Writer<W>(Formatter<FmtToIo<W>>);

impl<W> Writer<W>
where
    W: Write,
{
    /**
    Create a new json stream.
    */
    pub fn new(out: W) -> Self {
        Writer(Formatter::new(FmtToIo(out)))
    }

    /**
    Whether the stream has seen a complete, valid json structure.
    */
    pub fn is_valid(&self) -> bool {
        self.0.is_valid()
    }

    /**
    Complete the stream and return the inner writer.

    If the writer contains incomplete json then this method will fail.
    The returned error can be used to pull the original stream back out.
    */
    pub fn end(self) -> Result<W, End<Self>> {
        match self.0.end() {
            Ok(w) => Ok(w.0),
            Err(End { err, stream, .. }) => Err(End::new(err, Writer(stream))),
        }
    }

    /**
    Get the inner writer back out of the stream without ensuring it's valid.
    */
    pub fn into_inner_unchecked(self) -> W {
        self.0.into_inner_unchecked().0
    }
}

impl<W> Stream for Writer<W>
where
    W: Write,
{
    #[inline]
    fn fmt(&mut self, v: stream::Arguments) -> stream::Result {
        self.0.fmt(v)
    }

    #[inline]
    fn i64(&mut self, v: i64) -> stream::Result {
        self.0.i64(v)
    }

    #[inline]
    fn u64(&mut self, v: u64) -> stream::Result {
        self.0.u64(v)
    }

    #[inline]
    fn f64(&mut self, v: f64) -> stream::Result {
        self.0.f64(v)
    }

    #[inline]
    fn bool(&mut self, v: bool) -> stream::Result {
        self.0.bool(v)
    }

    #[inline]
    fn char(&mut self, v: char) -> stream::Result {
        self.0.char(v)
    }

    #[inline]
    fn str(&mut self, v: &str) -> stream::Result {
        self.0.str(v)
    }

    #[inline]
    fn none(&mut self) -> stream::Result {
        self.0.none()
    }

    #[inline]
    fn seq_begin(&mut self, len: Option<usize>) -> stream::Result {
        self.0.seq_begin(len)
    }

    #[inline]
    fn seq_elem(&mut self) -> stream::Result {
        self.0.seq_elem()
    }

    #[inline]
    fn seq_end(&mut self) -> stream::Result {
        self.0.seq_end()
    }

    #[inline]
    fn map_begin(&mut self, len: Option<usize>) -> stream::Result {
        self.0.map_begin(len)
    }

    #[inline]
    fn map_key(&mut self) -> stream::Result {
        self.0.map_key()
    }

    #[inline]
    fn map_value(&mut self) -> stream::Result {
        self.0.map_value()
    }

    #[inline]
    fn map_end(&mut self) -> stream::Result {
        self.0.map_end()
    }
}