use std::borrow::Cow;
use crate::{
data,
error::{ApiError, Result},
EndpointResult,
};
#[derive(Serialize, Clone, Hash, Debug)]
pub struct RegistrationArgs<'a> {
username: Cow<'a, str>,
email: Option<Cow<'a, str>>,
password: Cow<'a, str>,
}
impl<'a> RegistrationArgs<'a> {
pub fn new<T, U>(username: T, password: U) -> Self
where
T: Into<Cow<'a, str>>,
U: Into<Cow<'a, str>>,
{
RegistrationArgs {
username: username.into(),
email: None,
password: password.into(),
}
}
pub fn with_email<T, U, V>(username: T, password: U, email: V) -> Self
where
T: Into<Cow<'a, str>>,
U: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
RegistrationArgs {
username: username.into(),
password: password.into(),
email: Some(email.into()),
}
}
}
#[derive(serde_derive::Deserialize, Clone, Hash, Debug)]
pub(crate) struct Response {
ok: i32,
}
#[derive(Clone, Hash, Debug)]
pub struct RegistrationSuccess {
_non_exhaustive: (),
}
impl EndpointResult for RegistrationSuccess {
type RequestResult = Response;
type ErrorResult = data::ApiError;
fn from_raw(raw: Response) -> Result<RegistrationSuccess> {
let Response { ok } = raw;
if ok != 1 {
return Err(ApiError::NotOk(ok).into());
}
Ok(RegistrationSuccess {
_non_exhaustive: (),
})
}
}
#[cfg(test)]
mod tests {
use super::RegistrationSuccess;
use crate::EndpointResult;
use serde_json;
fn test_parse(json: serde_json::Value) {
let response = serde_json::from_value(json).unwrap();
let _ = RegistrationSuccess::from_raw(response).unwrap();
}
#[test]
fn parse_sample() {
test_parse(json! ({
"ok": 1,
}));
}
}