/*! This example demonstrates implementing a nested `Stream`, similar to the flat example from `sval`. */ pub struct Stream; impl<'sval> sval_nested::Stream<'sval> for Stream { type Ok = (); type Seq = Self; type Map = Self; type Tuple = Self; type Record = Self; type Enum = Self; fn null(self) -> sval_nested::Result { print!("null"); Ok(()) } fn bool(self, value: bool) -> sval_nested::Result { print!("{}", value); Ok(()) } fn i64(self, value: i64) -> sval_nested::Result { print!("{}", value); Ok(()) } fn f64(self, value: f64) -> sval_nested::Result { print!("{}", value); Ok(()) } fn text_computed(self, text: &str) -> sval_nested::Result { print!("{:?}", text); Ok(()) } fn seq_begin(self, _: Option) -> sval_nested::Result { print!("[ "); Ok(self) } fn map_begin(self, num_entries: Option) -> sval_nested::Result { self.seq_begin(num_entries) } fn tuple_begin( self, _: Option, _: Option, _: Option, num_entries: Option, ) -> sval_nested::Result { self.seq_begin(num_entries) } fn record_begin( self, _: Option, _: Option, _: Option, num_entries: Option, ) -> sval_nested::Result { self.map_begin(num_entries) } fn enum_begin( self, _: Option, _: Option, _: Option, ) -> sval_nested::Result { Ok(self) } } impl<'sval> sval_nested::StreamSeq<'sval> for Stream { type Ok = (); fn value_computed(&mut self, value: V) -> sval_nested::Result { sval_nested::Stream::value_computed(Stream, value)?; print!(", "); Ok(()) } fn end(self) -> sval_nested::Result { print!("]"); Ok(()) } } impl<'sval> sval_nested::StreamMap<'sval> for Stream { type Ok = (); fn key_computed(&mut self, key: V) -> sval_nested::Result { print!("[ "); sval_nested::Stream::value_computed(Stream, key)?; print!(", "); Ok(()) } fn value_computed(&mut self, value: V) -> sval_nested::Result { sval_nested::Stream::value_computed(Stream, value)?; print!(", ], "); Ok(()) } fn end(self) -> sval_nested::Result { sval_nested::StreamSeq::end(self) } } impl<'sval> sval_nested::StreamTuple<'sval> for Stream { type Ok = (); fn value_computed( &mut self, _: Option, _: sval::Index, value: V, ) -> sval_nested::Result { sval_nested::StreamSeq::value_computed(self, value) } fn end(self) -> sval_nested::Result { sval_nested::StreamSeq::end(self) } } impl<'sval> sval_nested::StreamRecord<'sval> for Stream { type Ok = (); fn value_computed( &mut self, _: Option, label: sval::Label, value: V, ) -> sval_nested::Result { sval_nested::StreamMap::key_computed(self, label.as_str())?; sval_nested::StreamMap::value_computed(self, value) } fn end(self) -> sval_nested::Result { sval_nested::StreamSeq::end(self) } } impl<'sval> sval_nested::StreamEnum<'sval> for Stream { type Ok = (); type Tuple = Self; type Record = Self; type Nested = Self; fn tag( self, tag: Option, label: Option, index: Option, ) -> sval_nested::Result { sval_nested::Stream::tag(self, tag, label, index) } fn tagged_computed( self, tag: Option, label: Option, index: Option, value: V, ) -> sval_nested::Result { sval_nested::Stream::tagged_computed(self, tag, label, index, value) } fn tuple_begin( self, tag: Option, label: Option, index: Option, num_entries: Option, ) -> sval_nested::Result { sval_nested::Stream::tuple_begin(self, tag, label, index, num_entries) } fn record_begin( self, tag: Option, label: Option, index: Option, num_entries: Option, ) -> sval_nested::Result { sval_nested::Stream::record_begin(self, tag, label, index, num_entries) } fn nested< F: FnOnce( Self::Nested, ) -> sval_nested::Result<>::Ok>, >( self, _: Option, _: Option, _: Option, variant: F, ) -> sval_nested::Result { variant(self) } fn empty(self) -> sval_nested::Result { sval_nested::Stream::null(self) } } fn main() -> sval_nested::Result { stream(42)?; stream(true)?; stream(Some(42))?; stream(None::)?; stream(sval::MapSlice::new(&[("a", 1), ("b", 2), ("c", 3)]))?; stream(&[&["Hello", "world"], &["Hello", "world"]])?; Ok(()) } fn stream(v: impl sval::Value) -> sval_nested::Result { sval_nested::stream_computed(Stream, v)?; println!(); Ok(()) }