use serde::Deserialize;
use serde::Serialize;
use std::fmt;
#[derive(Debug)]
pub struct Signup;
#[derive(Debug)]
pub struct Signin;
pub trait Credentials<Action, Response>: Serialize {}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Root<'a> {
#[serde(rename = "user")]
pub username: &'a str,
#[serde(rename = "pass")]
pub password: &'a str,
}
impl Credentials<Signin, Jwt> for Root<'_> {}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Namespace<'a> {
#[serde(rename = "ns")]
pub namespace: &'a str,
#[serde(rename = "user")]
pub username: &'a str,
#[serde(rename = "pass")]
pub password: &'a str,
}
impl Credentials<Signin, Jwt> for Namespace<'_> {}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Database<'a> {
#[serde(rename = "ns")]
pub namespace: &'a str,
#[serde(rename = "db")]
pub database: &'a str,
#[serde(rename = "user")]
pub username: &'a str,
#[serde(rename = "pass")]
pub password: &'a str,
}
impl Credentials<Signin, Jwt> for Database<'_> {}
#[derive(Debug, Serialize)]
pub struct Record<'a, P> {
#[serde(rename = "ns")]
pub namespace: &'a str,
#[serde(rename = "db")]
pub database: &'a str,
#[serde(rename = "ac")]
pub access: &'a str,
#[serde(flatten)]
pub params: P,
}
impl<T, P> Credentials<T, Jwt> for Record<'_, P> where P: Serialize {}
#[derive(Clone, Serialize, Deserialize)]
pub struct Jwt(pub(crate) String);
impl Jwt {
pub fn as_insecure_token(&self) -> &str {
&self.0
}
pub fn into_insecure_token(self) -> String {
self.0
}
}
impl From<String> for Jwt {
fn from(jwt: String) -> Self {
Jwt(jwt)
}
}
impl<'a> From<&'a String> for Jwt {
fn from(jwt: &'a String) -> Self {
Jwt(jwt.to_owned())
}
}
impl<'a> From<&'a str> for Jwt {
fn from(jwt: &'a str) -> Self {
Jwt(jwt.to_owned())
}
}
impl fmt::Debug for Jwt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Jwt(REDACTED)")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn as_insecure_token() {
let jwt = Jwt("super-long-jwt".to_owned());
assert_eq!(jwt.as_insecure_token(), "super-long-jwt");
}
#[test]
fn into_insecure_token() {
let jwt = Jwt("super-long-jwt".to_owned());
assert_eq!(jwt.into_insecure_token(), "super-long-jwt");
}
}