Merge pull request #120 from nushell/serialize_stream

Add serialize/deserialize for streams
This commit is contained in:
JT 2021-10-12 07:18:41 +13:00 committed by GitHub
commit 020143d050
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -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

View File

@ -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<RefCell<dyn Iterator<Item = Value>>>);
@ -44,22 +44,49 @@ impl Iterator for ValueStream {
}
impl Serialize for ValueStream {
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(_deserializer: D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'a>,
{
let mut output: Vec<Value> = vec![];
while let Some(value) = seq.next_element()? {
output.push(value);
}
Ok(ValueStream(Rc::new(RefCell::new(output.into_iter()))))
}
}