Skip to content

Commit

Permalink
error improvements, check bytes, and typo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Apr 1, 2024
1 parent fe3ed27 commit cbf459c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ impl fmt::Debug for ArchivedApiError {
}
}

impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code as u16, self.message)
}
}

impl std::error::Error for ApiError {}

macro_rules! error_codes {
(
$(#[$meta:meta])*
Expand Down Expand Up @@ -137,3 +145,36 @@ error_codes! {
Unknown = 1 = StatusCode::IM_A_TEAPOT,
}
}

impl From<std::io::Error> for ApiError {
fn from(err: std::io::Error) -> Self {
use std::io::ErrorKind as E;

Self {
code: ApiErrorCode::IOError,
message: Cow::Borrowed(match err.kind() {
E::AddrInUse => "Address In Use",
E::AddrNotAvailable => "Address Not Available",
E::AlreadyExists => "Entity Already Exists",
E::BrokenPipe => "Broken Pipe",
E::ConnectionAborted => "Connection Aborted",
E::ConnectionRefused => "Connection Refused",
E::ConnectionReset => "Connection Reset",
E::Interrupted => "Operation Interrupted",
E::InvalidData => "Invalid Data",
E::InvalidInput => "Invalid Input Parameter",
E::NotConnected => "Not Connected",
E::NotFound => "Entity Not Found",
E::Other => "Other Error",
E::OutOfMemory => "Out Of Memory",
E::PermissionDenied => "Permission Denied",
E::TimedOut => "Timed Out",
E::UnexpectedEof => "Unexpected End Of File",
E::Unsupported => "Unsupported",
E::WouldBlock => "Operation Would Block",
E::WriteZero => "Write Zero",
_ => "Unknown I/O error",
}),
}
}
}
6 changes: 5 additions & 1 deletion src/models/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum MessageKind {
Unavailable = 3,
}

common::impl_rkyv_for_pod!(MessageKind);
common::impl_rkyv_for_pod!(MessageKind + CheckBytes);

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -162,6 +162,7 @@ const _: () = {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct ReactionShorthand {
#[serde(flatten)]
pub emote: EmoteOrEmoji,
Expand All @@ -173,6 +174,7 @@ pub struct ReactionShorthand {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct ReactionFull {
pub emote: EmoteOrEmoji,

Expand All @@ -183,6 +185,7 @@ pub struct ReactionFull {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
#[serde(untagged)]
pub enum Reaction {
Shorthand(ReactionShorthand),
Expand All @@ -192,6 +195,7 @@ pub enum Reaction {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct Attachment {
#[serde(flatten)]
pub file: File,
Expand Down
2 changes: 1 addition & 1 deletion src/models/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[cfg_attr(feature = "rkyv", derive(rkyv::CheckBytes))]
#[repr(u8)]
pub enum Nullable<T> {
/// Neither present nor absent, and indeterminant value.
/// Neither present nor absent, an indeterminant value.
#[default]
Undefined = 0,
/// Certainly absent of value.
Expand Down

0 comments on commit cbf459c

Please sign in to comment.