mirror of
https://github.com/nushell/nushell
synced 2025-01-29 05:13:31 +00:00
Merge pull request #120 from nushell/serialize_stream
Add serialize/deserialize for streams
This commit is contained in:
commit
020143d050
2 changed files with 34 additions and 7 deletions
2
TODO.md
2
TODO.md
|
@ -26,9 +26,9 @@
|
||||||
- [x] Exports
|
- [x] Exports
|
||||||
- [x] Source
|
- [x] Source
|
||||||
- [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands.
|
- [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands.
|
||||||
|
- [x] Value serialization
|
||||||
- [ ] Input/output types
|
- [ ] Input/output types
|
||||||
- [ ] Support for `$in`
|
- [ ] Support for `$in`
|
||||||
- [ ] Value serialization
|
|
||||||
- [ ] Handling rows with missing columns during a cell path
|
- [ ] Handling rows with missing columns during a cell path
|
||||||
- [ ] ctrl-c support
|
- [ ] ctrl-c support
|
||||||
- [ ] operator overflow
|
- [ ] operator overflow
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::{cell::RefCell, fmt::Debug, rc::Rc};
|
use std::{cell::RefCell, fmt::Debug, rc::Rc};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{ser::SerializeSeq, Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
|
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
|
||||||
|
@ -44,22 +44,49 @@ impl Iterator for ValueStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize 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
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
// FIXME: implement these
|
let mut seq = serializer.serialize_seq(None)?;
|
||||||
todo!()
|
|
||||||
|
for element in self.0.borrow_mut().into_iter() {
|
||||||
|
seq.serialize_element(&element)?;
|
||||||
|
}
|
||||||
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for ValueStream {
|
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
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
// FIXME: implement these
|
// 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()))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue