use minreq::{Method, Request, Response};
use super::{Neos, NeosAuthenticated, NeosUnauthenticated, RequestError};
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
pub enum AnyNeos {
Unauthenticated(NeosUnauthenticated),
Authenticated(NeosAuthenticated),
}
impl From<NeosUnauthenticated> for AnyNeos {
fn from(api: NeosUnauthenticated) -> Self {
Self::Unauthenticated(api)
}
}
impl From<NeosAuthenticated> for AnyNeos {
fn from(api: NeosAuthenticated) -> Self {
Self::Authenticated(api)
}
}
impl From<AnyNeos> for NeosUnauthenticated {
fn from(api: AnyNeos) -> Self {
match api {
AnyNeos::Unauthenticated(v) => v,
AnyNeos::Authenticated(v) => v.downgrade(),
}
}
}
impl AnyNeos {
#[must_use]
pub const fn is_authenticated(&self) -> bool {
match self {
Self::Authenticated(_) => true,
Self::Unauthenticated(_) => false,
}
}
#[must_use]
pub const fn is_unauthenticated(&self) -> bool {
match self {
Self::Authenticated(_) => false,
Self::Unauthenticated(_) => true,
}
}
#[must_use]
pub const fn authenticated(&self) -> Option<&NeosAuthenticated> {
match self {
Self::Authenticated(api) => Some(api),
Self::Unauthenticated(_) => None,
}
}
#[must_use]
pub const fn unauthenticated(&self) -> Option<&NeosUnauthenticated> {
match self {
Self::Authenticated(_) => None,
Self::Unauthenticated(api) => Some(api),
}
}
#[must_use]
pub fn authenticated_mut(&mut self) -> Option<&mut NeosAuthenticated> {
match self {
Self::Authenticated(api) => Some(&mut *api),
Self::Unauthenticated(_) => None,
}
}
#[must_use]
pub fn unauthenticated_mut(&mut self) -> Option<&mut NeosUnauthenticated> {
match self {
Self::Authenticated(_) => None,
Self::Unauthenticated(api) => Some(api),
}
}
}
impl Neos for AnyNeos {
fn api_request(
&self,
method: Method,
url: &str,
build: &mut dyn FnMut(Request) -> Result<Request, minreq::Error>,
) -> Result<Response, RequestError> {
match self {
Self::Authenticated(api) => api.api_request(method, url, build),
Self::Unauthenticated(api) => api.api_request(method, url, build),
}
}
}