use crate::FieldPath;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Violation {
path: FieldPath,
code: ViolationCode,
}
impl Violation {
pub(crate) fn new(path: FieldPath, code: ViolationCode) -> Self {
Self { path, code }
}
pub fn path(&self) -> &FieldPath {
&self.path
}
pub fn code(&self) -> ViolationCode {
self.code
}
}
impl fmt::Display for Violation {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}: {}", self.path, self.code)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ViolationCode {
Required,
InvalidType,
UnsupportedMediaType,
}
impl ViolationCode {
pub fn as_str(self) -> &'static str {
match self {
Self::Required => "required",
Self::InvalidType => "invalid_type",
Self::UnsupportedMediaType => "unsupported_media_type",
}
}
}
impl fmt::Display for ViolationCode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}