Skip to main content

overdrive/
result.rs

1//! SDK-specific error types for OverDrive InCode SDK
2//! 
3//! Provides clean, user-friendly error types that wrap the internal engine errors.
4
5use std::fmt;
6
7/// SDK Result type
8pub type SdkResult<T> = std::result::Result<T, SdkError>;
9
10/// SDK Error types — clean, user-friendly errors
11#[derive(Debug)]
12pub enum SdkError {
13    /// Database file not found
14    DatabaseNotFound(String),
15    /// Database already exists
16    DatabaseAlreadyExists(String),
17    /// Table not found
18    TableNotFound(String),
19    /// Table already exists
20    TableAlreadyExists(String),
21    /// Document/record not found
22    DocumentNotFound(String),
23    /// Invalid SQL query
24    InvalidQuery(String),
25    /// I/O error (file operations)
26    IoError(std::io::Error),
27    /// Serialization/deserialization error
28    SerializationError(String),
29    /// Constraint violation (unique, foreign key, etc.)
30    ConstraintViolation(String),
31    /// Transaction error
32    TransactionError(String),
33    /// Database is closed
34    DatabaseClosed,
35    /// Security error — key missing, permission denied, injection detected
36    SecurityError(String),
37    /// Backup error — failed to copy or sync database files
38    BackupError(String),
39    /// Internal engine error
40    Internal(String),
41}
42
43impl fmt::Display for SdkError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            SdkError::DatabaseNotFound(p) => write!(f, "Database not found: {}", p),
47            SdkError::DatabaseAlreadyExists(p) => write!(f, "Database already exists: {}", p),
48            SdkError::TableNotFound(t) => write!(f, "Table not found: {}", t),
49            SdkError::TableAlreadyExists(t) => write!(f, "Table already exists: {}", t),
50            SdkError::DocumentNotFound(id) => write!(f, "Document not found: {}", id),
51            SdkError::InvalidQuery(q) => write!(f, "Invalid query: {}", q),
52            SdkError::IoError(e) => write!(f, "I/O error: {}", e),
53            SdkError::SerializationError(e) => write!(f, "Serialization error: {}", e),
54            SdkError::ConstraintViolation(c) => write!(f, "Constraint violation: {}", c),
55            SdkError::TransactionError(e) => write!(f, "Transaction error: {}", e),
56            SdkError::DatabaseClosed => write!(f, "Database is closed"),
57            SdkError::SecurityError(e) => write!(f, "Security error: {}", e),
58            SdkError::BackupError(e) => write!(f, "Backup error: {}", e),
59            SdkError::Internal(e) => write!(f, "Internal error: {}", e),
60        }
61    }
62}
63
64impl std::error::Error for SdkError {}
65
66impl From<std::io::Error> for SdkError {
67    fn from(e: std::io::Error) -> Self {
68        SdkError::IoError(e)
69    }
70}
71
72impl From<String> for SdkError {
73    fn from(e: String) -> Self {
74        SdkError::Internal(e)
75    }
76}
77
78impl From<serde_json::Error> for SdkError {
79    fn from(e: serde_json::Error) -> Self {
80        SdkError::SerializationError(e.to_string())
81    }
82}