Skip to main content

csaf_core/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 Pierre Gronau, ndaal in Cologne
3
4//! Error types for the CSAF core crate.
5
6use thiserror::Error;
7
8/// Central error type for CSAF core operations.
9#[derive(Debug, Error)]
10pub enum CsafError {
11    /// Storage (redb) error.
12    #[error("Storage error: {0}")]
13    Storage(String),
14
15    /// Database (SQLite) error.
16    #[error("Database error: {0}")]
17    Database(#[from] rusqlite::Error),
18
19    /// Serialization/deserialization error.
20    #[error("Serialization error: {0}")]
21    Serialization(#[from] serde_json::Error),
22
23    /// CSAF validation failed with one or more errors.
24    #[error("Validation failed: {0}")]
25    Validation(String),
26
27    /// File I/O error.
28    #[error("I/O error: {0}")]
29    Io(#[from] std::io::Error),
30
31    /// Configuration error.
32    #[error("Configuration error: {0}")]
33    Config(String),
34
35    /// Document not found.
36    #[error("Document not found: {0}")]
37    NotFound(String),
38
39    /// Duplicate document.
40    #[error("Document already exists: {0}")]
41    Duplicate(String),
42
43    /// Import error.
44    #[error("Import error: {0}")]
45    Import(String),
46
47    /// Export error.
48    #[error("Export error: {0}")]
49    Export(String),
50}
51
52impl From<redb::Error> for CsafError {
53    fn from(e: redb::Error) -> Self {
54        Self::Storage(e.to_string())
55    }
56}
57
58impl From<redb::DatabaseError> for CsafError {
59    fn from(e: redb::DatabaseError) -> Self {
60        Self::Storage(e.to_string())
61    }
62}
63
64impl From<redb::TableError> for CsafError {
65    fn from(e: redb::TableError) -> Self {
66        Self::Storage(e.to_string())
67    }
68}
69
70impl From<redb::TransactionError> for CsafError {
71    fn from(e: redb::TransactionError) -> Self {
72        Self::Storage(e.to_string())
73    }
74}
75
76impl From<redb::CommitError> for CsafError {
77    fn from(e: redb::CommitError) -> Self {
78        Self::Storage(e.to_string())
79    }
80}
81
82impl From<redb::StorageError> for CsafError {
83    fn from(e: redb::StorageError) -> Self {
84        Self::Storage(e.to_string())
85    }
86}
87
88/// Result type alias using `CsafError`.
89pub type Result<T> = std::result::Result<T, CsafError>;