From a240c8300965adb65887981768ab7832c5682a49 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Fri, 22 Jan 2021 18:00:29 +0100 Subject: [PATCH] Introduce Credentials struct. --- src/proto/server/credentials.rs | 55 +++++++++++++++++++++++++++++++++ src/proto/server/mod.rs | 2 ++ 2 files changed, 57 insertions(+) create mode 100644 src/proto/server/credentials.rs diff --git a/src/proto/server/credentials.rs b/src/proto/server/credentials.rs new file mode 100644 index 0000000..b6e5dbb --- /dev/null +++ b/src/proto/server/credentials.rs @@ -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 { + 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"); + } +} diff --git a/src/proto/server/mod.rs b/src/proto/server/mod.rs index 74eb3a6..3706b30 100644 --- a/src/proto/server/mod.rs +++ b/src/proto/server/mod.rs @@ -1,9 +1,11 @@ mod client; mod constants; +mod credentials; mod request; mod response; #[cfg(test)] mod testing; +pub use self::credentials::Credentials; pub use self::request::*; pub use self::response::*;