Validates that a trunk exists and is in the expected state. # Arguments `txn` - The transaction `trunk_id` - The trunk to validate `expected_state` - The expected state, or `None` for any state # Returns The trunk if validation passes.
(
txn: &T,
trunk_id: TrunkId,
expected_state: Option<TrunkState>,
)
| 299 | /// |
| 300 | /// The trunk if validation passes. |
| 301 | pub fn validate_trunk_state<T: MutCrdtTxnT>( |
| 302 | txn: &T, |
| 303 | trunk_id: TrunkId, |
| 304 | expected_state: Option<TrunkState>, |
| 305 | ) -> ApplyResult<Trunk> { |
| 306 | let trunk = txn |
| 307 | .get_trunk(trunk_id) |
| 308 | .map_err(|e| storage_err(e, "getting trunk"))? |
| 309 | .ok_or_else(|| ApplyError::trunk_not_found(trunk_id))?; |
| 310 | |
| 311 | if let Some(expected) = expected_state { |
| 312 | let matches = match expected { |
| 313 | TrunkState::Alive => trunk.state().is_alive(), |
| 314 | TrunkState::Deleted => trunk.state().is_deleted(), |
| 315 | TrunkState::Zombie => trunk.state().is_zombie(), |
| 316 | }; |
| 317 | |
| 318 | if !matches { |
| 319 | return Err(ApplyError::invalid_trunk_state( |
| 320 | trunk_id, |
| 321 | trunk.state().to_string(), |
| 322 | format!("expected {}", expected), |
| 323 | )); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | Ok(trunk) |
| 328 | } |
| 329 | |
| 330 | /// Checks if a path is available (not taken by another file). |
| 331 | /// |