Browse Source

Introduce Credentials struct.

wip
Titouan Rigoudy 4 years ago
parent
commit
a240c83009
2 changed files with 57 additions and 0 deletions
  1. +55
    -0
      src/proto/server/credentials.rs
  2. +2
    -0
      src/proto/server/mod.rs

+ 55
- 0
src/proto/server/credentials.rs View File

@ -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");
}
}

+ 2
- 0
src/proto/server/mod.rs View File

@ -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::*;

Loading…
Cancel
Save