use ckey::CKey;
use serde::{Deserialize, Serialize};
use std::fmt::Formatter;
use url::Url;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct EP {
key: CKey,
url: Url,
}
pub const LOCAL: &str = "local";
impl EP {
pub fn new_local() -> EP {
let key = CKey::new_rand();
let url = format!("{}:{}", LOCAL, key);
EP {
key,
url: Url::parse(url.as_str()).unwrap(),
}
}
pub fn parse(key: &str, url: &str) -> Result<EP, Box<dyn std::error::Error>> {
let parsed_key = CKey::parse(key)?;
let parsed_url = Url::parse(url)?;
Ok(EP {
key: parsed_key,
url: parsed_url,
})
}
}
impl std::fmt::Display for EP {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{\"key\":\"{}\",\"url\":\"{}\"}}",
self.key,
self.url.as_str()
)
}
}