uri_parsing_rs/ast/
authority.rs1use 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<u64>,
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<u64>, user_info: Option<UserInfo>) -> Self {
46 Self {
47 host_name,
48 port,
49 user_info,
50 }
51 }
52}