use derive_builder::Builder;
use regex::Regex;
use std::borrow::Cow;
#[derive(Default, Debug, Builder)]
pub struct Configuration<'a> {
public_resource: bool,
support_credentials: bool,
origin_groups: Vec<OriginGroup<'a>>,
}
impl<'a> Configuration<'a> {
pub fn public_resource(&self) -> bool {
self.public_resource
}
pub fn support_credentials(&self) -> bool {
self.support_credentials
}
pub fn origin_groups(&self) -> &[OriginGroup<'a>] {
&self.origin_groups
}
}
impl Configuration<'_> {
pub fn builder<'a>() -> ConfigurationBuilder<'a> {
ConfigurationBuilder::default()
}
}
#[derive(Default, Debug, Clone, Builder)]
pub struct OriginGroup<'a> {
#[builder(setter(into))]
origin_group_name: String,
#[builder(setter(into))]
plain_origins: Cow<'a, [String]>,
#[builder(setter(into))]
regex_origins: Cow<'a, [Regex]>,
access_control_max_age: u32,
allowed_methods: Vec<AllowedMethod>,
headers: Vec<String>,
exposed_headers: Vec<String>,
}
impl<'a> OriginGroup<'a> {
pub fn origin_group_name(&self) -> &str {
&self.origin_group_name
}
pub(crate) fn plain_origins_cow(&self) -> &Cow<'a, [String]> {
&self.plain_origins
}
pub(crate) fn regex_origins_cow(&self) -> &Cow<'a, [Regex]> {
&self.regex_origins
}
pub fn regex_origins(&self) -> &[Regex] {
&self.regex_origins
}
pub fn plain_origins(&self) -> &[String] {
&self.plain_origins
}
pub fn access_control_max_age(&self) -> u32 {
self.access_control_max_age
}
pub fn allowed_methods(&self) -> &[AllowedMethod] {
&self.allowed_methods
}
pub fn headers(&self) -> &[String] {
&self.headers
}
pub fn exposed_headers(&self) -> &[String] {
&self.exposed_headers
}
}
impl OriginGroup<'_> {
pub fn builder<'b>() -> OriginGroupBuilder<'b> {
OriginGroupBuilder::default()
}
}
#[derive(Default, Debug, Clone, Builder)]
pub struct AllowedMethod {
#[builder(setter(into))]
method_name: String,
allowed: bool,
}
impl AllowedMethod {
pub fn builder() -> AllowedMethodBuilder {
AllowedMethodBuilder::default()
}
}
pub(crate) trait CorsConfig<'a> {
fn public_resource(&self) -> bool;
fn support_credentials(&self) -> bool;
fn origin_groups(&self) -> &[OriginGroup<'a>];
}
impl<'a> CorsConfig<'a> for Configuration<'a> {
fn public_resource(&self) -> bool {
self.public_resource
}
fn support_credentials(&self) -> bool {
self.support_credentials
}
fn origin_groups(&self) -> &[OriginGroup<'a>] {
&self.origin_groups
}
}
#[cfg(test)]
mockall::mock! {
pub Configuration {}
impl CorsConfig<'_> for Configuration {
fn public_resource(&self) -> bool {
self.public_resource
}
fn support_credentials(&self) -> bool {
self.support_credentials
}
fn origin_groups(&self) -> &[OriginGroup<'static>] {
self.origin_groups.as_slice()
}
}
}
impl AllowedMethod {
pub fn method_name(&self) -> &str {
&self.method_name
}
pub fn allowed(&self) -> bool {
self.allowed
}
}