use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{
collections::HashSet,
fmt,
hash::{Hash, Hasher},
};
use url::Url;
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct Origin {
name: String,
url: Url,
}
impl Origin {
pub fn new(name: String, url: Url) -> Self {
Self { name, url }
}
pub fn name(&self) -> &str {
&self.name
}
pub fn url(&self) -> &Url {
&self.url
}
}
impl PartialEq for Origin {
fn eq(&self, other: &Self) -> bool {
self.url == other.url
}
}
impl Hash for Origin {
fn hash<H: Hasher>(&self, state: &mut H) {
self.url.hash(state);
}
}
impl fmt::Display for Origin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.url)
}
}
impl From<Url> for Origin {
fn from(url: Url) -> Self {
let name = url.authority().to_owned();
Self { name, url }
}
}
#[async_trait]
pub trait RemoteOrigins {
type Error: std::error::Error + std::fmt::Debug;
async fn list_servers(&self) -> Result<HashSet<Origin>, Self::Error>;
async fn add_server(&mut self, origin: Origin)
-> Result<(), Self::Error>;
async fn replace_server(
&mut self,
old_origin: &Origin,
new_origin: Origin,
) -> Result<(), Self::Error>;
async fn remove_server(
&mut self,
origin: &Origin,
) -> Result<(), Self::Error>;
}