automerge/rust/automerge/src/values.rs
Alex Good dd3c6d1303
Move rust workspace into ./rust
After some discussion with PVH I realise that the repo structure in the
last reorg was very rust-centric. In an attempt to put each language on
a level footing move the rust code and project files into ./rust
2022-10-16 19:55:51 +01:00

60 lines
1.3 KiB
Rust

use crate::exid::ExId;
use crate::{Automerge, Value};
use std::fmt;
pub struct Values<'a> {
range: Box<dyn 'a + ValueIter<'a>>,
doc: &'a Automerge,
}
impl<'a> fmt::Debug for Values<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Values").finish()
}
}
pub(crate) trait ValueIter<'a> {
fn next_value(&mut self, doc: &'a Automerge) -> Option<(Value<'a>, ExId)>;
}
pub(crate) struct NoValues {}
impl<'a> ValueIter<'a> for NoValues {
fn next_value(&mut self, _doc: &'a Automerge) -> Option<(Value<'a>, ExId)> {
None
}
}
impl<'a> Values<'a> {
pub(crate) fn new<R: 'a + ValueIter<'a>>(doc: &'a Automerge, range: Option<R>) -> Self {
if let Some(range) = range {
Self {
range: Box::new(range),
doc,
}
} else {
Self::empty(doc)
}
}
pub(crate) fn empty(doc: &'a Automerge) -> Self {
Self {
range: Box::new(NoValues {}),
doc,
}
}
}
impl<'a> Iterator for Values<'a> {
type Item = (Value<'a>, ExId);
fn next(&mut self) -> Option<Self::Item> {
self.range.next_value(self.doc)
}
}
impl<'a> DoubleEndedIterator for Values<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}