nushell/crates/nu-protocol/src/value/stream.rs

78 lines
1.7 KiB
Rust
Raw Normal View History

2021-09-08 04:26:57 +02:00
use crate::*;
use std::{cell::RefCell, fmt::Debug, rc::Rc};
2021-10-01 07:11:49 +02:00
use serde::{Deserialize, Serialize};
2021-09-08 04:26:57 +02:00
#[derive(Clone)]
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
impl ValueStream {
pub fn into_string(self) -> String {
format!(
"[{}]",
self.map(|x: Value| x.into_string())
.collect::<Vec<String>>()
.join(", ")
)
}
2021-10-01 08:01:22 +02:00
pub fn collect_string(self) -> String {
self.map(|x: Value| x.collect_string())
.collect::<Vec<String>>()
.join("\n")
}
2021-09-08 04:26:57 +02:00
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {
ValueStream(Rc::new(RefCell::new(input)))
}
}
impl Debug for ValueStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ValueStream").finish()
}
}
impl Iterator for ValueStream {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
{
2021-09-23 18:42:03 +02:00
self.0.borrow_mut().next()
2021-09-08 04:26:57 +02:00
}
}
}
2021-10-01 07:11:49 +02:00
impl Serialize for ValueStream {
2021-10-01 07:20:25 +02:00
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
2021-10-01 07:11:49 +02:00
where
S: serde::Serializer,
{
2021-10-01 07:20:25 +02:00
// FIXME: implement these
2021-10-01 07:11:49 +02:00
todo!()
}
}
impl<'de> Deserialize<'de> for ValueStream {
2021-10-01 07:20:25 +02:00
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
2021-10-01 07:11:49 +02:00
where
D: serde::Deserializer<'de>,
{
2021-10-01 07:20:25 +02:00
// FIXME: implement these
2021-10-01 07:11:49 +02:00
todo!()
}
}
2021-09-08 04:26:57 +02:00
pub trait IntoValueStream {
fn into_value_stream(self) -> ValueStream;
}
impl<T> IntoValueStream for T
where
T: Iterator<Item = Value> + 'static,
{
fn into_value_stream(self) -> ValueStream {
ValueStream::from_stream(self)
}
}