Validates that a branch belongs to the specified trunk. # Arguments `txn` - The transaction `branch_id` - The branch to validate `expected_trunk` - The expected parent trunk # Returns `Ok(())` if the branch belongs to the trunk.
(
txn: &T,
branch_id: BranchId,
expected_trunk: TrunkId,
)
| 444 | /// |
| 445 | /// `Ok(())` if the branch belongs to the trunk. |
| 446 | pub fn validate_branch_parent<T: MutCrdtTxnT>( |
| 447 | txn: &T, |
| 448 | branch_id: BranchId, |
| 449 | expected_trunk: TrunkId, |
| 450 | ) -> ApplyResult<()> { |
| 451 | let branch = txn |
| 452 | .get_branch(branch_id) |
| 453 | .map_err(|e| storage_err(e, "getting branch"))? |
| 454 | .ok_or_else(|| ApplyError::branch_not_found(branch_id))?; |
| 455 | |
| 456 | if branch.trunk() != expected_trunk { |
| 457 | return Err(ApplyError::internal(format!( |
| 458 | "Branch {} belongs to trunk {}, not {}", |
| 459 | branch_id, |
| 460 | branch.trunk(), |
| 461 | expected_trunk |
| 462 | ))); |
| 463 | } |
| 464 | |
| 465 | Ok(()) |
| 466 | } |
| 467 | |
| 468 | // Tests |
| 469 |