1pub mod auth;
21pub mod backend;
22pub mod clock;
23pub mod constants;
24pub mod crdt;
25pub mod database;
26pub mod entry;
27pub mod height;
28pub mod instance;
29#[cfg(all(unix, feature = "service"))]
30pub mod service;
31pub mod snapshot;
32pub mod store;
33pub mod sync;
34#[cfg(any(test, feature = "testing"))]
35pub mod testing;
36pub mod transaction;
37pub mod user;
38
39pub use auth::crypto::{PrivateKey, PublicKey};
40pub use clock::{Clock, SystemClock};
41#[cfg(any(test, feature = "testing"))]
42pub use clock::{ClockHold, FixedClock};
43pub use database::{Database, DatabaseKey};
44pub use entry::{Entry, ID};
45pub use height::HeightStrategy;
46pub use instance::{Instance, NewUser, WeakInstance, WriteCallback, WriteEvent, WriteSource};
47pub use snapshot::Snapshot;
48pub use store::{Registered, Store};
49#[cfg(any(test, feature = "testing"))]
50pub use testing::{Cluster, Peer};
51pub use transaction::Transaction;
53
54#[cfg(feature = "y-crdt")]
59pub mod y_crdt {
60 pub use yrs::*;
61}
62
63pub type Result<T, E = Error> = std::result::Result<T, E>;
65
66#[derive(Debug, thiserror::Error)]
74pub enum Error {
75 #[error("I/O error: {0}")]
76 Io(#[from] std::io::Error),
77
78 #[error("Serialization error: {0}")]
79 Serialize(#[from] serde_json::Error),
80
81 #[error(transparent)]
83 Auth(Box<auth::AuthError>),
84
85 #[error(transparent)]
87 Backend(Box<backend::BackendError>),
88
89 #[error(transparent)]
91 Instance(Box<instance::InstanceError>),
92
93 #[error(transparent)]
95 CRDT(Box<crdt::CRDTError>),
96
97 #[error(transparent)]
99 Store(Box<store::StoreError>),
100
101 #[error(transparent)]
103 Transaction(Box<transaction::TransactionError>),
104
105 #[error(transparent)]
107 Sync(Box<sync::SyncError>),
108
109 #[error(transparent)]
111 Entry(Box<entry::EntryError>),
112
113 #[error(transparent)]
115 Id(Box<entry::id::IdError>),
116
117 #[error(transparent)]
119 User(Box<user::UserError>),
120}
121
122impl From<sync::SyncError> for Error {
123 fn from(err: sync::SyncError) -> Self {
124 Error::Sync(Box::new(err))
125 }
126}
127
128impl From<entry::EntryError> for Error {
129 fn from(err: entry::EntryError) -> Self {
130 Error::Entry(Box::new(err))
131 }
132}
133
134impl From<entry::id::IdError> for Error {
135 fn from(err: entry::id::IdError) -> Self {
136 Error::Id(Box::new(err))
137 }
138}
139
140impl From<user::UserError> for Error {
141 fn from(err: user::UserError) -> Self {
142 Error::User(Box::new(err))
143 }
144}
145
146impl Error {
147 pub fn module(&self) -> &'static str {
149 match self {
150 Error::Auth(_) => "auth",
151 Error::Backend(_) => "backend",
152 Error::Instance(_) => "instance",
153 Error::CRDT(_) => "crdt",
154 Error::Store(_) => "store",
155 Error::Transaction(_) => "transaction",
156 Error::Sync(_) => "sync",
157 Error::Entry(_) => "entry",
158 Error::Id(_) => "id",
159 Error::User(_) => "user",
160 Error::Io(_) => "io",
161 Error::Serialize(_) => "serialize",
162 }
163 }
164
165 pub fn is_not_found(&self) -> bool {
167 match self {
168 Error::Auth(auth_err) => auth_err.is_not_found(),
169 Error::Backend(backend_err) => backend_err.is_not_found(),
170 Error::Instance(base_err) => base_err.is_not_found(),
171 Error::CRDT(crdt_err) => crdt_err.is_not_found(),
172 Error::Store(store_err) => store_err.is_not_found(),
173 Error::Sync(sync_err) => sync_err.is_not_found(),
174 Error::User(user_err) => user_err.is_not_found(),
175 _ => false,
176 }
177 }
178
179 pub fn is_permission_denied(&self) -> bool {
181 match self {
182 Error::Auth(auth_err) => auth_err.is_permission_denied(),
183 Error::Transaction(transaction_err) => transaction_err.is_authentication_error(),
184 _ => false,
185 }
186 }
187
188 pub fn is_conflict(&self) -> bool {
190 match self {
191 Error::Instance(base_err) => base_err.is_already_exists(),
192 _ => false,
193 }
194 }
195
196 pub fn is_authentication_error(&self) -> bool {
198 match self {
199 Error::Auth(_) => true,
200 Error::Instance(base_err) => base_err.is_authentication_error(),
201 Error::Transaction(transaction_err) => transaction_err.is_authentication_error(),
202 _ => false,
203 }
204 }
205
206 pub fn is_database_error(&self) -> bool {
208 matches!(self, Error::Backend(_))
209 }
210
211 pub fn is_integrity_error(&self) -> bool {
213 match self {
214 Error::Backend(backend_err) => backend_err.is_integrity_error(),
215 _ => false,
216 }
217 }
218
219 pub fn is_io_error(&self) -> bool {
221 match self {
222 Error::Io(_) => true,
223 Error::Backend(backend_err) => backend_err.is_io_error(),
224 _ => false,
225 }
226 }
227
228 pub fn is_base_database_error(&self) -> bool {
230 matches!(self, Error::Instance(_))
231 }
232
233 pub fn is_validation_error(&self) -> bool {
235 match self {
236 Error::Id(_) => true, Error::Instance(base_err) => base_err.is_validation_error(),
238 Error::Backend(backend_err) => backend_err.is_logical_error(),
239 Error::Transaction(transaction_err) => transaction_err.is_validation_error(),
240 Error::Entry(entry_err) => entry_err.is_validation_error(),
241 _ => false,
242 }
243 }
244
245 pub fn is_operation_error(&self) -> bool {
247 match self {
248 Error::Instance(base_err) => base_err.is_operation_error(),
249 Error::Store(store_err) => store_err.is_operation_error(),
250 Error::Transaction(transaction_err) => transaction_err.is_validation_error(),
251 _ => false,
252 }
253 }
254
255 pub fn is_type_error(&self) -> bool {
257 match self {
258 Error::Store(store_err) => store_err.is_type_error(),
259 _ => false,
260 }
261 }
262
263 pub fn is_crdt_error(&self) -> bool {
265 matches!(self, Error::CRDT(_))
266 }
267
268 pub fn is_crdt_merge_error(&self) -> bool {
270 match self {
271 Error::CRDT(crdt_err) => crdt_err.is_merge_error(),
272 _ => false,
273 }
274 }
275
276 pub fn is_crdt_serialization_error(&self) -> bool {
278 match self {
279 Error::CRDT(crdt_err) => crdt_err.is_serialization_error(),
280 _ => false,
281 }
282 }
283
284 pub fn is_crdt_type_error(&self) -> bool {
286 match self {
287 Error::CRDT(crdt_err) => crdt_err.is_type_error(),
288 _ => false,
289 }
290 }
291
292 pub fn is_store_error(&self) -> bool {
294 matches!(self, Error::Store(_))
295 }
296
297 pub fn is_store_serialization_error(&self) -> bool {
299 match self {
300 Error::Store(store_err) => store_err.is_serialization_error(),
301 _ => false,
302 }
303 }
304
305 pub fn is_store_type_error(&self) -> bool {
307 match self {
308 Error::Store(store_err) => store_err.is_type_error(),
309 _ => false,
310 }
311 }
312
313 pub fn is_already_committed(&self) -> bool {
315 match self {
316 Error::Transaction(transaction_err) => transaction_err.is_already_committed(),
317 _ => false,
318 }
319 }
320
321 pub fn is_entry_error(&self) -> bool {
323 match self {
324 Error::Transaction(transaction_err) => transaction_err.is_entry_error(),
325 Error::Entry(_) => true,
326 _ => false,
327 }
328 }
329
330 pub fn is_id_error(&self) -> bool {
332 matches!(self, Error::Id(_))
333 }
334
335 pub fn is_entry_validation_error(&self) -> bool {
337 match self {
338 Error::Entry(entry_err) => entry_err.is_validation_error(),
339 _ => false,
340 }
341 }
342
343 pub fn is_entry_serialization_error(&self) -> bool {
345 match self {
346 Error::Entry(entry_err) => entry_err.is_serialization_error(),
347 _ => false,
348 }
349 }
350}