Remove ruma_events::InvalidEvent
This commit is contained in:
parent
418f92153d
commit
c40e3974e7
@ -1,5 +1,9 @@
|
||||
# [unreleased]
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Remove `TryFromRaw`, `FromRaw` and `InvalidEvent`
|
||||
|
||||
# 0.3.0
|
||||
|
||||
Breaking changes:
|
||||
|
@ -37,7 +37,10 @@ pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream>
|
||||
}
|
||||
}
|
||||
|
||||
fn from_parts(event_type: &str, input: Box<::serde_json::value::RawValue>) -> Result<Self, String> {
|
||||
fn from_parts(
|
||||
event_type: &str,
|
||||
input: Box<::serde_json::value::RawValue>,
|
||||
) -> Result<Self, ::serde_json::Error> {
|
||||
match event_type {
|
||||
#(
|
||||
#event_type_str => {
|
||||
@ -45,7 +48,7 @@ pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream>
|
||||
Ok(#ident::#variants(content))
|
||||
},
|
||||
)*
|
||||
ev => Err(format!("event not supported {}", ev)),
|
||||
ev => Err(::serde::de::Error::custom(format!("event not supported {}", ev))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,12 +51,14 @@ pub fn expand_event_content(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
fn from_parts(
|
||||
ev_type: &str,
|
||||
content: Box<::serde_json::value::RawValue>
|
||||
) -> Result<Self, String> {
|
||||
) -> Result<Self, ::serde_json::Error> {
|
||||
if ev_type != #event_type {
|
||||
return Err(format!("expected `{}` found {}", #event_type, ev_type));
|
||||
return Err(::serde::de::Error::custom(
|
||||
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
||||
));
|
||||
}
|
||||
|
||||
::serde_json::from_str(content.get()).map_err(|e| e.to_string())
|
||||
::serde_json::from_str(content.get())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -3,64 +3,6 @@ use std::{
|
||||
fmt::{self, Display, Formatter},
|
||||
};
|
||||
|
||||
/// An event that is malformed or otherwise invalid.
|
||||
///
|
||||
/// When attempting to deserialize an [`EventJson`](enum.EventJson.html), an error in the input
|
||||
/// data may cause deserialization to fail, or the JSON structure may be correct, but additional
|
||||
/// constraints defined in the matrix specification are not upheld. This type provides an error
|
||||
/// message and a flag for which type of error was encountered.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InvalidEvent {
|
||||
/// A description of the error that occurred.
|
||||
pub(crate) message: String,
|
||||
/// The kind of error that occurred.
|
||||
pub(crate) kind: InvalidEventKind,
|
||||
}
|
||||
|
||||
/// The kind of error that occurred.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum InvalidEventKind {
|
||||
/// A deserialization error from malformed input.
|
||||
Deserialization,
|
||||
/// An error occurred validating input according the the matrix spec.
|
||||
Validation,
|
||||
}
|
||||
|
||||
impl InvalidEvent {
|
||||
/// Constructor used in the event content macros.
|
||||
///
|
||||
/// This has to be public to allow the macros to be used outside of ruma-events.
|
||||
#[doc(hidden)]
|
||||
pub fn wrong_event_type(expected: &str, found: &str) -> Self {
|
||||
Self {
|
||||
message: format!("expected `{}` found {}", expected, found),
|
||||
kind: InvalidEventKind::Deserialization,
|
||||
}
|
||||
}
|
||||
/// A message describing why the event is invalid.
|
||||
pub fn message(&self) -> String {
|
||||
self.message.clone()
|
||||
}
|
||||
|
||||
/// Returns whether this is a deserialization error.
|
||||
pub fn is_deserialization(&self) -> bool {
|
||||
self.kind == InvalidEventKind::Deserialization
|
||||
}
|
||||
|
||||
/// Returns whether this is a validation error.
|
||||
pub fn is_validation(&self) -> bool {
|
||||
self.kind == InvalidEventKind::Validation
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for InvalidEvent {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message())
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for InvalidEvent {}
|
||||
|
||||
/// An error returned when attempting to create an event with data that would make it invalid.
|
||||
///
|
||||
/// This type is similar to [`InvalidEvent`](struct.InvalidEvent.html), but used during the
|
||||
|
@ -10,10 +10,7 @@ use serde::{
|
||||
};
|
||||
use serde_json::value::RawValue;
|
||||
|
||||
use crate::{
|
||||
error::{InvalidEvent, InvalidEventKind},
|
||||
EventContent,
|
||||
};
|
||||
use crate::EventContent;
|
||||
|
||||
/// A wrapper around `Box<RawValue>`, to be used in place of event [content] [collection] types in
|
||||
/// Matrix endpoint definition to allow request and response types to contain unknown events in
|
||||
@ -49,13 +46,8 @@ where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
/// Try to deserialize the JSON into the expected event type.
|
||||
pub fn deserialize(&self) -> Result<T, InvalidEvent> {
|
||||
match serde_json::from_str(self.json.get()) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(err) => {
|
||||
Err(InvalidEvent { message: err.to_string(), kind: InvalidEventKind::Validation })
|
||||
}
|
||||
}
|
||||
pub fn deserialize(&self) -> Result<T, serde_json::Error> {
|
||||
serde_json::from_str(self.json.get())
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,9 +56,8 @@ where
|
||||
T: EventContent,
|
||||
{
|
||||
/// Try to deserialize the JSON as event content
|
||||
pub fn deserialize_content(self, event_type: &str) -> Result<T, InvalidEvent> {
|
||||
pub fn deserialize_content(self, event_type: &str) -> Result<T, serde_json::Error> {
|
||||
T::from_parts(event_type, self.json)
|
||||
.map_err(|err| InvalidEvent { message: err, kind: InvalidEventKind::Deserialization })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ pub use self::{
|
||||
AnyRoomEventStub, AnyStateEvent, AnyStateEventContent, AnyStateEventStub,
|
||||
AnyStrippedStateEventStub, AnyToDeviceEvent, AnyToDeviceEventContent,
|
||||
},
|
||||
error::{FromStrError, InvalidEvent, InvalidInput},
|
||||
error::{FromStrError, InvalidInput},
|
||||
event_kinds::{
|
||||
BasicEvent, EphemeralRoomEvent, EphemeralRoomEventStub, MessageEvent, MessageEventStub,
|
||||
StateEvent, StateEventStub, StrippedStateEventStub, ToDeviceEvent,
|
||||
@ -218,7 +218,7 @@ pub trait EventContent: Sized + Serialize {
|
||||
fn event_type(&self) -> &str;
|
||||
|
||||
/// Constructs the given event content.
|
||||
fn from_parts(event_type: &str, content: Box<RawJsonValue>) -> Result<Self, String>;
|
||||
fn from_parts(event_type: &str, content: Box<RawJsonValue>) -> Result<Self, serde_json::Error>;
|
||||
}
|
||||
|
||||
/// Marker trait for the content of an ephemeral room event.
|
||||
|
Loading…
x
Reference in New Issue
Block a user