use std::fmt::{Display, Formatter}; use std::str::FromStr; use serde::{Deserialize, Serialize}; use super::instance::Instance; #[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] pub struct User { pub username: String, pub instance: Instance, } impl Display for User { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}", self.username, self.instance.url) } } impl From for User { fn from(user_string: String) -> Self { User::from_str(&user_string).unwrap() } } impl FromStr for User { type Err = (); fn from_str(s: &str) -> Result { let mut colon_split = s.split(':'); let username = colon_split.next().unwrap().to_string(); let instance = Instance::from_str(colon_split.next().unwrap()).unwrap(); Ok(Self { username, instance }) } }