Page MenuHomePhabricator

D8789.id29832.diff
No OneTemporary

D8789.id29832.diff

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

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)

Event Timeline