50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::user::domain::UserError;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub struct SigninUserCommand<'a> {
|
|
login: &'a str,
|
|
password: &'a str,
|
|
}
|
|
|
|
impl<'a> SigninUserCommand<'a> {
|
|
pub fn password(&self) -> &str {
|
|
&self.password
|
|
}
|
|
|
|
pub fn login(&self) -> &str {
|
|
&self.login
|
|
}
|
|
|
|
pub fn new_command(login: &'a str, password: &'a str) -> Result<Self, UserError> {
|
|
let login = login.trim();
|
|
if password.is_empty() {
|
|
Err(UserError::PasswordIsEmpty)
|
|
} else if login.is_empty() {
|
|
Err(UserError::LoginIsEmpty)
|
|
} else {
|
|
Ok(Self { password, login })
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_sign_user_cmd() {
|
|
assert_eq!(
|
|
SigninUserCommand::new_command("foo", "",).err(),
|
|
Some(UserError::PasswordIsEmpty)
|
|
);
|
|
|
|
assert_eq!(
|
|
SigninUserCommand::new_command(" ", "foo",).err(),
|
|
Some(UserError::LoginIsEmpty)
|
|
);
|
|
assert!(SigninUserCommand::new_command("foo", "foo",).is_ok(),);
|
|
}
|
|
}
|