use std::collections::HashMap;
use crate::constants;
#[derive(Debug)]
pub struct SizeLimit {
pub(crate) whole_stream: u64,
pub(crate) per_field: u64,
pub(crate) field_map: HashMap<String, u64>,
}
impl SizeLimit {
pub fn new() -> SizeLimit {
SizeLimit::default()
}
pub fn whole_stream(mut self, limit: u64) -> SizeLimit {
self.whole_stream = limit;
self
}
pub fn per_field(mut self, limit: u64) -> SizeLimit {
self.per_field = limit;
self
}
pub fn for_field<N: Into<String>>(mut self, field_name: N, limit: u64) -> SizeLimit {
self.field_map.insert(field_name.into(), limit);
self
}
pub(crate) fn extract_size_limit_for(&self, field: Option<&str>) -> u64 {
field
.and_then(|field| self.field_map.get(&field.to_owned()))
.copied()
.unwrap_or(self.per_field)
}
}
impl Default for SizeLimit {
fn default() -> Self {
SizeLimit {
whole_stream: constants::DEFAULT_WHOLE_STREAM_SIZE_LIMIT,
per_field: constants::DEFAULT_PER_FIELD_SIZE_LIMIT,
field_map: HashMap::default(),
}
}
}