1use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6pub type TalonResult<T> = Result<T, TalonError>;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "kebab-case")]
12pub enum ErrorCode {
13 InvalidScope,
15 InvalidWhere,
17 InvalidSince,
19 DbBusy,
21 DbCorrupt,
23 NotIndexed,
25 Internal,
27}
28
29impl std::fmt::Display for ErrorCode {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match self {
32 Self::InvalidScope => write!(f, "invalid-scope"),
33 Self::InvalidWhere => write!(f, "invalid-where"),
34 Self::InvalidSince => write!(f, "invalid-since"),
35 Self::DbBusy => write!(f, "db-busy"),
36 Self::DbCorrupt => write!(f, "db-corrupt"),
37 Self::NotIndexed => write!(f, "not-indexed"),
38 Self::Internal => write!(f, "internal"),
39 }
40 }
41}
42
43#[derive(Debug, Error)]
45#[non_exhaustive]
46pub enum TalonError {
47 #[error("{feature} is not implemented yet")]
49 NotImplemented {
50 feature: &'static str,
52 },
53
54 #[error("invalid input for {field}: {message}")]
56 InvalidInput {
57 field: &'static str,
59 message: String,
61 },
62
63 #[error("scope '{name}' is not declared in config")]
65 InvalidScope {
66 name: String,
68 },
69
70 #[error("invalid --where filter: {message}")]
72 InvalidWhere {
73 message: String,
75 },
76
77 #[error("invalid --since timestamp: {message}")]
79 InvalidSince {
80 message: String,
82 },
83
84 #[error("database busy: another sync is in progress")]
86 DbBusy,
87
88 #[error("database corrupt: {message}")]
90 DbCorrupt {
91 message: String,
93 },
94
95 #[error("not indexed: vault '{path}' has no index")]
97 NotIndexed {
98 path: String,
100 },
101
102 #[error("config error: {message}")]
104 Config {
105 message: String,
107 },
108
109 #[error("internal error: {message}")]
111 Internal {
112 message: String,
114 },
115
116 #[error("sqlite error in {context}: {source}")]
118 Sqlite {
119 context: &'static str,
121 #[source]
123 source: rusqlite::Error,
124 },
125}
126
127impl TalonError {
128 #[must_use]
130 pub const fn code(&self) -> ErrorCode {
131 match self {
132 Self::InvalidScope { .. } => ErrorCode::InvalidScope,
133 Self::InvalidWhere { .. } => ErrorCode::InvalidWhere,
134 Self::InvalidSince { .. } => ErrorCode::InvalidSince,
135 Self::DbBusy => ErrorCode::DbBusy,
136 Self::DbCorrupt { .. } => ErrorCode::DbCorrupt,
137 Self::NotIndexed { .. } => ErrorCode::NotIndexed,
138 Self::Internal { .. }
139 | Self::NotImplemented { .. }
140 | Self::InvalidInput { .. }
141 | Self::Config { .. }
142 | Self::Sqlite { .. } => ErrorCode::Internal,
143 }
144 }
145}