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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::{convert::Infallible, fmt::Debug};
use async_trait::async_trait;
use bonsaidb_core::{
connection::Session,
permissions::PermissionDenied,
schema::{InsertError, InvalidNameError},
};
use crate::{server::ConnectedClient, CustomServer, Error, ServerConfiguration};
#[async_trait]
pub trait Backend: Debug + Send + Sync + Sized + 'static {
type Error: std::error::Error + Send + Sync;
type ClientData: Send + Sync + Debug;
#[allow(unused_variables)]
fn configure(
config: ServerConfiguration<Self>,
) -> Result<ServerConfiguration<Self>, BackendError<Self::Error>> {
Ok(config)
}
#[allow(unused_variables)]
async fn initialize(server: &CustomServer<Self>) -> Result<(), BackendError<Self::Error>> {
Ok(())
}
#[allow(unused_variables)]
#[must_use]
async fn client_connected(
client: &ConnectedClient<Self>,
server: &CustomServer<Self>,
) -> Result<ConnectionHandling, BackendError<Self::Error>> {
log::info!(
"{:?} client connected from {:?}",
client.transport(),
client.address()
);
Ok(ConnectionHandling::Accept)
}
#[allow(unused_variables)]
async fn client_disconnected(
client: ConnectedClient<Self>,
server: &CustomServer<Self>,
) -> Result<(), BackendError<Self::Error>> {
log::info!(
"{:?} client disconnected ({:?})",
client.transport(),
client.address()
);
Ok(())
}
#[allow(unused_variables)]
async fn client_authenticated(
client: ConnectedClient<Self>,
session: &Session,
server: &CustomServer<Self>,
) -> Result<(), BackendError<Self::Error>> {
log::info!(
"{:?} client authenticated as user: {:?}",
client.transport(),
session.identity
);
Ok(())
}
}
#[cfg_attr(feature = "cli", derive(clap::Subcommand))]
#[derive(Debug)]
pub enum NoBackend {}
impl Backend for NoBackend {
type Error = Infallible;
type ClientData = ();
}
pub enum ConnectionHandling {
Accept,
Reject,
}
#[derive(thiserror::Error, Debug)]
pub enum BackendError<E = Infallible> {
#[error("backend error: {0}")]
Backend(E),
#[error("server error: {0}")]
Server(#[from] Error),
}
impl<E> From<PermissionDenied> for BackendError<E> {
fn from(permission_denied: PermissionDenied) -> Self {
Self::Server(Error::from(permission_denied))
}
}
impl<E> From<bonsaidb_core::Error> for BackendError<E> {
fn from(err: bonsaidb_core::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<E> From<bonsaidb_local::Error> for BackendError<E> {
fn from(err: bonsaidb_local::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<E> From<std::io::Error> for BackendError<E> {
fn from(err: std::io::Error) -> Self {
Self::Server(Error::from(err))
}
}
impl<E> From<InvalidNameError> for BackendError<E> {
fn from(err: InvalidNameError) -> Self {
Self::Server(Error::from(err))
}
}
#[cfg(feature = "websockets")]
impl<E> From<bincode::Error> for BackendError<E> {
fn from(other: bincode::Error) -> Self {
Self::Server(Error::from(bonsaidb_local::Error::from(other)))
}
}
impl<E> From<pot::Error> for BackendError<E> {
fn from(other: pot::Error) -> Self {
Self::Server(Error::from(bonsaidb_local::Error::from(other)))
}
}
impl<T, E> From<InsertError<T>> for BackendError<E> {
fn from(error: InsertError<T>) -> Self {
Self::Server(Error::from(error.error))
}
}