|
|
|
@ -0,0 +1,55 @@ |
|
|
|
//! Defines the `Credentials` struct, for use when logging in to a server.
|
|
|
|
|
|
|
|
/// Credentials for logging in a client to a server.
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct Credentials {
|
|
|
|
user_name: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Credentials {
|
|
|
|
/// Attempts to build credentials.
|
|
|
|
///
|
|
|
|
/// Returns `None` if `password` is empty, `Some(credentials)` otherwise.
|
|
|
|
pub fn new(user_name: String, password: String) -> Option<Credentials> {
|
|
|
|
if password.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Credentials {
|
|
|
|
user_name,
|
|
|
|
password,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The user name to log in as.
|
|
|
|
pub fn user_name(&self) -> &str {
|
|
|
|
&self.user_name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The password to log in with.
|
|
|
|
pub fn password(&self) -> &str {
|
|
|
|
&self.password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Credentials;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_password() {
|
|
|
|
assert_eq!(Credentials::new("alice".to_string(), String::new()), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_success() {
|
|
|
|
let user_name = "alice".to_string();
|
|
|
|
let password = "sekrit".to_string();
|
|
|
|
|
|
|
|
let credentials = Credentials::new(user_name, password).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(credentials.user_name(), "alice");
|
|
|
|
assert_eq!(credentials.password(), "sekrit");
|
|
|
|
}
|
|
|
|
}
|