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

Function encode

atomic-core/examples/compact_semantic_codec/codec.rs:202–275  ·  view source on GitHub ↗
(ops: &[FileOps], content: &[u8], level: i32)

Source from the content-addressed store, hash-verified

200}
201
202pub(super) fn encode(ops: &[FileOps], content: &[u8], level: i32) -> Result<Vec<u8>> {
203 let mut lines = HashMap::new();
204 let mut offset = 0;
205 for line in content.split_inclusive(|b| *b == b'\n') {
206 lines.entry(line).or_insert(offset);
207 // Semantic snapshots sometimes omit the line ending.
208 let trimmed = line.strip_suffix(b"\n").unwrap_or(line);
209 lines.entry(trimmed).or_insert(offset);
210 offset += line.len();
211 }
212 let mut e = Encoder {
213 c: Columns {
214 content_hash: *blake3::hash(content).as_bytes(),
215 ..Columns::default()
216 },
217 s: State::default(),
218 content,
219 lines,
220 literals: HashMap::new(),
221 };
222 for file in ops {
223 e.c.files.push((
224 file.trunk_id(),
225 file.path().into(),
226 file.trunk_op().cloned(),
227 ));
228 put(&mut e.c.control, file.line_ops().len() as u64);
229 for line in file.line_ops() {
230 e.branch(line.branch_id());
231 e.optional_number(line.old_line_num());
232 e.optional_number(line.new_line_num());
233 put(&mut e.c.control, u64::from(line.content_range().is_some()));
234 if let Some((a, b)) = line.content_range() {
235 delta(&mut e.c.positions, &mut e.s.position, a.get());
236 delta(&mut e.c.positions, &mut e.s.position, b.get());
237 }
238 match line.operation() {
239 BranchOp::Insert { after, content } => {
240 put(&mut e.c.control, 0);
241 e.optional_branch(*after);
242 e.leaves(content, line.content_range())?;
243 }
244 BranchOp::Delete { branch, content } => {
245 put(&mut e.c.control, 1);
246 e.branch(*branch);
247 e.leaves(content, None)?;
248 }
249 BranchOp::Modify {
250 branch,
251 old_content,
252 new_content,
253 } => {
254 put(&mut e.c.control, 2);
255 e.branch(*branch);
256 e.leaves(old_content, None)?;
257 e.leaves(new_content, line.content_range())?;
258 }
259 BranchOp::Restore { branch } => {

Calls 15

optional_numberMethod · 0.80
extendMethod · 0.80
putFunction · 0.70
deltaFunction · 0.70
getMethod · 0.65
hashFunction · 0.50
lenMethod · 0.45
as_bytesMethod · 0.45
pushMethod · 0.45
trunk_idMethod · 0.45
pathMethod · 0.45
trunk_opMethod · 0.45