From 87d802fa2e841ac3e4ff7a42a42b4a48cef12991 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 13 Apr 2024 06:34:56 +0200 Subject: [PATCH] fix(apb): .id() now works on json strings too --- apb/src/types/base.rs | 17 +++++++++++++++-- apb/src/types/link.rs | 14 ++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/apb/src/types/base.rs b/apb/src/types/base.rs index c9065cd..ee78e70 100644 --- a/apb/src/types/base.rs +++ b/apb/src/types/base.rs @@ -32,8 +32,21 @@ impl Base for String { #[cfg(feature = "unstructured")] impl Base for serde_json::Value { - crate::getter! { id -> &str } - crate::getter! { base_type -> type BaseType } + fn base_type(&self) -> Option { + if self.is_string() { + Some(BaseType::Link(LinkType::Link)) + } else { + self.get("type")?.as_str()?.try_into().ok() + } + } + + fn id(&self) -> Option<&str> { + if self.is_string() { + self.as_str() + } else { + self.get("id").map(|x| x.as_str())? + } + } } #[cfg(feature = "unstructured")] diff --git a/apb/src/types/link.rs b/apb/src/types/link.rs index 31de5d7..438dd53 100644 --- a/apb/src/types/link.rs +++ b/apb/src/types/link.rs @@ -37,16 +37,10 @@ impl Link for String { impl Link for serde_json::Value { // TODO this can fail, but it should never do! fn href(&self) -> &str { - match self { - serde_json::Value::String(x) => x, - serde_json::Value::Object(map) => - map.get("href") - .map(|h| h.as_str().unwrap_or("")) - .unwrap_or(""), - _ => { - tracing::error!("failed getting href on invalid json Link object"); - "" - }, + if self.is_string() { + self.as_str().unwrap_or("") + } else { + self.get("href").map(|x| x.as_str().unwrap_or("")).unwrap_or("") } }