use crate::config::advanced::auth::AuthorizationRestrictions;
use http::Uri;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use std::fs::File;
use std::io::Read;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UrlOrStatic {
Url(Uri),
Static(AuthorizationRestrictions),
}
impl<'de> Deserialize<'de> for UrlOrStatic {
fn deserialize<D>(deserializer: D) -> Result<UrlOrStatic, D::Error>
where
D: Deserializer<'de>,
{
let uri = String::deserialize(deserializer)?;
if uri.to_lowercase().starts_with("http://") || uri.to_lowercase().starts_with("https://") {
Ok(UrlOrStatic::Url(uri.parse().map_err(Error::custom)?))
} else {
let mut auth_rules =
File::open(uri.strip_prefix("file://").unwrap_or(&uri)).map_err(Error::custom)?;
let mut buf = vec![];
auth_rules.read_to_end(&mut buf).map_err(Error::custom)?;
Ok(UrlOrStatic::Static(
serde_json::from_slice(buf.as_slice()).map_err(Error::custom)?,
))
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ForwardExtensions {
json_path: String,
name: String,
}
impl ForwardExtensions {
pub fn new(json_path: String, name: String) -> Self {
Self { json_path, name }
}
pub fn json_path(&self) -> &str {
&self.json_path
}
pub fn name(&self) -> &str {
&self.name
}
}