forked from alemi/upub
fix(apb): .id() now works on json strings too
This commit is contained in:
parent
9c9b5ec380
commit
87d802fa2e
2 changed files with 19 additions and 12 deletions
|
@ -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<BaseType> {
|
||||
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")]
|
||||
|
|
|
@ -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("")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue