use lazy_static::lazy_static; use regex::Regex; pub struct Data { id: Id, profile: Option, } pub struct Id { name: String, host: Option, } pub struct Profile { name: String, } impl Data { fn get_profile(&self) -> Result { Err("not implemented".to_owned()) } fn set_profile(&self, d: &Profile) -> Result<(), String> { Err("not implemented".to_owned()) } } // hello@world.com -> Id {hello, world.com} pub fn parse_id(v: &str) -> Result { lazy_static! { static ref RE: Regex = Regex::new(r"^([A-Za-z0-9_]+)(?:@([A-Za-z0-9\-\.]+))?$").unwrap(); } match RE.captures(v) { Some(caps) => Ok(Id { name: caps.get(1).unwrap().as_str().to_owned(), host: caps.get(2).map_or(None, |m| Some(m.as_str().to_owned())), }), None => Err("invalid idstr".to_owned()), } } impl Id { fn to_string(&self, default_host: &str) -> String { format!("{}@{}", self.name, self.host.as_deref().unwrap_or(default_host)) } }