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>,
)
| 354 | /// |
| 355 | /// The leaf if validation passes. |
| 356 | pub 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 | /// |