use super::{ErrorHandler, DEFAULT_PAYLOAD_LIMIT};
use crate::{ContentTypeHandler, MsgPackError};
use actix_web::{error::Error, HttpRequest};
use mime::Mime;
use std::sync::Arc;
#[derive(Clone)]
pub struct MsgPackConfig {
pub(crate) limit: usize,
pub(crate) error_handler: Option<ErrorHandler>,
pub(crate) content_type: Option<ContentTypeHandler>,
}
pub const DEFAULT_CONFIG: MsgPackConfig =
MsgPackConfig { limit: DEFAULT_PAYLOAD_LIMIT, error_handler: None, content_type: None };
impl MsgPackConfig {
pub fn limit(&mut self, limit: usize) -> &mut Self {
self.limit = limit;
self
}
pub fn error_handler<F>(&mut self, handler: F) -> &mut Self
where
F: Fn(MsgPackError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.error_handler = Some(Arc::new(handler));
self
}
pub fn content_type<F>(&mut self, handler: F) -> &mut Self
where
F: Fn(Mime) -> bool + Send + Sync + 'static,
{
self.content_type = Some(Arc::new(handler));
self
}
}
impl Default for MsgPackConfig {
fn default() -> Self {
DEFAULT_CONFIG
}
}