record_query/value/
cbor.rs

1use crate::error;
2
3use crate::value;
4use serde;
5use serde_cbor;
6use std::fmt;
7use std::io;
8
9pub struct Source<R>(serde_cbor::de::Deserializer<serde_cbor::de::IoRead<R>>)
10where
11    R: io::Read;
12
13pub struct Sink<W>(serde_cbor::ser::Serializer<serde_cbor::ser::IoWrite<W>>)
14where
15    W: io::Write;
16
17#[inline]
18pub fn source<R>(r: R) -> Source<R>
19where
20    R: io::Read,
21{
22    Source(serde_cbor::de::Deserializer::new(
23        serde_cbor::de::IoRead::new(r),
24    ))
25}
26
27#[inline]
28pub fn sink<W>(w: W) -> Sink<W>
29where
30    W: io::Write,
31{
32    Sink(serde_cbor::ser::Serializer::new(
33        serde_cbor::ser::IoWrite::new(w),
34    ))
35}
36
37impl<R> value::Source for Source<R>
38where
39    R: io::Read,
40{
41    #[inline]
42    fn read(&mut self) -> error::Result<Option<value::Value>> {
43        match serde::Deserialize::deserialize(&mut self.0) {
44            Ok(v) => Ok(Some(v)),
45            Err(e) => match e.classify() {
46                serde_cbor::error::Category::Eof => Ok(None),
47                _ => Err(error::Error::from(e)),
48            },
49        }
50    }
51}
52
53impl<W> value::Sink for Sink<W>
54where
55    W: io::Write,
56{
57    #[inline]
58    fn write(&mut self, v: value::Value) -> error::Result<()> {
59        serde::Serialize::serialize(&v, &mut self.0).map_err(From::from)
60    }
61}
62
63impl<R> fmt::Debug for Source<R>
64where
65    R: io::Read,
66{
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        f.debug_struct("CborSource").finish()
69    }
70}
71
72impl<W> fmt::Debug for Sink<W>
73where
74    W: io::Write,
75{
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        f.debug_struct("CborSink").finish()
78    }
79}