Skip to main content

world_id_proof/artifacts/
error.rs

1/// Identifies one of the ZK artifacts a [`crate::artifacts::ZkArtifactSource`] can provide.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub enum ZkArtifactKind {
4    QueryMaterial,
5    NullifierMaterial,
6    OwnershipProver,
7    OwnershipVerifier,
8}
9
10impl std::fmt::Display for ZkArtifactKind {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        let name = match self {
13            Self::QueryMaterial => "query proof material",
14            Self::NullifierMaterial => "nullifier proof material",
15            Self::OwnershipProver => "ownership prover",
16            Self::OwnershipVerifier => "ownership verifier",
17        };
18        f.write_str(name)
19    }
20}
21
22/// Error returned by [`crate::artifacts::ZkArtifactSource`] implementations.
23#[derive(Debug, thiserror::Error)]
24pub enum ZkArtifactError {
25    /// The source is not configured to provide this artifact (e.g. no path was
26    /// set on a filesystem source, or a dummy source was used).
27    #[error(
28        "{kind} is not provided by this ZK artifact source{}",
29        fmt_detail(detail)
30    )]
31    NotProvided {
32        kind: ZkArtifactKind,
33        /// Optional source-specific hint on how to make the artifact available.
34        detail: Option<String>,
35    },
36    /// The artifact can never be provided on this target/feature combination.
37    #[error("{kind} is unavailable: {reason}")]
38    Unavailable {
39        kind: ZkArtifactKind,
40        reason: &'static str,
41    },
42    /// The artifact exists but its bytes could not be read, parsed, or verified.
43    #[error("failed to load {kind}: {message}")]
44    Load {
45        kind: ZkArtifactKind,
46        message: String,
47    },
48}
49
50impl ZkArtifactError {
51    /// The artifact this error refers to.
52    #[must_use]
53    pub fn kind(&self) -> ZkArtifactKind {
54        match self {
55            Self::NotProvided { kind, .. }
56            | Self::Unavailable { kind, .. }
57            | Self::Load { kind, .. } => *kind,
58        }
59    }
60
61    /// Wraps an underlying load failure, preserving its full error chain in the message.
62    pub fn load(kind: ZkArtifactKind, error: impl Into<eyre::Report>) -> Self {
63        Self::Load {
64            kind,
65            message: format!("{:#}", error.into()),
66        }
67    }
68}
69
70fn fmt_detail(detail: &Option<String>) -> String {
71    detail
72        .as_deref()
73        .map(|d| format!(" ({d})"))
74        .unwrap_or_default()
75}