Skip to content

Commit

Permalink
chore: minor clean up and derive more behavior on events (#381)
Browse files Browse the repository at this point in the history
* feat: derive event header traits

IPLD impls debug, PartialEq, Eq so unless we allow true floats I think this is okay

* chore: clean up errors and unused code
  • Loading branch information
dav1do authored Jun 14, 2024
1 parent b438141 commit 67cb492
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 75 deletions.
2 changes: 1 addition & 1 deletion event/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{
};

/// Sequence of byte values.
#[derive(Clone, PartialEq, Default, Debug)]
#[derive(Clone, PartialEq, Eq, Default, Debug)]
pub struct Bytes(Vec<u8>);

impl Bytes {
Expand Down
12 changes: 11 additions & 1 deletion event/src/unvalidated/payload/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<D> Payload<D> {
}

/// Headers for an init event
#[derive(Default, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Header {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -81,4 +81,14 @@ impl Header {
pub fn should_index(&self) -> bool {
self.should_index.unwrap_or(true)
}

/// The unique value for the stream
pub fn unique(&self) -> Option<&[u8]> {
self.unique.as_ref().map(Bytes::as_slice)
}

/// The context value for the stream
pub fn context(&self) -> Option<&[u8]> {
self.context.as_ref().map(Bytes::as_slice)
}
}
29 changes: 8 additions & 21 deletions service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,21 @@ impl Error {
impl From<Error> for recon::Error {
fn from(value: Error) -> Self {
match value {
Error::Application { error } => recon::Error::Application {
error: error.context("recon error"),
},
Error::Fatal { error } => recon::Error::Fatal {
error: error.context("recon error"),
},
Error::Transient { error } => recon::Error::Transient {
error: error.context("recon error"),
},
Error::InvalidArgument { error } => recon::Error::Application {
error: error.context("recon error"),
},
Error::Application { error } => recon::Error::Application { error },
Error::Fatal { error } => recon::Error::Fatal { error },
Error::Transient { error } => recon::Error::Transient { error },
Error::InvalidArgument { error } => recon::Error::Application { error },
}
}
}

impl From<ceramic_store::Error> for Error {
fn from(value: ceramic_store::Error) -> Self {
match value {
ceramic_store::Error::Application { error } => Error::Application {
error: error.context("store error"),
},
ceramic_store::Error::Fatal { error } => Error::Fatal {
error: error.context("store error"),
},
ceramic_store::Error::Transient { error } => Error::Transient {
error: error.context("store error"),
},
ceramic_store::Error::Application { error } => Error::Application { error },
ceramic_store::Error::Fatal { error } => Error::Fatal { error },
ceramic_store::Error::Transient { error } => Error::Transient { error },
ceramic_store::Error::InvalidArgument { error } => Error::InvalidArgument { error },
}
}
}
17 changes: 17 additions & 0 deletions store/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub enum Error {
/// The error details that may include context and other information
error: anyhow::Error,
},
#[error("InvalidArgument: {error}")]
/// Invalid client input
InvalidArgument {
/// The error details that may include context and other information
error: anyhow::Error,
},
#[error("Fatal error encountered: {error}")]
/// A fatal error that is unlikely to be recoverable, and may require terminating the process completely
Fatal {
Expand Down Expand Up @@ -45,6 +51,13 @@ impl Error {
}
}

/// Crate an InvalidArgument error
pub fn new_invalid_arg(error: impl Into<anyhow::Error>) -> Self {
Self::InvalidArgument {
error: error.into(),
}
}

/// Add context to the internal error. Works identically to `anyhow::context`
pub fn context<C>(self, context: C) -> Self
where
Expand All @@ -60,6 +73,9 @@ impl Error {
Error::Transient { error } => Self::Transient {
error: error.context(context),
},
Error::InvalidArgument { error } => Self::InvalidArgument {
error: error.context(context),
},
}
}
}
Expand Down Expand Up @@ -115,6 +131,7 @@ impl From<Error> for recon::Error {
Error::Application { error } => recon::Error::Application { error },
Error::Fatal { error } => recon::Error::Fatal { error },
Error::Transient { error } => recon::Error::Transient { error },
Error::InvalidArgument { error } => recon::Error::Application { error },
}
}
}
52 changes: 0 additions & 52 deletions store/src/sql/entities/cid.rs

This file was deleted.

0 comments on commit 67cb492

Please sign in to comment.