Skip to main content

pask_wire/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 Wilder Management Inc. (d/b/a Wilder Robotics) <rob@wilder-robotics.com>
3// pask-wire is licensed Apache-2.0. No commercial agreement is required to use,
4// modify or redistribute it; see LICENSING.md in the workspace root.
5
6use alloc::string::String;
7use core::fmt;
8
9/// Errors returned while parsing, producing, or verifying a statement.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum Error {
12    /// JSON parsing or data-model conversion failed.
13    Json(String),
14    /// JCS serialization failed.
15    Jcs(String),
16    /// The COSE envelope is malformed.
17    Cose(&'static str),
18    /// A signature could not be created or verified.
19    Signature,
20    /// A protected-header requirement was not met.
21    Header(&'static str),
22    /// A payload profile requirement was not met.
23    Validation(&'static str),
24    /// The embedded payload bytes are not their JCS serialization.
25    NonCanonicalPayload,
26    /// An attached SCITT Receipt or its inclusion proof is unusable.
27    ///
28    /// Distinct from [`Self::Signature`], which is reserved for a signature
29    /// that was checked and did not verify. A relying party needs to tell a
30    /// Receipt it could not read from one it read and disbelieved.
31    Receipt(&'static str),
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::Json(message) => write!(formatter, "JSON error: {message}"),
38            Self::Jcs(message) => write!(formatter, "JCS error: {message}"),
39            Self::Cose(message) => write!(formatter, "COSE error: {message}"),
40            Self::Signature => formatter.write_str("signature error"),
41            Self::Header(message) => write!(formatter, "protected-header error: {message}"),
42            Self::Validation(message) => write!(formatter, "payload validation error: {message}"),
43            Self::NonCanonicalPayload => formatter.write_str("payload is not JCS canonical"),
44            Self::Receipt(message) => write!(formatter, "attached receipt error: {message}"),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl std::error::Error for Error {}
51
52/// Result type used by this crate.
53pub type Result<T> = core::result::Result<T, Error>;