Skip to main content

ndr_ffi/
error.rs

1//! Error types for the FFI layer.
2
3use std::fmt;
4
5/// FFI-friendly error type.
6#[derive(Debug, Clone, uniffi::Error)]
7pub enum NdrError {
8    InvalidKey(String),
9    InvalidEvent(String),
10    CryptoFailure(String),
11    StateMismatch(String),
12    Serialization(String),
13    InviteError(String),
14    SessionNotReady(String),
15}
16
17impl fmt::Display for NdrError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            NdrError::InvalidKey(msg) => write!(f, "Invalid key: {}", msg),
21            NdrError::InvalidEvent(msg) => write!(f, "Invalid event: {}", msg),
22            NdrError::CryptoFailure(msg) => write!(f, "Crypto failure: {}", msg),
23            NdrError::StateMismatch(msg) => write!(f, "State mismatch: {}", msg),
24            NdrError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
25            NdrError::InviteError(msg) => write!(f, "Invite error: {}", msg),
26            NdrError::SessionNotReady(msg) => write!(f, "Session not ready: {}", msg),
27        }
28    }
29}
30
31impl std::error::Error for NdrError {}
32
33impl From<nostr_double_ratchet::Error> for NdrError {
34    fn from(err: nostr_double_ratchet::Error) -> Self {
35        match err {
36            nostr_double_ratchet::Error::Encryption(msg) => NdrError::CryptoFailure(msg),
37            nostr_double_ratchet::Error::Decryption(msg) => NdrError::CryptoFailure(msg),
38            nostr_double_ratchet::Error::InvalidHeader => {
39                NdrError::InvalidEvent("Invalid header".into())
40            }
41            nostr_double_ratchet::Error::TooManySkippedMessages => {
42                NdrError::StateMismatch("Too many skipped messages".into())
43            }
44            nostr_double_ratchet::Error::NotInitiator => {
45                NdrError::SessionNotReady("Not initiator".into())
46            }
47            nostr_double_ratchet::Error::EventMustBeUnsigned => {
48                NdrError::InvalidEvent("Event must be unsigned".into())
49            }
50            nostr_double_ratchet::Error::FailedToDecryptHeader => {
51                NdrError::CryptoFailure("Failed to decrypt header".into())
52            }
53            nostr_double_ratchet::Error::InvalidEvent(msg) => NdrError::InvalidEvent(msg),
54            nostr_double_ratchet::Error::Serialization(msg) => NdrError::Serialization(msg),
55            nostr_double_ratchet::Error::Storage(msg) => NdrError::Serialization(msg),
56            nostr_double_ratchet::Error::SessionNotReady => {
57                NdrError::SessionNotReady("Session not ready".into())
58            }
59            nostr_double_ratchet::Error::DeviceIdRequired => {
60                NdrError::InviteError("Device ID required".into())
61            }
62            nostr_double_ratchet::Error::Invite(msg) => NdrError::InviteError(msg),
63            nostr_double_ratchet::Error::Json(e) => NdrError::Serialization(e.to_string()),
64            nostr_double_ratchet::Error::Hex(e) => NdrError::InvalidKey(e.to_string()),
65            nostr_double_ratchet::Error::NostrKey(e) => NdrError::InvalidKey(e.to_string()),
66            nostr_double_ratchet::Error::Nostr(e) => NdrError::InvalidEvent(e.to_string()),
67            nostr_double_ratchet::Error::Nip44(e) => NdrError::CryptoFailure(e.to_string()),
68        }
69    }
70}
71
72impl From<serde_json::Error> for NdrError {
73    fn from(err: serde_json::Error) -> Self {
74        NdrError::Serialization(err.to_string())
75    }
76}
77
78impl From<hex::FromHexError> for NdrError {
79    fn from(err: hex::FromHexError) -> Self {
80        NdrError::InvalidKey(err.to_string())
81    }
82}