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

Function validate_branch_state

atomic-core/src/crdt/apply/branch.rs:407–433  ·  view source on GitHub ↗

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

(
    txn: &T,
    branch_id: BranchId,
    expected_state: Option<BranchState>,
)

Source from the content-addressed store, hash-verified

405///
406/// The branch if validation passes.
407pub fn validate_branch_state<T: MutCrdtTxnT>(
408 txn: &T,
409 branch_id: BranchId,
410 expected_state: Option<BranchState>,
411) -> ApplyResult<Branch> {
412 let branch = txn
413 .get_branch(branch_id)
414 .map_err(|e| storage_err(e, "getting branch"))?
415 .ok_or_else(|| ApplyError::branch_not_found(branch_id))?;
416
417 if let Some(expected) = expected_state {
418 let matches = match expected {
419 BranchState::Alive => branch.state().is_alive(),
420 BranchState::Deleted => branch.state().is_deleted(),
421 };
422
423 if !matches {
424 return Err(ApplyError::invalid_branch_state(
425 branch_id,
426 branch.state().to_string(),
427 format!("expected {}", expected),
428 ));
429 }
430 }
431
432 Ok(branch)
433}
434
435/// Validates that a branch belongs to the specified trunk.
436///

Callers 1

Calls 5

storage_errFunction · 0.85
get_branchMethod · 0.45
is_aliveMethod · 0.45
stateMethod · 0.45
is_deletedMethod · 0.45

Tested by 1