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

Method validate_content_ranges

atomic-core/src/change/span_patch.rs:191–249  ·  view source on GitHub ↗

Check local byte bounds. Dependency closure, file ownership, and conflicts must additionally be checked against repository state.

(&self, content_len: u64)

Source from the content-addressed store, hash-verified

189 /// Check local byte bounds. Dependency closure, file ownership, and
190 /// conflicts must additionally be checked against repository state.
191 pub fn validate_content_ranges(&self, content_len: u64) -> Result<(), SpanPatchError> {
192 if self.version != 1 && self.version != VERSION {
193 return Err(SpanPatchError::UnsupportedVersion(self.version));
194 }
195 if self.version == 1 && !self.semantic_owners.is_empty() {
196 return Err(SpanPatchError::InvalidEncoding);
197 }
198 if self
199 .semantic_owners
200 .iter()
201 .any(|entry| entry.authored_id == 0 || entry.owner == Hash::NONE)
202 || self
203 .semantic_owners
204 .windows(2)
205 .any(|pair| pair[0].authored_id >= pair[1].authored_id)
206 {
207 return Err(SpanPatchError::InvalidEncoding);
208 }
209 let mut owner_hashes = std::collections::HashSet::new();
210 if self
211 .semantic_owners
212 .iter()
213 .any(|entry| !owner_hashes.insert(entry.owner))
214 {
215 return Err(SpanPatchError::InvalidEncoding);
216 }
217 for (index, op) in self.ops.iter().enumerate() {
218 let reason = match op {
219 SpanOp::Root { start, end, .. } if start > end || *end > content_len => {
220 Some("root range is reversed or outside the contents")
221 }
222 SpanOp::Edit { file, target, .. }
223 if file.owner == Hash::NONE || target.owner == Hash::NONE =>
224 {
225 Some("file and target owners must be real changes")
226 }
227 SpanOp::Edit { target, .. } if target.start > target.end => {
228 Some("target span is reversed")
229 }
230 SpanOp::Edit {
231 target, start, end, ..
232 } if start > end || *end > target.end.saturating_sub(target.start) => {
233 Some("edit range is reversed or outside the target span")
234 }
235 SpanOp::Edit {
236 insert_start,
237 insert_end,
238 ..
239 } if insert_start > insert_end || *insert_end > content_len => {
240 Some("inserted range is reversed or outside the contents")
241 }
242 _ => None,
243 };
244 if let Some(reason) = reason {
245 return Err(SpanPatchError::InvalidOperation { index, reason });
246 }
247 }
248 Ok(())

Callers 3

index_changeFunction · 0.45
localize_file_opsFunction · 0.45
validate_v2Method · 0.45

Calls 3

is_emptyMethod · 0.45
iterMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected