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

Function validate_trunk_state

atomic-core/src/crdt/apply/trunk.rs:301–328  ·  view source on GitHub ↗

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>,
)

Source from the content-addressed store, hash-verified

299///
300/// The trunk if validation passes.
301pub 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///

Callers 1

Calls 6

storage_errFunction · 0.85
get_trunkMethod · 0.45
is_aliveMethod · 0.45
stateMethod · 0.45
is_deletedMethod · 0.45
is_zombieMethod · 0.45

Tested by 1