use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Denial {
pub gate: &'static str,
pub code: String,
pub message: String,
pub context: Vec<(String, String)>,
}
impl Denial {
pub fn new(gate: &'static str, message: impl Into<String>) -> Self {
Self {
gate,
code: String::new(),
message: message.into(),
context: vec![],
}
}
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = code.into();
self
}
pub fn with_context(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.context.push((key.into(), value.into()));
self
}
}
impl fmt::Display for Denial {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.gate, self.message)
}
}
impl std::error::Error for Denial {}