use std::collections::BTreeMap;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use super::ValidationPath;
use super::ViolationCode;
use super::ViolationParam;
use crate::ValidatorId;
#[must_use]
#[derive(Clone, Eq, PartialEq)]
pub struct Violation {
rule_id: ValidatorId,
code: ViolationCode,
path: ValidationPath,
params: BTreeMap<&'static str, ViolationParam>,
}
impl Violation {
#[inline]
pub fn new(rule_id: ValidatorId, code: ViolationCode) -> Self {
Self {
rule_id,
code,
path: ValidationPath::root(),
params: BTreeMap::new(),
}
}
#[must_use]
#[inline]
pub const fn rule_id(&self) -> ValidatorId {
self.rule_id
}
#[must_use]
#[inline]
pub const fn code(&self) -> ViolationCode {
self.code
}
#[must_use]
#[inline]
pub const fn path(&self) -> &ValidationPath {
&self.path
}
#[must_use]
#[inline]
pub const fn params(&self) -> &BTreeMap<&'static str, ViolationParam> {
&self.params
}
#[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
}
pub(crate) fn from_draft(rule_id: ValidatorId, draft: crate::ViolationDraft) -> Self {
let (code, path, params) = draft.parts();
Self {
rule_id,
code,
path,
params,
}
}
}
impl Debug for Violation {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter
.debug_struct("Violation")
.field("rule_id", &self.rule_id)
.field("code", &self.code)
.finish_non_exhaustive()
}
}
impl Display for Violation {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&self.code, formatter)
}
}