Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3385431
D8789.id29832.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D8789.id29832.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,40 @@
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 attributevalues 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
Sat, Nov 30, 12:20 AM (20 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2598774
Default Alt Text
D8789.id29832.diff (1 KB)
Attached To
Mode
D8789: [services-lib] Add DDB Attribute conversion traits
Attached
Detach File
Event Timeline
Log In to Comment