Replace custom Void type, change TryFromRaw::Err bound
This commit is contained in:
parent
63b85f524c
commit
9536099f38
@ -1,4 +1,7 @@
|
|||||||
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
|
fmt::{Debug, Display, Formatter, Result as FmtResult},
|
||||||
|
};
|
||||||
|
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{DeserializeOwned, Error as SerdeError, Visitor},
|
de::{DeserializeOwned, Error as SerdeError, Visitor},
|
||||||
@ -115,14 +118,14 @@ pub trait FromRaw: Sized {
|
|||||||
pub trait TryFromRaw: Sized {
|
pub trait TryFromRaw: Sized {
|
||||||
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
|
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
|
||||||
type Raw: DeserializeOwned;
|
type Raw: DeserializeOwned;
|
||||||
type Err: Into<String>;
|
type Err: Display;
|
||||||
|
|
||||||
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>;
|
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: FromRaw> TryFromRaw for T {
|
impl<T: FromRaw> TryFromRaw for T {
|
||||||
type Raw = <T as FromRaw>::Raw;
|
type Raw = <T as FromRaw>::Raw;
|
||||||
type Err = Void;
|
type Err = Infallible;
|
||||||
|
|
||||||
fn try_from_raw(raw: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)> {
|
fn try_from_raw(raw: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)> {
|
||||||
Ok(Self::from_raw(raw))
|
Ok(Self::from_raw(raw))
|
||||||
@ -153,9 +156,9 @@ where
|
|||||||
|
|
||||||
match T::try_from_raw(raw_data) {
|
match T::try_from_raw(raw_data) {
|
||||||
Ok(value) => Ok(EventResult::Ok(value)),
|
Ok(value) => Ok(EventResult::Ok(value)),
|
||||||
Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent(
|
Err((err, raw_data)) => Ok(EventResult::Err(InvalidEvent(
|
||||||
InnerInvalidEvent::Validation {
|
InnerInvalidEvent::Validation {
|
||||||
message: msg.into(),
|
message: err.to_string(),
|
||||||
raw_data,
|
raw_data,
|
||||||
},
|
},
|
||||||
))),
|
))),
|
||||||
@ -163,14 +166,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Void {}
|
|
||||||
|
|
||||||
impl From<Void> for String {
|
|
||||||
fn from(v: Void) -> Self {
|
|
||||||
match v {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A basic event.
|
/// A basic event.
|
||||||
pub trait Event: Debug + Serialize + TryFromRaw {
|
pub trait Event: Debug + Serialize + TryFromRaw {
|
||||||
/// The type of this event's `content` field.
|
/// The type of this event's `content` field.
|
||||||
@ -242,7 +237,7 @@ enum InnerInvalidEvent<T> {
|
|||||||
/// The event data that failed validation.
|
/// The event data that failed validation.
|
||||||
raw_data: T,
|
raw_data: T,
|
||||||
|
|
||||||
/// An message describing why the event was invalid.
|
/// A message describing why the event was invalid.
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@ -115,6 +115,7 @@
|
|||||||
//#![deny(warnings)]
|
//#![deny(warnings)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult},
|
fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult},
|
||||||
};
|
};
|
||||||
@ -226,7 +227,7 @@ enum InnerInvalidEvent<T> {
|
|||||||
/// The event data that failed validation.
|
/// The event data that failed validation.
|
||||||
raw_data: T,
|
raw_data: T,
|
||||||
|
|
||||||
/// An message describing why the event was invalid.
|
/// A message describing why the event was invalid.
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -259,31 +260,20 @@ pub trait FromRaw: Sized {
|
|||||||
pub trait TryFromRaw: Sized {
|
pub trait TryFromRaw: Sized {
|
||||||
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
|
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
|
||||||
type Raw: DeserializeOwned;
|
type Raw: DeserializeOwned;
|
||||||
type Err: Into<String>;
|
type Err: Display;
|
||||||
|
|
||||||
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>;
|
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: FromRaw> TryFromRaw for T {
|
impl<T: FromRaw> TryFromRaw for T {
|
||||||
type Raw = <T as FromRaw>::Raw;
|
type Raw = <T as FromRaw>::Raw;
|
||||||
type Err = Void;
|
type Err = Infallible;
|
||||||
|
|
||||||
fn try_from_raw(raw: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)> {
|
fn try_from_raw(raw: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)> {
|
||||||
Ok(Self::from_raw(raw))
|
Ok(Self::from_raw(raw))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Replace with ! once that is stable
|
|
||||||
/// An empty type
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Void {}
|
|
||||||
|
|
||||||
impl From<Void> for String {
|
|
||||||
fn from(v: Void) -> Self {
|
|
||||||
match v {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The result of deserializing an event, which may or may not be valid.
|
/// The result of deserializing an event, which may or may not be valid.
|
||||||
///
|
///
|
||||||
/// When data is successfully deserialized and validated, this structure will contain the
|
/// When data is successfully deserialized and validated, this structure will contain the
|
||||||
@ -335,9 +325,9 @@ where
|
|||||||
|
|
||||||
match T::try_from_raw(raw_data) {
|
match T::try_from_raw(raw_data) {
|
||||||
Ok(value) => Ok(EventResult::Ok(value)),
|
Ok(value) => Ok(EventResult::Ok(value)),
|
||||||
Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent(
|
Err((err, raw_data)) => Ok(EventResult::Err(InvalidEvent(
|
||||||
InnerInvalidEvent::Validation {
|
InnerInvalidEvent::Validation {
|
||||||
message: msg.into(),
|
message: err.to_string(),
|
||||||
raw_data,
|
raw_data,
|
||||||
},
|
},
|
||||||
))),
|
))),
|
||||||
|
@ -7,7 +7,7 @@ pub fn try_convert_variant<Enum: TryFromRaw, Content: TryFromRaw>(
|
|||||||
) -> Result<Enum, (String, Enum::Raw)> {
|
) -> Result<Enum, (String, Enum::Raw)> {
|
||||||
Content::try_from_raw(raw)
|
Content::try_from_raw(raw)
|
||||||
.map(variant)
|
.map(variant)
|
||||||
.map_err(|(msg, raw)| (msg.into(), raw_variant(raw)))
|
.map_err(|(err, raw)| (err.to_string(), raw_variant(raw)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serde_json_error_to_generic_de_error<E: serde::de::Error>(error: serde_json::Error) -> E {
|
pub fn serde_json_error_to_generic_de_error<E: serde::de::Error>(error: serde_json::Error) -> E {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user