Skip to main content

vck_common/
error.rs

1// SPDX-FileCopyrightText: 2026 JC-Lab <joseph@jc-lab.net>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use alloc::string::String;
6use core::fmt::{Display, Formatter};
7
8pub type VckResult<T> = core::result::Result<T, VckError>;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum VckError {
12    InvalidData(&'static str),
13    ValidationFailed(&'static str),
14    Unsupported(&'static str),
15    NotFound(&'static str),
16    PermissionDenied(&'static str),
17    SizeMismatch { expected: usize, actual: usize },
18    SignatureMismatch,
19    ChecksumMismatch,
20    CryptoFailed(&'static str),
21    MsgpackEncode(String),
22    MsgpackDecode(String),
23    Io(String),
24}
25
26impl Display for VckError {
27    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
28        match self {
29            Self::InvalidData(msg) => write!(f, "invalid data: {msg}"),
30            Self::ValidationFailed(msg) => write!(f, "validation failed: {msg}"),
31            Self::Unsupported(msg) => write!(f, "unsupported: {msg}"),
32            Self::NotFound(msg) => write!(f, "not found: {msg}"),
33            Self::PermissionDenied(msg) => write!(f, "permission denied: {msg}"),
34            Self::SizeMismatch { expected, actual } => {
35                write!(f, "size mismatch: expected {expected}, got {actual}")
36            }
37            Self::SignatureMismatch => f.write_str("signature mismatch"),
38            Self::ChecksumMismatch => f.write_str("checksum mismatch"),
39            Self::CryptoFailed(msg) => write!(f, "cryptography failed: {msg}"),
40            Self::MsgpackEncode(msg) => write!(f, "msgpack encode failed: {msg}"),
41            Self::MsgpackDecode(msg) => write!(f, "msgpack decode failed: {msg}"),
42            Self::Io(msg) => write!(f, "io failed: {msg}"),
43        }
44    }
45}
46
47impl core::error::Error for VckError {}