pub(crate) mod client;
mod curve;
pub(crate) mod server;
pub use client::{AuthBuilder, AuthClient};
pub use curve::*;
pub use server::{StatusCode, StatusCodeParseError};
use crate::prelude::TryFrom;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use std::option;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PlainClientCreds {
pub(crate) username: String,
pub(crate) password: String,
}
impl PlainClientCreds {
pub fn new<U, P>(username: U, password: P) -> Self
where
U: Into<String>,
P: Into<String>,
{
Self {
username: username.into(),
password: password.into(),
}
}
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
}
impl<'a> From<&'a PlainClientCreds> for PlainClientCreds {
fn from(creds: &'a PlainClientCreds) -> Self {
creds.to_owned()
}
}
impl<'a> From<&'a PlainClientCreds> for Mechanism {
fn from(creds: &'a PlainClientCreds) -> Self {
Self::from(creds.to_owned())
}
}
impl From<PlainClientCreds> for Mechanism {
fn from(creds: PlainClientCreds) -> Self {
Mechanism::PlainClient(creds)
}
}
impl IntoIterator for PlainClientCreds {
type Item = Self;
type IntoIter = option::IntoIter<Self>;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
impl<'a> IntoIterator for &'a PlainClientCreds {
type Item = Self;
type IntoIter = option::IntoIter<Self>;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Mechanism {
Null,
PlainClient(PlainClientCreds),
PlainServer,
CurveClient(CurveClientCreds),
CurveServer(CurveServerCreds),
}
impl<'a> From<&'a Mechanism> for Mechanism {
fn from(mechanism: &'a Mechanism) -> Self {
mechanism.to_owned()
}
}
impl Default for Mechanism {
fn default() -> Self {
Mechanism::Null
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum MechanismName {
Null,
Plain,
Curve,
}
#[derive(Debug, Error)]
#[error("unsupported mechanism")]
pub(crate) struct InvalidMechanismName;
impl<'a> TryFrom<&'a str> for MechanismName {
type Error = InvalidMechanismName;
fn try_from(s: &'a str) -> Result<MechanismName, InvalidMechanismName> {
match s {
"NULL" => Ok(MechanismName::Null),
"PLAIN" => Ok(MechanismName::Plain),
"CURVE" => Ok(MechanismName::Curve),
_ => Err(InvalidMechanismName),
}
}
}