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>,
)
| 405 | /// |
| 406 | /// The branch if validation passes. |
| 407 | pub 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 | /// |