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
use std::fmt::Debug;
#[derive(Clone, PartialEq)]
pub struct SqlServerAuth {
user: String,
password: String,
}
impl SqlServerAuth {
pub(crate) fn user(&self) -> &str {
&self.user
}
pub(crate) fn password(&self) -> &str {
&self.password
}
}
impl Debug for SqlServerAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqlServerAuth")
.field("user", &self.user)
.field("password", &"<HIDDEN>")
.finish()
}
}
#[derive(Clone, PartialEq)]
#[cfg(any(windows, doc))]
#[cfg_attr(feature = "docs", doc(windows))]
pub struct WindowsAuth {
pub(crate) user: String,
pub(crate) password: String,
pub(crate) domain: Option<String>,
}
#[cfg(any(windows, doc))]
#[cfg_attr(feature = "docs", doc(windows))]
impl Debug for WindowsAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WindowsAuth")
.field("user", &self.user)
.field("password", &"<HIDDEN>")
.field("domain", &self.domain)
.finish()
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AuthMethod {
SqlServer(SqlServerAuth),
#[cfg(any(windows, doc))]
#[cfg_attr(feature = "docs", doc(cfg(windows)))]
Windows(WindowsAuth),
#[cfg(any(windows, all(unix, feature = "integrated-auth-gssapi"), doc))]
#[cfg_attr(
feature = "docs",
doc(cfg(any(windows, all(unix, feature = "integrated-auth-gssapi"))))
)]
Integrated,
#[doc(hidden)]
None,
}
impl AuthMethod {
pub fn sql_server(user: impl ToString, password: impl ToString) -> Self {
Self::SqlServer(SqlServerAuth {
user: user.to_string(),
password: password.to_string(),
})
}
#[cfg(any(windows, doc))]
#[cfg_attr(feature = "docs", doc(cfg(windows)))]
pub fn windows(user: impl AsRef<str>, password: impl ToString) -> Self {
let (domain, user) = match user.as_ref().find("\\") {
Some(idx) => (Some(&user.as_ref()[..idx]), &user.as_ref()[idx + 1..]),
_ => (None, user.as_ref()),
};
Self::Windows(WindowsAuth {
user: user.to_string(),
password: password.to_string(),
domain: domain.map(|s| s.to_string()),
})
}
}