Skip to main content

khive_runtime/operations/
resolve_uuid_or_prefix.rs

1//! Shared UUID-or-prefix resolution for pack handlers.
2
3use uuid::Uuid;
4
5use crate::error::{RuntimeError, RuntimeResult};
6use crate::runtime::{KhiveRuntime, NamespaceToken};
7
8impl KhiveRuntime {
9    /// Parse a full UUID or resolve a compact hexadecimal prefix in the caller's
10    /// primary namespace, using the same lookup policy as [`Self::resolve_prefix`].
11    ///
12    /// A parseable full UUID is returned without a lookup or an existence,
13    /// liveness or authorization check. Otherwise input must contain at least
14    /// eight ASCII hexadecimal characters with no separators or whitespace.
15    /// Missing prefixes and invalid shapes return distinct `InvalidInput` errors;
16    /// lookup storage and ambiguity errors propagate unchanged.
17    pub async fn resolve_uuid_or_prefix(
18        &self,
19        token: &NamespaceToken,
20        s: &str,
21    ) -> RuntimeResult<Uuid> {
22        if let Ok(uuid) = s.parse::<Uuid>() {
23            return Ok(uuid);
24        }
25        if s.len() >= 8 && s.chars().all(|c| c.is_ascii_hexdigit()) {
26            return match self.resolve_prefix(token, s).await? {
27                Some(uuid) => Ok(uuid),
28                None => Err(RuntimeError::InvalidInput(format!(
29                    "no record matches prefix: {s:?}"
30                ))),
31            };
32        }
33        Err(RuntimeError::InvalidInput(format!(
34            "invalid UUID (expected full UUID or 8+ hex prefix): {s:?}"
35        )))
36    }
37}