use rama_core::Layer;
use rama_http::HeaderName;
use super::UserAgentSelectFallback;
#[derive(Debug, Clone)]
pub struct UserAgentEmulateLayer<P> {
provider: P,
optional: bool,
try_auto_detect_user_agent: bool,
input_header_order: Option<HeaderName>,
select_fallback: Option<UserAgentSelectFallback>,
}
impl<P> UserAgentEmulateLayer<P> {
pub fn new(provider: P) -> Self {
Self {
provider,
optional: false,
try_auto_detect_user_agent: false,
input_header_order: None,
select_fallback: None,
}
}
rama_utils::macros::generate_set_and_with! {
pub fn is_optional(mut self, optional: bool) -> Self {
self.optional = optional;
self
}
}
rama_utils::macros::generate_set_and_with! {
pub fn try_auto_detect_user_agent(mut self, try_auto_detect_user_agent: bool) -> Self {
self.try_auto_detect_user_agent = try_auto_detect_user_agent;
self
}
}
rama_utils::macros::generate_set_and_with! {
pub fn input_header_order(mut self, name: Option<HeaderName>) -> Self {
self.input_header_order = name;
self
}
}
rama_utils::macros::generate_set_and_with! {
pub fn select_fallback(mut self, fb: Option<UserAgentSelectFallback>) -> Self {
self.select_fallback = fb;
self
}
}
}
impl<S, P: Clone> Layer<S> for UserAgentEmulateLayer<P> {
type Service = super::UserAgentEmulateService<S, P>;
fn layer(&self, inner: S) -> Self::Service {
super::UserAgentEmulateService::new(inner, self.provider.clone())
.with_is_optional(self.optional)
.with_try_auto_detect_user_agent(self.try_auto_detect_user_agent)
.maybe_with_select_fallback(self.select_fallback)
.maybe_with_input_header_order(self.input_header_order.clone())
}
fn into_layer(self, inner: S) -> Self::Service {
super::UserAgentEmulateService::new(inner, self.provider)
.with_is_optional(self.optional)
.with_try_auto_detect_user_agent(self.try_auto_detect_user_agent)
.maybe_with_select_fallback(self.select_fallback)
.maybe_with_input_header_order(self.input_header_order)
}
}