1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::sync::Arc;
use pliantdb_core::networking::{self, fabruic};
use pliantdb_local::core::{self, schema};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid database name: {0}")]
InvalidDatabaseName(String),
#[error("database '{0}' was not found")]
DatabaseNotFound(String),
#[error("a database with name '{0}' already exists")]
DatabaseNameAlreadyTaken(String),
#[error("a networking error occurred: '{0}'")]
Transport(#[from] fabruic::Error),
#[cfg(feature = "websockets")]
#[error("a websocket error occurred: '{0}'")]
Websocket(#[from] tokio_tungstenite::tungstenite::Error),
#[error("a networking error occurred: '{0}'")]
Io(#[from] tokio::io::Error),
#[error("an error occurred processing a request: '{0}'")]
Request(Arc<anyhow::Error>),
#[error(
"database '{database_name}' was created with schema '{stored_schema}', not '{schema}'"
)]
SchemaMismatch {
database_name: String,
schema: schema::Id,
stored_schema: schema::Id,
},
#[error("schema '{0}' was already registered")]
SchemaAlreadyRegistered(schema::Id),
#[error("error from core {0}")]
Core(#[from] core::Error),
#[error("an error occurred interacting with a database: {0}")]
Storage(#[from] pliantdb_local::Error),
}
impl From<Error> for core::Error {
fn from(other: Error) -> Self {
#[allow(clippy::clippy::match_wildcard_for_single_variants)]
match other {
Error::Storage(storage) => Self::Storage(storage.to_string()),
Error::Core(core) => core,
Error::Io(io) => Self::Io(io.to_string()),
Error::Transport(networking) => Self::Transport(networking.to_string()),
#[cfg(feature = "websockets")]
Error::Websocket(err) => Self::Websocket(err.to_string()),
Error::InvalidDatabaseName(name) => {
Self::Networking(networking::Error::InvalidDatabaseName(name))
}
Error::DatabaseNotFound(name) => {
Self::Networking(networking::Error::DatabaseNotFound(name))
}
Error::DatabaseNameAlreadyTaken(name) => {
Self::Networking(networking::Error::DatabaseNameAlreadyTaken(name))
}
Error::Request(err) => Self::Server(err.to_string()),
Error::SchemaMismatch {
database_name,
schema,
stored_schema,
} => Self::Networking(networking::Error::SchemaMismatch {
database_name,
schema,
stored_schema,
}),
Error::SchemaAlreadyRegistered(id) => {
Self::Networking(networking::Error::SchemaAlreadyRegistered(id))
}
}
}
}
pub trait ResultExt<R> {
fn map_err_to_core(self) -> Result<R, core::Error>
where
Self: Sized;
}
impl<R> ResultExt<R> for Result<R, Error> {
fn map_err_to_core(self) -> Result<R, core::Error>
where
Self: Sized,
{
self.map_err(core::Error::from)
}
}
#[cfg(feature = "websockets")]
impl From<bincode::Error> for Error {
fn from(other: bincode::Error) -> Self {
Self::Core(core::Error::Websocket(format!(
"error deserializing message: {:?}",
other
)))
}
}