Skip to main content

ankurah_proto/
error.rs

1#[derive(Debug)]
2pub enum DecodeError {
3    NotStringValue,
4    InvalidBase64(base64::DecodeError),
5    InvalidLength,
6    InvalidUlid,
7    InvalidFallback,
8    InvalidFormat,
9    Other(anyhow::Error),
10}
11
12impl std::fmt::Display for DecodeError {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            DecodeError::NotStringValue => write!(f, "Not a string value"),
16            DecodeError::InvalidBase64(e) => write!(f, "Invalid Base64: {}", e),
17            DecodeError::InvalidLength => write!(f, "Invalid Length"),
18            DecodeError::InvalidUlid => write!(f, "Invalid ULID"),
19            DecodeError::InvalidFallback => write!(f, "Invalid Fallback"),
20            DecodeError::Other(e) => write!(f, "Other: {}", e),
21            DecodeError::InvalidFormat => write!(f, "Invalid Format"),
22        }
23    }
24}
25
26impl std::error::Error for DecodeError {}
27
28impl From<base64::DecodeError> for DecodeError {
29    fn from(e: base64::DecodeError) -> Self { DecodeError::InvalidBase64(e) }
30}
31
32/// Simplified error type for UniFFI export (no complex inner types)
33#[derive(Debug, thiserror::Error)]
34#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
35#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
36pub enum IdParseError {
37    #[error("Invalid base64 encoding")]
38    InvalidBase64,
39    #[error("Invalid ID length")]
40    InvalidLength,
41    #[error("Invalid format: {0}")]
42    InvalidFormat(String),
43}
44
45impl From<DecodeError> for IdParseError {
46    fn from(e: DecodeError) -> Self {
47        match e {
48            DecodeError::InvalidBase64(_) => IdParseError::InvalidBase64,
49            DecodeError::InvalidLength => IdParseError::InvalidLength,
50            _ => IdParseError::InvalidFormat(e.to_string()),
51        }
52    }
53}