use crate::*; use std::{cell::RefCell, fmt::Debug, rc::Rc}; use serde::{Deserialize, Serialize}; #[derive(Clone)] pub struct ValueStream(pub Rc>>); impl ValueStream { pub fn into_string(self) -> String { format!( "[{}]", self.map(|x: Value| x.into_string()) .collect::>() .join(", ") ) } pub fn from_stream(input: impl Iterator + '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.0.borrow_mut().next() } } } impl Serialize for ValueStream { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { todo!() } } impl<'de> Deserialize<'de> for ValueStream { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { todo!() } } pub trait IntoValueStream { fn into_value_stream(self) -> ValueStream; } impl IntoValueStream for T where T: Iterator + 'static, { fn into_value_stream(self) -> ValueStream { ValueStream::from_stream(self) } }