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

Method leaves

atomic-core/src/change/format_v3/semantic/encode.rs:79–147  ·  view source on GitHub ↗
(
        &mut self,
        ops: &[LeafOp],
        range: Option<(ChangePosition, ChangePosition)>,
    )

Source from the content-addressed store, hash-verified

77 }
78 }
79 fn leaves(
80 &mut self,
81 ops: &[LeafOp],
82 range: Option<(ChangePosition, ChangePosition)>,
83 ) -> FormatResult<()> {
84 charge(&mut self.remaining, ops.len(), LeafOp::COST)?;
85 for op in ops {
86 if let Some(content) = op.content() {
87 charge(&mut self.remaining, content.len(), 1)?;
88 }
89 }
90 let joined: Vec<u8> = ops
91 .iter()
92 .filter_map(LeafOp::content)
93 .flatten()
94 .copied()
95 .collect();
96 let hint = range.and_then(|(a, b)| {
97 let a = usize::try_from(a.get()).ok()?;
98 let b = usize::try_from(b.get()).ok()?;
99 (self.content.get(a..b)? == joined).then_some(a)
100 });
101 let sequential = self
102 .content
103 .get(self.s.cursor..)
104 .filter(|rest| rest.starts_with(&joined))
105 .map(|_| self.s.cursor);
106 let mut offset = hint
107 .or(sequential)
108 .or_else(|| self.lines.get(joined.as_slice()).copied());
109 put(&mut self.c.control, ops.len() as u64);
110 for op in ops {
111 match op {
112 LeafOp::Insert {
113 after,
114 kind,
115 content,
116 } => {
117 put(&mut self.c.control, 0);
118 put(&mut self.c.control, u64::from(after.is_some()));
119 if let Some(id) = after {
120 self.id(false, id.change_id().get(), id.leaf_idx());
121 }
122 self.c.kinds.extend(postcard::to_allocvec(kind)?);
123 self.bytes(content, offset);
124 }
125 LeafOp::Delete { leaf } | LeafOp::Restore { leaf } => {
126 put(
127 &mut self.c.control,
128 if matches!(op, LeafOp::Delete { .. }) {
129 1
130 } else {
131 3
132 },
133 );
134 self.id(false, leaf.change_id().get(), leaf.leaf_idx());
135 }
136 LeafOp::Replace { leaf, new_content } => {

Callers 1

lineMethod · 0.45

Calls 14

leaf_idxMethod · 0.80
extendMethod · 0.80
chargeFunction · 0.70
putFunction · 0.70
getMethod · 0.65
lenMethod · 0.45
contentMethod · 0.45
collectMethod · 0.45
iterMethod · 0.45
okMethod · 0.45
as_sliceMethod · 0.45
idMethod · 0.45

Tested by

no test coverage detected