use std::collections::HashMap;
use crate::Error;
#[derive(Debug, Copy, Clone)]
pub enum SpecProxyMode {
Inline,
Listening,
}
#[derive(Debug)]
pub struct SpecConfiguration<'a> {
disable_spec_proxy: bool,
operating_mode: SpecProxyMode,
percentage_of_ips: u8,
host_to_fastly_backend: HashMap<&'a str, &'a str>,
}
impl<'a> SpecConfiguration<'a> {
pub fn builder(
host_to_fastly_backend: HashMap<&'a str, &'a str>,
) -> SpecConfigurationBuilder<'a> {
SpecConfigurationBuilder {
config: SpecConfiguration::new(host_to_fastly_backend),
}
}
pub fn disable_spec_proxy(&self) -> bool {
self.disable_spec_proxy
}
pub fn operating_mode(&self) -> SpecProxyMode {
self.operating_mode
}
pub fn percentage_of_ips(&self) -> u8 {
self.percentage_of_ips
}
pub fn backend_for_host(&self, host: &'_ str) -> Result<&'a str, Error> {
self.host_to_fastly_backend
.get(host)
.cloned()
.ok_or_else(|| Error::MissingFastlyBackend(host.to_string()))
}
fn new(host_to_fastly_backend: HashMap<&'a str, &'a str>) -> Self {
Self {
disable_spec_proxy: false,
operating_mode: SpecProxyMode::Listening,
percentage_of_ips: 100,
host_to_fastly_backend,
}
}
}
#[derive(Debug)]
pub struct SpecConfigurationBuilder<'a> {
config: SpecConfiguration<'a>,
}
impl<'a> SpecConfigurationBuilder<'a> {
pub fn with_disable_spec_proxy(mut self, disable: bool) -> Self {
self.config.disable_spec_proxy = disable;
self
}
pub fn with_operating_mode(mut self, mode: SpecProxyMode) -> Self {
self.config.operating_mode = mode;
self
}
pub fn with_percentage_of_ips(mut self, percentage_of_ips: u8) -> Self {
self.config.percentage_of_ips = percentage_of_ips;
self
}
pub fn build(self) -> SpecConfiguration<'a> {
self.config
}
}