MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / leaves

Method leaves

atomic-core/examples/compact_semantic_codec/codec.rs:138–199  ·  view source on GitHub ↗
(
        &mut self,
        ops: &[LeafOp],
        range: Option<(ChangePosition, ChangePosition)>,
    )

Source from the content-addressed store, hash-verified

136 }
137
138 fn leaves(
139 &mut self,
140 ops: &[LeafOp],
141 range: Option<(ChangePosition, ChangePosition)>,
142 ) -> Result<()> {
143 // Match whole authored fragments, not each token against the whole blob.
144 // Old/deleted text absent from this change goes into a lossless literal pool.
145 let joined: Vec<u8> = ops
146 .iter()
147 .filter_map(LeafOp::content)
148 .flatten()
149 .copied()
150 .collect();
151 let hinted = range.and_then(|(a, b)| {
152 let a = usize::try_from(a.get()).ok()?;
153 let b = usize::try_from(b.get()).ok()?;
154 (self.content.get(a..b)? == joined).then_some(a)
155 });
156 let sequential = self
157 .content
158 .get(self.s.content_cursor..)
159 .filter(|rest| rest.starts_with(&joined))
160 .map(|_| self.s.content_cursor);
161 let mut offset = hinted
162 .or(sequential)
163 .or_else(|| self.lines.get(joined.as_slice()).copied());
164 put(&mut self.c.control, ops.len() as u64);
165 for op in ops {
166 match op {
167 LeafOp::Insert {
168 after,
169 kind,
170 content,
171 } => {
172 put(&mut self.c.control, 0);
173 put(&mut self.c.control, u64::from(after.is_some()));
174 if let Some(after) = after {
175 self.leaf(*after);
176 }
177 self.c.kinds.extend(postcard::to_allocvec(kind)?);
178 self.bytes(content, offset);
179 }
180 LeafOp::Delete { leaf } => {
181 put(&mut self.c.control, 1);
182 self.leaf(*leaf);
183 }
184 LeafOp::Replace { leaf, new_content } => {
185 put(&mut self.c.control, 2);
186 self.leaf(*leaf);
187 self.bytes(new_content, offset);
188 }
189 LeafOp::Restore { leaf } => {
190 put(&mut self.c.control, 3);
191 self.leaf(*leaf);
192 }
193 }
194 if let (Some(at), Some(bytes)) = (&mut offset, op.content()) {
195 *at += bytes.len();

Callers 2

encodeFunction · 0.45
decodeFunction · 0.45

Calls 15

extendMethod · 0.80
putFunction · 0.70
getFunction · 0.70
getMethod · 0.65
collectMethod · 0.45
iterMethod · 0.45
okMethod · 0.45
as_sliceMethod · 0.45
lenMethod · 0.45
leafMethod · 0.45
bytesMethod · 0.45
contentMethod · 0.45

Tested by

no test coverage detected