#[derive(Clone, serde::Serialize, serde::Deserialize)] pub struct Path(pub Vec); impl std::ops::Deref for Path { type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef for usize { fn as_ref(&self) -> &Path { todo!() } } impl Path { // delta is default to be 1 pub fn transform(pre_insert_path: &Path, b: &Path, offset: i64) -> Path { if pre_insert_path.len() > b.len() { return b.clone(); } if pre_insert_path.is_empty() || b.is_empty() { return b.clone(); } // check the prefix for i in 0..(pre_insert_path.len() - 1) { if pre_insert_path.0[i] != b.0[i] { return b.clone(); } } let mut prefix: Vec = pre_insert_path.0[0..(pre_insert_path.len() - 1)].into(); let mut suffix: Vec = b.0[pre_insert_path.0.len()..].into(); let prev_insert_last: usize = *pre_insert_path.0.last().unwrap(); let b_at_index = b.0[pre_insert_path.0.len() - 1]; if prev_insert_last <= b_at_index { prefix.push(((b_at_index as i64) + offset) as usize); } else { prefix.push(b_at_index); } prefix.append(&mut suffix); Path(prefix) } } impl From> for Path { fn from(v: Vec) -> Self { Path(v) } }