forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.rs
More file actions
82 lines (70 loc) · 2.22 KB
/
Copy pathencode.rs
File metadata and controls
82 lines (70 loc) · 2.22 KB
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
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use crate::database::HasOutput;
use crate::Database;
/// Type returned from [`Encode::encode`] that indicates if the value encoded is the SQL `null` or not.
pub enum IsNull {
/// The value is the SQL `null`.
///
/// No data was written to the output buffer.
///
Yes,
/// The value is not the SQL `null`.
///
/// This does not mean that any data was written to the output buffer. For example,
/// an empty string has no data, but is not null.
///
No,
}
/// A type that can be encoded into a SQL value.
pub trait Encode<Db: Database>: Send + Sync {
/// Encode this value into the specified SQL type.
fn encode(&self, ty: &Db::TypeInfo, out: &mut <Db as HasOutput<'_>>::Output) -> Result;
}
impl<T: Encode<Db>, Db: Database> Encode<Db> for &T {
#[inline]
fn encode(&self, ty: &Db::TypeInfo, out: &mut <Db as HasOutput<'_>>::Output) -> Result {
(*self).encode(ty, out)
}
}
/// Errors which can occur while encoding a SQL value.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
TypeNotCompatible {
rust_type_name: &'static str,
sql_type_name: &'static str,
},
/// A general error raised while encoding a value.
Custom(Box<dyn StdError + Send + Sync>),
}
impl Error {
#[doc(hidden)]
pub fn msg<D: Display>(msg: D) -> Self {
Self::Custom(msg.to_string().into())
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::TypeNotCompatible { rust_type_name, sql_type_name } => {
write!(
f,
"Rust type `{}` is not compatible with SQL type `{}`",
rust_type_name, sql_type_name
)
}
Self::Custom(error) => {
write!(f, "{}", error)
}
}
}
}
// noinspection DuplicatedCode
impl<E: StdError + Send + Sync + 'static> From<E> for Error {
fn from(error: E) -> Self {
Self::Custom(Box::new(error))
}
}
/// A specialized result type representing the result of encoding a SQL value.
pub type Result = std::result::Result<IsNull, Error>;