1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::ast::user_info::UserInfo;
use std::fmt::Formatter;
use crate::ast::host_name::HostName;

#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Authority {
  host_name: HostName,
  port: Option<u16>,
  user_info: Option<UserInfo>,
}

impl Default for Authority {
  fn default() -> Self {
    Authority {
      host_name: HostName::default(),
      port: Option::default(),
      user_info: Option::default(),
    }
  }
}

impl std::fmt::Display for Authority {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "{}{}{}",
      self
        .user_info
        .iter()
        .map(|ui| format!("{}@", ui.to_string()))
        .fold("".to_string(), |mut acc, s| {
          acc.push_str(&s);
          acc
        }),
      self.host_name.to_string(),
      self
        .port
        .map(|n| format!(":{}", n))
        .unwrap_or("".to_string()),
    )
  }
}

impl Authority {
  pub fn new(host_name: HostName, port: Option<u16>, user_info: Option<UserInfo>) -> Self {
    Self {
      host_name,
      port,
      user_info,
    }
  }

  pub fn host_name(&self) -> &HostName {
    &self.host_name
  }

  pub fn port(&self) -> Option<u16> {
    self.port
  }

  pub fn user_info(&self) -> Option<&UserInfo> {
    self.user_info.as_ref()
  }
}