fadaway/src/ctx/user.rs

44 lines
1011 B
Rust

use lazy_static::lazy_static;
use regex::Regex;
pub struct Data {
id: Id,
profile: Option<Profile>,
}
pub struct Id {
name: String,
host: Option<String>,
}
pub struct Profile {
name: String,
}
impl Data {
fn get_profile(&self) -> Result<Profile, String> {
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<Id, String> {
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))
}
}