Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32867934
D8789.1768133373.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D8789.1768133373.diff
View Options
diff --git a/services/comm-services-lib/src/database.rs b/services/comm-services-lib/src/database.rs
--- a/services/comm-services-lib/src/database.rs
+++ b/services/comm-services-lib/src/database.rs
@@ -63,6 +63,41 @@
InvalidNumberFormat(ParseIntError),
}
+/// Conversion trait for [`AttributeValue`]
+///
+/// Types implementing this trait are able to do the following:
+/// ```rust
+/// use comm_services_lib::database::{TryFromAttribute, AttributeTryInto};
+///
+/// let foo = SomeType::try_from_attr("MyAttribute", Some(attribute));
+///
+/// // if `AttributeTryInto` is imported, also:
+/// let bar = Some(attribute).attr_try_into("MyAttribute");
+pub trait TryFromAttribute: Sized {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError>;
+}
+
+/// Do NOT implement this trait directly. Implement [`TryFromAttribute`] instead
+pub trait AttributeTryInto<T> {
+ fn attr_try_into(
+ self,
+ attribute_name: impl Into<String>,
+ ) -> Result<T, DBItemError>;
+}
+// Automatic attr_try_into() for all attribute values
+// that have TryFromAttribute implemented
+impl<T: TryFromAttribute> AttributeTryInto<T> for Option<AttributeValue> {
+ fn attr_try_into(
+ self,
+ attribute_name: impl Into<String>,
+ ) -> Result<T, DBItemError> {
+ T::try_from_attr(attribute_name, self)
+ }
+}
+
pub fn parse_string_attribute(
attribute_name: impl Into<String>,
attribute_value: Option<AttributeValue>,
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Jan 11, 12:09 PM (1 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5916202
Default Alt Text
D8789.1768133373.diff (1 KB)
Attached To
Mode
D8789: [services-lib] Add DDB Attribute conversion traits
Attached
Detach File
Event Timeline
Log In to Comment