Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor clean up and derive more behavior on events #381

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading