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 collect_string(self) -> String { self.map(|x: Value| x.collect_string()) .collect::>() .join("\n") } 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, { // FIXME: implement these todo!() } } impl<'de> Deserialize<'de> for ValueStream { fn deserialize(_deserializer: D) -> Result where D: serde::Deserializer<'de>, { // FIXME: implement these 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) } }