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

Function validate_leaf_state

atomic-core/src/crdt/apply/leaf.rs:356–382  ·  view source on GitHub ↗

Validates that a leaf exists and is in the expected state. # Arguments `txn` - The transaction `leaf_id` - The leaf to validate `expected_state` - The expected state, or `None` for any state # Returns The leaf if validation passes.

(
    txn: &T,
    leaf_id: LeafId,
    expected_state: Option<LeafState>,
)

Source from the content-addressed store, hash-verified

354///
355/// The leaf if validation passes.
356pub fn validate_leaf_state<T: MutCrdtTxnT>(
357 txn: &T,
358 leaf_id: LeafId,
359 expected_state: Option<LeafState>,
360) -> ApplyResult<Leaf> {
361 let leaf = txn
362 .get_leaf(leaf_id)
363 .map_err(|e| storage_err(e, "getting leaf"))?
364 .ok_or_else(|| ApplyError::leaf_not_found(leaf_id))?;
365
366 if let Some(expected) = expected_state {
367 let matches = match expected {
368 LeafState::Alive => leaf.state().is_alive(),
369 LeafState::Deleted => leaf.state().is_deleted(),
370 };
371
372 if !matches {
373 return Err(ApplyError::invalid_leaf_state(
374 leaf_id,
375 leaf.state().to_string(),
376 format!("expected {}", expected),
377 ));
378 }
379 }
380
381 Ok(leaf)
382}
383
384/// Validates that a leaf belongs to the specified branch.
385///

Callers 1

test_validate_leaf_stateFunction · 0.85

Calls 5

storage_errFunction · 0.85
get_leafMethod · 0.45
is_aliveMethod · 0.45
stateMethod · 0.45
is_deletedMethod · 0.45

Tested by 1

test_validate_leaf_stateFunction · 0.68