use std::collections::BTreeMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use crate::ValidationPath;
use crate::ViolationCode;
use crate::ViolationParam;
#[must_use]
#[derive(Eq, PartialEq)]
pub struct ViolationDraft {
code: ViolationCode,
path: ValidationPath,
params: BTreeMap<&'static str, ViolationParam>,
}
impl ViolationDraft {
#[inline]
pub fn new(code: ViolationCode) -> Self {
Self {
code,
path: ValidationPath::root(),
params: BTreeMap::new(),
}
}
#[inline]
pub fn with_path(mut self, path: ValidationPath) -> Self {
self.path = path;
self
}
pub fn with_param(mut self, name: &'static str, value: ViolationParam) -> Self {
self.params.insert(name, value);
self
}
#[inline]
pub(crate) fn parts(self) -> (ViolationCode, ValidationPath, BTreeMap<&'static str, ViolationParam>) {
(self.code, self.path, self.params)
}
}
impl Debug for ViolationDraft {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter
.debug_struct("ViolationDraft")
.field("code", &self.code)
.field("path", &self.path)
.field("parameter_count", &self.params.len())
.finish()
}
}