uri_parsing_rs/ast/
user_info.rs1use std::fmt::Formatter;
2
3#[derive(Debug, Clone, PartialEq, Hash)]
4pub struct UserInfo {
5 user_name: String,
6 password: Option<String>,
7}
8
9impl Default for UserInfo {
10 fn default() -> Self {
11 UserInfo {
12 user_name: String::default(),
13 password: Option::default(),
14 }
15 }
16}
17
18impl std::fmt::Display for UserInfo {
19 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20 write!(
21 f,
22 "{}{}",
23 self.user_name(),
24 self
25 .password
26 .iter()
27 .map(|s| format!(":{}", s))
28 .fold("".to_string(), |mut acc, s| {
29 acc.push_str(&s);
30 acc
31 })
32 )
33 }
34}
35
36impl From<(&str, Option<&str>)> for UserInfo {
37 fn from((user_name, password): (&str, Option<&str>)) -> Self {
38 Self::new(user_name.to_string(), password.map(|s| s.to_string()))
39 }
40}
41
42impl UserInfo {
43 pub fn new(user_name: String, password: Option<String>) -> Self {
44 Self {
45 user_name,
46 password,
47 }
48 }
49 pub fn user_name(&self) -> &str {
50 &self.user_name
51 }
52 pub fn password(&self) -> Option<&String> {
53 self.password.as_ref()
54 }
55}