From ab006ffde9a7825f6c78a768463da88424ef70cf Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 1 Jun 2024 01:49:29 +0200 Subject: [PATCH] feat(apb): helper: Option to Field<&str> --- apb/src/field.rs | 18 ++++++++++++++++++ apb/src/lib.rs | 9 +++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 apb/src/field.rs diff --git a/apb/src/field.rs b/apb/src/field.rs new file mode 100644 index 00000000..336148e1 --- /dev/null +++ b/apb/src/field.rs @@ -0,0 +1,18 @@ +#[derive(Debug, thiserror::Error)] +#[error("missing field '{0}'")] +pub struct FieldErr(pub &'static str); + +pub type Field = Result; + + +// TODO this trait is really ad-hoc and has awful naming... + +pub trait OptionalString { + fn str(self) -> Option; +} + +impl OptionalString for Field<&str> { + fn str(self) -> Option { + self.ok().map(|x| x.to_string()) + } +} diff --git a/apb/src/lib.rs b/apb/src/lib.rs index bf0feb4e..691b9833 100644 --- a/apb/src/lib.rs +++ b/apb/src/lib.rs @@ -98,6 +98,9 @@ pub mod target; mod key; pub use key::{PublicKey, PublicKeyMut}; +pub mod field; +pub use field::{Field, FieldErr}; + #[cfg(feature = "jsonld")] mod jsonld; @@ -131,12 +134,6 @@ pub use types::{ }, }; -#[derive(Debug, thiserror::Error)] -#[error("missing field '{0}'")] -pub struct FieldErr(pub &'static str); - -pub type Field = Result; - #[cfg(feature = "unstructured")] pub fn new() -> serde_json::Value { serde_json::Value::Object(serde_json::Map::default())