project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use std::sync::Weak; |
| 2 | |
| 3 | use loro_common::{ContainerID, LoroResult, LoroValue}; |
| 4 | |
| 5 | use crate::{ |
| 6 | configure::Configure, |
| 7 | container::idx::ContainerIdx, |
| 8 | event::{Diff, Index, InternalDiff}, |
| 9 | op::{Op, RawOp}, |
| 10 | LoroDocInner, |
| 11 | }; |
| 12 | |
| 13 | use super::{ApplyLocalOpReturn, ContainerState, DiffApplyContext}; |
| 14 | |
| 15 | #[derive(Debug, Clone)] |
| 16 | pub struct UnknownState { |
| 17 | idx: ContainerIdx, |
| 18 | } |
| 19 | |
| 20 | impl UnknownState { |
| 21 | pub fn new(idx: ContainerIdx) -> Self { |
| 22 | Self { idx } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl ContainerState for UnknownState { |
| 27 | fn container_idx(&self) -> ContainerIdx { |
| 28 | self.idx |
| 29 | } |
| 30 | |
| 31 | fn is_state_empty(&self) -> bool { |
| 32 | false |
| 33 | } |
| 34 | |
| 35 | // An unknown container created and edited within one import/checkout still |
| 36 | // receives its (empty) diff; there is nothing to apply. |
| 37 | fn apply_diff_and_convert(&mut self, diff: InternalDiff, _ctx: DiffApplyContext) -> Diff { |
| 38 | assert!(matches!(diff, InternalDiff::Unknown), "{diff:?}"); |
| 39 | Diff::Unknown |
| 40 | } |
| 41 | |
| 42 | fn apply_diff(&mut self, diff: InternalDiff, _ctx: DiffApplyContext) -> LoroResult<()> { |
| 43 | assert!(matches!(diff, InternalDiff::Unknown), "{diff:?}"); |
| 44 | Ok(()) |
| 45 | } |
| 46 | |
| 47 | fn apply_local_op(&mut self, _raw_op: &RawOp, _op: &Op) -> LoroResult<ApplyLocalOpReturn> { |
| 48 | unreachable!() |
| 49 | } |
| 50 | |
| 51 | #[doc = r" Convert a state to a diff, such that an empty state will be transformed into the same as this state when it's applied."] |
| 52 | fn to_diff(&mut self, _doc: &Weak<LoroDocInner>) -> Diff { |
| 53 | Diff::Unknown |
| 54 | } |
| 55 | |
| 56 | fn get_value(&mut self) -> LoroValue { |
| 57 | // The payload of an unknown container is opaque to this version of |
| 58 | // Loro; expose it as `Null`, matching the public API convention in |
| 59 | // `ValueOrContainer::get_deep_value` (crates/loro/src/lib.rs) and |
| 60 | // `decode_value` below. |
| 61 | LoroValue::Null |
| 62 | } |
| 63 | |
| 64 | #[doc = r" Get the index of the child container"] |
| 65 | #[allow(unused)] |
| 66 | fn get_child_index(&self, id: &ContainerID) -> Option<Index> { |
| 67 | None |
| 68 | } |
| 69 | |
| 70 | fn contains_child(&self, _id: &ContainerID) -> bool { |
| 71 | false |
| 72 | } |
| 73 | |
| 74 | #[allow(unused)] |
| 75 | fn get_child_containers(&self) -> Vec<ContainerID> { |
| 76 | vec![] |
| 77 | } |
| 78 | |
| 79 | fn fork(&self, _config: &Configure) -> Self { |
| 80 | self.clone() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | mod snapshot { |
| 85 | use loro_common::LoroValue; |
| 86 | |
| 87 | use crate::state::FastStateSnapshot; |
| 88 | |
| 89 | use super::UnknownState; |
| 90 | |
| 91 | impl FastStateSnapshot for UnknownState { |
| 92 | fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, _w: W) {} |
| 93 | |
| 94 | fn decode_value(bytes: &[u8]) -> loro_common::LoroResult<(loro_common::LoroValue, &[u8])> { |
| 95 | Ok((LoroValue::Null, bytes)) |
| 96 | } |
| 97 | |
| 98 | fn decode_snapshot_fast( |
| 99 | idx: crate::container::idx::ContainerIdx, |
| 100 | _v: (loro_common::LoroValue, &[u8]), |
| 101 | _ctx: crate::state::ContainerCreationContext, |
| 102 | ) -> loro_common::LoroResult<Self> |
| 103 | where |
| 104 | Self: Sized, |
| 105 | { |
| 106 | Ok(UnknownState::new(idx)) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod tests { |
| 113 | use std::sync::Weak; |
| 114 | |
| 115 | use loro_common::{ContainerID, ContainerType}; |
| 116 | |
| 117 | use crate::{ |
| 118 | configure::Configure, |
| 119 | container::idx::ContainerIdx, |
| 120 | event::Diff, |
| 121 | state::{ContainerCreationContext, ContainerState, FastStateSnapshot}, |
| 122 | InternalString, |
| 123 | }; |
| 124 | |
| 125 | use super::UnknownState; |
| 126 | |
| 127 | fn unknown_idx() -> ContainerIdx { |
| 128 | ContainerIdx::from_index_and_type(7, ContainerType::Unknown(3)) |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn unknown_state_reports_identity_without_material_value() { |
| 133 | let mut state = UnknownState::new(unknown_idx()); |
| 134 | assert_eq!(state.container_idx(), unknown_idx()); |
| 135 | assert!(!state.is_state_empty()); |
| 136 | |
| 137 | assert!(matches!(state.to_diff(&Weak::new()), Diff::Unknown)); |
| 138 | assert_eq!(state.get_child_containers(), Vec::<ContainerID>::new()); |
| 139 | assert!(!state.contains_child(&ContainerID::Root { |
| 140 | name: InternalString::from("child"), |
| 141 | container_type: ContainerType::Text, |
| 142 | })); |
| 143 | assert_eq!( |
| 144 | state.get_child_index(&ContainerID::Root { |
| 145 | name: InternalString::from("child"), |
| 146 | container_type: ContainerType::Text, |
| 147 | }), |
| 148 | None |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn unknown_state_fork_keeps_container_identity() { |
| 154 | let state = UnknownState::new(unknown_idx()); |
| 155 | let forked = state.fork(&Configure::default()); |
| 156 | |
| 157 | assert_eq!(forked.container_idx(), unknown_idx()); |
| 158 | assert!(!forked.is_state_empty()); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn unknown_snapshot_roundtrip_preserves_bytes_and_index() { |
| 163 | let mut state = UnknownState::new(unknown_idx()); |
| 164 | let mut encoded = Vec::new(); |
| 165 | state.encode_snapshot_fast(&mut encoded); |
| 166 | assert!(encoded.is_empty()); |
| 167 | |
| 168 | let input = b"opaque-unknown-payload"; |
| 169 | let (value, rest) = UnknownState::decode_value(input).unwrap(); |
| 170 | assert_eq!(value, loro_common::LoroValue::Null); |
| 171 | assert_eq!(rest, input); |
| 172 | |
| 173 | let decoded = UnknownState::decode_snapshot_fast( |
| 174 | unknown_idx(), |
| 175 | (value, rest), |
| 176 | ContainerCreationContext { |
| 177 | configure: &Configure::default(), |
| 178 | peer: 0, |
| 179 | }, |
| 180 | ) |
| 181 | .unwrap(); |
| 182 | assert_eq!(decoded.container_idx(), unknown_idx()); |
| 183 | } |
| 184 | } |