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::iter::{FromIterator, IntoIterator};
use crate::buffer::SaslString;
pub struct Mechanisms {
inner: Alloc,
}
enum Alloc {
Rust(String),
Gsasl(SaslString),
}
impl Mechanisms {
pub fn from_sasl(inner: SaslString) -> Self {
Self { inner: Alloc::Gsasl(inner) }
}
pub fn iter(&self) -> impl Iterator<Item=&str> {
let s = match self.inner {
Alloc::Gsasl(ref s) => s.to_str().unwrap(),
Alloc::Rust(ref s) => s,
};
s.split_ascii_whitespace()
}
pub fn as_slice(&self) -> &[u8] {
match self.inner {
Alloc::Gsasl(ref s) => s.to_bytes(),
Alloc::Rust(ref s) => s.as_bytes(),
}
}
pub fn as_raw_ptr(&self) -> *const libc::c_char {
match self.inner {
Alloc::Gsasl(ref s) => s.as_raw_ptr() as *const libc::c_char,
Alloc::Rust(ref s) => s.as_ptr() as *const libc::c_char,
}
}
}
impl<B: AsRef<str>> FromIterator<B> for Mechanisms {
fn from_iter<I: IntoIterator<Item=B>>(iter: I) -> Self {
let mut buf = String::new();
for m in iter {
buf.push_str(m.as_ref());
buf.push(' ');
}
buf.pop();
Self { inner: Alloc::Rust(buf) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_iter_test() {
let mechs = ["PLAIN", "SCRAM-SHA1", "GSSAPI"];
let m: Mechanisms = mechs.iter().collect();
assert_eq!(m.as_slice(), b"PLAIN SCRAM-SHA1 GSSAPI");
}
}