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
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, PartialEq)]
pub struct Author {
    parts: Vec<String>,
}

impl Author {
    pub fn parse(s: String) -> Author {
        let mut vec: Vec<String> = s.split("/").into_iter().filter(|s| !s.is_empty()).map(|s| s.to_owned()).collect();
        match hostname::get() {
            Ok(hostname) => vec.insert(0, hostname.to_string_lossy().into()),
            Err(e) => {
                vec.insert(0, "?".to_owned());
                eprintln!("Failed to detect hostname {}", e);
            },
        }
        Self {
            parts: vec
        }
    }
}

impl Display for Author {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.parts.join("/"))
    }
}