diff --git a/event/src/bytes.rs b/event/src/bytes.rs index 58557977e..79334def4 100644 --- a/event/src/bytes.rs +++ b/event/src/bytes.rs @@ -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); impl Bytes { diff --git a/event/src/unvalidated/payload/init.rs b/event/src/unvalidated/payload/init.rs index f1a7c99f1..a89ee25c6 100644 --- a/event/src/unvalidated/payload/init.rs +++ b/event/src/unvalidated/payload/init.rs @@ -26,7 +26,7 @@ impl Payload { } /// 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")] @@ -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) + } } diff --git a/service/src/error.rs b/service/src/error.rs index d2af9d6a2..f75c33d0a 100644 --- a/service/src/error.rs +++ b/service/src/error.rs @@ -81,18 +81,10 @@ impl Error { impl From 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 }, } } } @@ -100,15 +92,10 @@ impl From for recon::Error { impl From 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 }, } } } diff --git a/store/src/error.rs b/store/src/error.rs index 75628acc4..afd574126 100644 --- a/store/src/error.rs +++ b/store/src/error.rs @@ -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 { @@ -45,6 +51,13 @@ impl Error { } } + /// Crate an InvalidArgument error + pub fn new_invalid_arg(error: impl Into) -> Self { + Self::InvalidArgument { + error: error.into(), + } + } + /// Add context to the internal error. Works identically to `anyhow::context` pub fn context(self, context: C) -> Self where @@ -60,6 +73,9 @@ impl Error { Error::Transient { error } => Self::Transient { error: error.context(context), }, + Error::InvalidArgument { error } => Self::InvalidArgument { + error: error.context(context), + }, } } } @@ -115,6 +131,7 @@ impl From 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 }, } } } diff --git a/store/src/sql/entities/cid.rs b/store/src/sql/entities/cid.rs deleted file mode 100644 index 5211faa3b..000000000 --- a/store/src/sql/entities/cid.rs +++ /dev/null @@ -1,52 +0,0 @@ -use std::borrow::Cow; - -use cid::Cid; -use sqlx::{ - encode::IsNull, - error::BoxDynError, - sqlite::{SqliteArgumentValue, SqliteRow, SqliteTypeInfo, SqliteValueRef}, - Decode, Encode, FromRow, Row, Sqlite, Type, -}; - -#[derive(Debug)] -pub struct CidBlob(pub(crate) Cid); - -impl Type for CidBlob { - fn type_info() -> SqliteTypeInfo { - <&[u8] as Type>::type_info() - } - - fn compatible(ty: &SqliteTypeInfo) -> bool { - <&[u8] as Type>::compatible(ty) - } -} - -impl<'q> Encode<'q, Sqlite> for CidBlob { - fn encode(self, args: &mut Vec>) -> IsNull { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self.0.to_bytes()))); - - IsNull::No - } - - fn encode_by_ref(&self, args: &mut Vec>) -> IsNull { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self.0.to_bytes()))); - - IsNull::No - } -} - -impl<'r> Decode<'r, Sqlite> for CidBlob { - fn decode(value: SqliteValueRef<'r>) -> Result { - let v: &[u8] = <&[u8] as Decode>::decode(value)?; - let cid = Cid::try_from(v)?; - Ok(CidBlob(cid)) - } -} - -impl FromRow<'_, SqliteRow> for CidBlob { - fn from_row(row: &SqliteRow) -> std::result::Result { - let v: Vec = row.get(0); - let cid = Cid::try_from(v.as_slice()).map_err(|e| sqlx::Error::Decode(Box::new(e)))?; - Ok(CidBlob(cid)) - } -}