use racal::Queryable;
use serde::{Deserialize, Serialize};
use super::NoAuthentication;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum UserIdOrUsername {
Id(crate::id::User),
Username(String),
}
impl UserIdOrUsername {
#[must_use]
pub const fn is_id(&self) -> bool { matches!(self, Self::Id(_)) }
#[must_use]
pub const fn is_username(&self) -> bool { matches!(self, Self::Username(_)) }
}
impl AsRef<str> for UserIdOrUsername {
fn as_ref(&self) -> &str {
match self {
Self::Id(v) => v.as_ref(),
Self::Username(v) => v,
}
}
}
impl From<&'static str> for UserIdOrUsername {
fn from(v: &'static str) -> Self { Self::Username(v.to_owned()) }
}
impl From<String> for UserIdOrUsername {
fn from(v: String) -> Self { Self::Username(v) }
}
impl From<crate::id::User> for UserIdOrUsername {
fn from(v: crate::id::User) -> Self { Self::Id(v) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UserInfo {
pub user: UserIdOrUsername,
}
impl UserInfo {
pub fn new(user: impl Into<UserIdOrUsername>) -> Self {
Self { user: user.into() }
}
}
impl Queryable<NoAuthentication, crate::model::User> for UserInfo {
fn url(&self, _: &NoAuthentication) -> String {
format!(
"{}/users/{}?byUsername={}",
crate::API_BASE_URI,
self.user.as_ref(),
&(!self.user.is_id()).to_string()
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UserStatus {
pub user_id: crate::id::User,
}
impl UserStatus {
pub fn new(user_id: impl Into<crate::id::User>) -> Self {
Self { user_id: user_id.into() }
}
}
impl Queryable<NoAuthentication, crate::model::UserStatus> for UserStatus {
fn url(&self, _: &NoAuthentication) -> String {
format!("{}/users/{}/status", crate::API_BASE_URI, self.user_id.as_ref(),)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UserSearch {
pub name: String,
}
impl UserSearch {
pub fn new(name: impl Into<String>) -> Self { Self { name: name.into() } }
}
impl Queryable<NoAuthentication, Vec<crate::model::User>> for UserSearch {
fn url(&self, _: &NoAuthentication) -> String {
format!("{}/users?name={}", crate::API_BASE_URI, self.name)
}
}