feat(apb): helper: Option<String> to Field<&str>

This commit is contained in:
əlemi 2024-06-01 01:49:29 +02:00
parent 456ca2d8b1
commit ab006ffde9
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 21 additions and 6 deletions

18
apb/src/field.rs Normal file
View file

@ -0,0 +1,18 @@
#[derive(Debug, thiserror::Error)]
#[error("missing field '{0}'")]
pub struct FieldErr(pub &'static str);
pub type Field<T> = Result<T, FieldErr>;
// TODO this trait is really ad-hoc and has awful naming...
pub trait OptionalString {
fn str(self) -> Option<String>;
}
impl OptionalString for Field<&str> {
fn str(self) -> Option<String> {
self.ok().map(|x| x.to_string())
}
}

View file

@ -98,6 +98,9 @@ pub mod target;
mod key; mod key;
pub use key::{PublicKey, PublicKeyMut}; pub use key::{PublicKey, PublicKeyMut};
pub mod field;
pub use field::{Field, FieldErr};
#[cfg(feature = "jsonld")] #[cfg(feature = "jsonld")]
mod 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<T> = Result<T, FieldErr>;
#[cfg(feature = "unstructured")] #[cfg(feature = "unstructured")]
pub fn new() -> serde_json::Value { pub fn new() -> serde_json::Value {
serde_json::Value::Object(serde_json::Map::default()) serde_json::Value::Object(serde_json::Map::default())