diff --git a/services/comm-services-lib/src/blob/mod.rs b/services/comm-services-lib/src/blob/mod.rs --- a/services/comm-services-lib/src/blob/mod.rs +++ b/services/comm-services-lib/src/blob/mod.rs @@ -1,2 +1,3 @@ #[cfg(feature = "blob-client")] pub mod client; +pub mod types; diff --git a/services/comm-services-lib/src/blob/types.rs b/services/comm-services-lib/src/blob/types.rs new file mode 100644 --- /dev/null +++ b/services/comm-services-lib/src/blob/types.rs @@ -0,0 +1,45 @@ +use aws_sdk_dynamodb::types::AttributeValue; +use derive_more::Constructor; +use std::collections::HashMap; + +use crate::database::{AttributeTryInto, DBItemError, TryFromAttribute}; + +/// Blob owning information - stores both blob_hash and holder +#[derive(Clone, Debug, Constructor)] +pub struct BlobInfo { + pub blob_hash: String, + pub holder: String, +} + +impl From for AttributeValue { + fn from(value: BlobInfo) -> Self { + let map = HashMap::from([ + ("blob_hash".to_string(), AttributeValue::S(value.blob_hash)), + ("holder".to_string(), AttributeValue::S(value.holder)), + ]); + AttributeValue::M(map) + } +} +impl From<&BlobInfo> for AttributeValue { + fn from(value: &BlobInfo) -> Self { + AttributeValue::from(value.to_owned()) + } +} + +impl TryFromAttribute for BlobInfo { + fn try_from_attr( + attribute_name: impl Into, + attribute: Option, + ) -> Result { + let attr_name: String = attribute_name.into(); + let mut inner_map: HashMap = + attribute.attr_try_into(&attr_name)?; + let blob_hash = inner_map + .remove("blob_hash") + .attr_try_into(format!("{attr_name}.blob_hash"))?; + let holder = inner_map + .remove("holder") + .attr_try_into(format!("{attr_name}.holder"))?; + Ok(BlobInfo { blob_hash, holder }) + } +}