From 74fb4fd9ad1bb143121c74492eeb2845b34cf7a0 Mon Sep 17 00:00:00 2001 From: lumi Date: Fri, 24 Feb 2017 18:29:10 +0100 Subject: [PATCH] clean up SASL code --- src/sasl.rs | 60 -------------------------------- src/sasl/mechanisms/anonymous.rs | 15 ++++++++ src/sasl/mechanisms/mod.rs | 7 ++++ src/sasl/mechanisms/plain.rs | 30 ++++++++++++++++ src/sasl/mod.rs | 18 ++++++++++ 5 files changed, 70 insertions(+), 60 deletions(-) delete mode 100644 src/sasl.rs create mode 100644 src/sasl/mechanisms/anonymous.rs create mode 100644 src/sasl/mechanisms/mod.rs create mode 100644 src/sasl/mechanisms/plain.rs create mode 100644 src/sasl/mod.rs diff --git a/src/sasl.rs b/src/sasl.rs deleted file mode 100644 index 6a047ce..0000000 --- a/src/sasl.rs +++ /dev/null @@ -1,60 +0,0 @@ -//! Provides the `SaslMechanism` trait and some implementations. - -pub trait SaslMechanism { - /// The name of the mechanism. - fn name() -> &'static str; - - /// Provides initial payload of the SASL mechanism. - fn initial(&mut self) -> Vec { - Vec::new() - } - - /// Creates a response to the SASL challenge. - fn response(&mut self, _challenge: &[u8]) -> Vec { - Vec::new() - } -} - -/// A few SASL mechanisms. -pub mod mechanisms { - use super::SaslMechanism; - - pub struct Anonymous; - - impl Anonymous { - pub fn new() -> Anonymous { - Anonymous - } - } - - impl SaslMechanism for Anonymous { - fn name() -> &'static str { "ANONYMOUS" } - } - - pub struct Plain { - name: String, - password: String, - } - - impl Plain { - pub fn new, P: Into>(name: N, password: P) -> Plain { - Plain { - name: name.into(), - password: password.into(), - } - } - } - - impl SaslMechanism for Plain { - fn name() -> &'static str { "PLAIN" } - - fn initial(&mut self) -> Vec { - let mut auth = Vec::new(); - auth.push(0); - auth.extend(self.name.bytes()); - auth.push(0); - auth.extend(self.password.bytes()); - auth - } - } -} diff --git a/src/sasl/mechanisms/anonymous.rs b/src/sasl/mechanisms/anonymous.rs new file mode 100644 index 0000000..c64c5e9 --- /dev/null +++ b/src/sasl/mechanisms/anonymous.rs @@ -0,0 +1,15 @@ +//! Provides the SASL "ANONYMOUS" mechanism. + +use sasl::SaslMechanism; + +pub struct Anonymous; + +impl Anonymous { + pub fn new() -> Anonymous { + Anonymous + } +} + +impl SaslMechanism for Anonymous { + fn name() -> &'static str { "ANONYMOUS" } +} diff --git a/src/sasl/mechanisms/mod.rs b/src/sasl/mechanisms/mod.rs new file mode 100644 index 0000000..64a47ec --- /dev/null +++ b/src/sasl/mechanisms/mod.rs @@ -0,0 +1,7 @@ +///! Provides a few SASL mechanisms. + +mod anonymous; +mod plain; + +pub use self::anonymous::Anonymous; +pub use self::plain::Plain; diff --git a/src/sasl/mechanisms/plain.rs b/src/sasl/mechanisms/plain.rs new file mode 100644 index 0000000..ea43472 --- /dev/null +++ b/src/sasl/mechanisms/plain.rs @@ -0,0 +1,30 @@ +//! Provides the SASL "PLAIN" mechanism. + +use sasl::SaslMechanism; + +pub struct Plain { + name: String, + password: String, +} + +impl Plain { + pub fn new, P: Into>(name: N, password: P) -> Plain { + Plain { + name: name.into(), + password: password.into(), + } + } +} + +impl SaslMechanism for Plain { + fn name() -> &'static str { "PLAIN" } + + fn initial(&mut self) -> Vec { + let mut auth = Vec::new(); + auth.push(0); + auth.extend(self.name.bytes()); + auth.push(0); + auth.extend(self.password.bytes()); + auth + } +} diff --git a/src/sasl/mod.rs b/src/sasl/mod.rs new file mode 100644 index 0000000..6f6aa0b --- /dev/null +++ b/src/sasl/mod.rs @@ -0,0 +1,18 @@ +//! Provides the `SaslMechanism` trait and some implementations. + +pub trait SaslMechanism { + /// The name of the mechanism. + fn name() -> &'static str; + + /// Provides initial payload of the SASL mechanism. + fn initial(&mut self) -> Vec { + Vec::new() + } + + /// Creates a response to the SASL challenge. + fn response(&mut self, _challenge: &[u8]) -> Vec { + Vec::new() + } +} + +pub mod mechanisms;