automerge/rust/automerge/src/parents.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

35 lines
809 B
Rust

use crate::op_set::OpSet;
use crate::types::ObjId;
use crate::{exid::ExId, Prop};
#[derive(Debug)]
pub struct Parents<'a> {
pub(crate) obj: ObjId,
pub(crate) ops: &'a OpSet,
}
impl<'a> Parents<'a> {
pub fn path(&mut self) -> Vec<(ExId, Prop)> {
let mut path = self.collect::<Vec<_>>();
path.reverse();
path
}
}
impl<'a> Iterator for Parents<'a> {
type Item = (ExId, Prop);
fn next(&mut self) -> Option<Self::Item> {
if self.obj.is_root() {
None
} else if let Some((obj, key)) = self.ops.parent_object(&self.obj) {
self.obj = obj;
Some((
self.ops.id_to_exid(self.obj.0),
self.ops.export_key(self.obj, key),
))
} else {
None
}
}
}