use crate::config::advanced::Bytes;
use crate::config::advanced::auth::AuthorizationRestrictions;
use crate::config::advanced::callout::Callout;
use crate::error::Error::ParseError;
use crate::error::Result;
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub enum AuthorizationSource {
Callout(Box<Callout>),
Static(AuthorizationRestrictions),
}
impl AuthorizationSource {
pub fn callout(&self) -> Option<&Callout> {
match self {
Self::Callout(callout) => Some(callout),
Self::Static(_) => None,
}
}
pub fn callout_mut(&mut self) -> Option<&mut Callout> {
match self {
Self::Callout(callout) => Some(callout),
Self::Static(_) => None,
}
}
pub fn static_restrictions(&self) -> Option<&AuthorizationRestrictions> {
match self {
Self::Static(restrictions) => Some(restrictions),
Self::Callout(_) => None,
}
}
}
#[derive(Deserialize, Debug, Clone)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum AuthorizationSourceBuilder {
Callout(Box<Callout>),
Static { path: PathBuf },
}
impl AuthorizationSourceBuilder {
pub fn build(self) -> Result<AuthorizationSource> {
match self {
Self::Callout(callout) => Ok(AuthorizationSource::Callout(callout)),
Self::Static { path } => {
let buf = Bytes::try_from(path.clone())?.into_inner();
let restrictions: AuthorizationRestrictions = serde_json::from_slice(&buf)
.map_err(|err| ParseError(format!("parsing {}: {err}", path.display())))?;
Ok(AuthorizationSource::Static(restrictions))
}
}
}
}