uri_rs/ast/
authority.rs

1use crate::ast::user_info::UserInfo;
2use std::fmt::Formatter;
3use crate::ast::host_name::HostName;
4
5#[derive(Debug, Clone, PartialEq, Hash)]
6pub struct Authority {
7  host_name: HostName,
8  port: Option<u16>,
9  user_info: Option<UserInfo>,
10}
11
12impl Default for Authority {
13  fn default() -> Self {
14    Authority {
15      host_name: HostName::default(),
16      port: Option::default(),
17      user_info: Option::default(),
18    }
19  }
20}
21
22impl std::fmt::Display for Authority {
23  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24    write!(
25      f,
26      "{}{}{}",
27      self
28        .user_info
29        .iter()
30        .map(|ui| format!("{}@", ui.to_string()))
31        .fold("".to_string(), |mut acc, s| {
32          acc.push_str(&s);
33          acc
34        }),
35      self.host_name.to_string(),
36      self
37        .port
38        .map(|n| format!(":{}", n))
39        .unwrap_or("".to_string()),
40    )
41  }
42}
43
44impl Authority {
45  pub fn new(host_name: HostName, port: Option<u16>, user_info: Option<UserInfo>) -> Self {
46    Self {
47      host_name,
48      port,
49      user_info,
50    }
51  }
52
53  pub fn host_name(&self) -> &HostName {
54    &self.host_name
55  }
56
57  pub fn port(&self) -> Option<u16> {
58    self.port
59  }
60
61  pub fn user_info(&self) -> Option<&UserInfo> {
62    self.user_info.as_ref()
63  }
64}