Use error structs for errors instead of plain strings.

This commit is contained in:
Emmanuel Gil Peyrot 2020-05-15 13:48:27 +02:00
commit 7fd6923464
10 changed files with 357 additions and 99 deletions

View file

@ -1,6 +1,6 @@
//! Provides the SASL "PLAIN" mechanism.
use crate::client::Mechanism;
use crate::client::{Mechanism, MechanismError};
use crate::common::{Credentials, Identity, Password, Secret};
/// A struct for the SASL PLAIN mechanism.
@ -27,15 +27,15 @@ impl Mechanism for Plain {
"PLAIN"
}
fn from_credentials(credentials: Credentials) -> Result<Plain, String> {
fn from_credentials(credentials: Credentials) -> Result<Plain, MechanismError> {
if let Secret::Password(Password::Plain(password)) = credentials.secret {
if let Identity::Username(username) = credentials.identity {
Ok(Plain::new(username, password))
} else {
Err("PLAIN requires a username".to_owned())
Err(MechanismError::PlainRequiresUsername)
}
} else {
Err("PLAIN requires a plaintext password".to_owned())
Err(MechanismError::PlainRequiresPlaintextPassword)
}
}