pub struct MessengerValidateHook<V> { /* private fields */ }Expand description
Maps message names to typed Validatable payloads and rejects on violations.
§Examples
use serenade_messenger::{
Command, CommandHandler, Message, MessageBus, ValidationMiddleware,
};
use serenade_validator::{
Constraint, ConstraintViolationList, Length, MessengerValidateHook, NotBlank,
RecursiveValidator, Validatable, Validator,
};
struct EnqueueJob {
code: String,
}
impl Message for EnqueueJob {
const NAME: &'static str = "job.enqueue";
}
impl Command for EnqueueJob {}
impl Validatable for EnqueueJob {
fn validate(&self, validator: &dyn Validator) -> ConstraintViolationList {
validator.validate_value(
&self.code,
"code",
&[&NotBlank as &dyn Constraint, &Length::new(1, 32)],
)
}
}
struct OkHandler;
impl CommandHandler<EnqueueJob> for OkHandler {
fn handle(&self, _: &EnqueueJob) -> Result<(), serenade_messenger::MessengerError> {
Ok(())
}
}
let mut hook = MessengerValidateHook::new(RecursiveValidator);
hook.register::<EnqueueJob>();
let mut bus = MessageBus::new();
bus.add_middleware(ValidationMiddleware::new(hook));
bus.register_command(OkHandler).unwrap();
assert!(bus
.dispatch_command(&EnqueueJob { code: String::new() })
.is_err());Implementations§
Source§impl<V> MessengerValidateHook<V>
impl<V> MessengerValidateHook<V>
Sourcepub fn new(validator: V) -> MessengerValidateHook<V>
pub fn new(validator: V) -> MessengerValidateHook<V>
Creates a hook around validator.
Sourcepub fn register<T>(&mut self)where
T: Message + Validatable + 'static,
pub fn register<T>(&mut self)where
T: Message + Validatable + 'static,
Registers T under T::NAME when T: Message + Validatable.
Trait Implementations§
Source§impl<V> ValidateHook for MessengerValidateHook<V>where
V: Validator,
impl<V> ValidateHook for MessengerValidateHook<V>where
V: Validator,
Source§fn validate(&self, ctx: &DispatchContext<'_>) -> Result<(), MessengerError>
fn validate(&self, ctx: &DispatchContext<'_>) -> Result<(), MessengerError>
Returns
Ok(()) when the message may proceed. Read moreAuto Trait Implementations§
impl<V> !RefUnwindSafe for MessengerValidateHook<V>
impl<V> !UnwindSafe for MessengerValidateHook<V>
impl<V> Forget for MessengerValidateHook<V>
impl<V> Freeze for MessengerValidateHook<V>where
V: Freeze,
impl<V> Move for MessengerValidateHook<V>
impl<V> Send for MessengerValidateHook<V>where
V: Send,
impl<V> Sync for MessengerValidateHook<V>where
V: Sync,
impl<V> Unpin for MessengerValidateHook<V>where
V: Unpin,
impl<V> UnsafeUnpin for MessengerValidateHook<V>where
V: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SerializableKey for T
impl<T> SerializableKey for T
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Borrows the value.
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Takes the value.