diff --git a/TODO.md b/TODO.md index 8d55d0cb5c..a580743004 100644 --- a/TODO.md +++ b/TODO.md @@ -26,9 +26,9 @@ - [x] Exports - [x] Source - [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands. +- [x] Value serialization - [ ] Input/output types - [ ] Support for `$in` -- [ ] Value serialization - [ ] Handling rows with missing columns during a cell path - [ ] ctrl-c support - [ ] operator overflow diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index 4ac557247f..d3774ca750 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -1,7 +1,7 @@ use crate::*; use std::{cell::RefCell, fmt::Debug, rc::Rc}; -use serde::{Deserialize, Serialize}; +use serde::{ser::SerializeSeq, Deserialize, Serialize}; #[derive(Clone)] pub struct ValueStream(pub Rc>>); @@ -44,22 +44,49 @@ impl Iterator for ValueStream { } impl Serialize for ValueStream { - fn serialize(&self, _serializer: S) -> Result + fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { - // FIXME: implement these - todo!() + let mut seq = serializer.serialize_seq(None)?; + + for element in self.0.borrow_mut().into_iter() { + seq.serialize_element(&element)?; + } + seq.end() } } impl<'de> Deserialize<'de> for ValueStream { - fn deserialize(_deserializer: D) -> Result + fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { // FIXME: implement these - todo!() + deserializer.deserialize_seq(MySeqVisitor) + } +} + +struct MySeqVisitor; + +impl<'a> serde::de::Visitor<'a> for MySeqVisitor { + type Value = ValueStream; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("a value stream") + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: serde::de::SeqAccess<'a>, + { + let mut output: Vec = vec![]; + + while let Some(value) = seq.next_element()? { + output.push(value); + } + + Ok(ValueStream(Rc::new(RefCell::new(output.into_iter())))) } }