From 678daac2eb07006beb903f6cc810006733a67e80 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 30 Dec 2024 02:25:24 +0100 Subject: [PATCH] `astreams`: Implement u64 processing and Link.height and Link.width --- acrate_astreams/src/activitystreams/jsonld.rs | 62 +++++++++++++++++++ acrate_astreams/src/activitystreams/mod.rs | 9 ++- acrate_astreams/tests/test_activitystreams.rs | 41 +++++++++++- 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/acrate_astreams/src/activitystreams/jsonld.rs b/acrate_astreams/src/activitystreams/jsonld.rs index ba84642..a930496 100644 --- a/acrate_astreams/src/activitystreams/jsonld.rs +++ b/acrate_astreams/src/activitystreams/jsonld.rs @@ -17,6 +17,8 @@ pub trait StreamsJsonLD { fn jsonld_any_value_mediatype(&self, id: &Iri) -> Option>; fn jsonld_any_node_string(&self, id: &Iri) -> Option>; fn jsonld_any_value_langtag(&self, id: &Iri) -> Option>; + fn jsonld_any_value_u32(&self, id: &Iri) -> Option>; + fn jsonld_any_value_u64(&self, id: &Iri) -> Option>; } impl StreamsJsonLD for &json_ld::Node { @@ -172,6 +174,54 @@ impl StreamsJsonLD for &json_ld::Node { Some(Ok(langtag)) } + + fn jsonld_any_value_u32(&self, id: &Iri) -> Option> { + let property = match self.properties.get_any(&id) { + None => return None, + Some(property) => property, + }; + + let value = match property.as_value() { + None => return Some(Err(anyhow!("Couldn't process property as JSON-LD value"))), + Some(value) => value, + }; + + let number = match value.as_number() { + None => return Some(Err(anyhow!("Couldn't process property as JSON-LD number"))), + Some(number) => number + }; + + let r#u32 = match number.as_u32() { + None => return Some(Err(anyhow!("Couldn't losslessly convert JSON-LD number to u64"))), + Some(r#u32) => r#u32, + }; + + Some(Ok(r#u32)) + } + + fn jsonld_any_value_u64(&self, id: &Iri) -> Option> { + let property = match self.properties.get_any(&id) { + None => return None, + Some(property) => property, + }; + + let value = match property.as_value() { + None => return Some(Err(anyhow!("Couldn't process property as JSON-LD value"))), + Some(value) => value, + }; + + let number = match value.as_number() { + None => return Some(Err(anyhow!("Couldn't process property as JSON-LD number"))), + Some(number) => number + }; + + let r#u64 = match number.as_u64() { + None => return Some(Err(anyhow!("Couldn't losslessly convert JSON-LD number to u64"))), + Some(r#u64) => r#u64, + }; + + Some(Ok(r#u64)) + } } impl StreamsLink for &json_ld::Node { @@ -204,4 +254,16 @@ impl StreamsLink for &json_ld::Node { iri!("https://www.w3.org/ns/activitystreams#hreflang") ) } + + fn activitystreams_height(&self) -> Option> { + self.jsonld_any_value_u64( + iri!("https://www.w3.org/ns/activitystreams#height") + ) + } + + fn activitystreams_width(&self) -> Option> { + self.jsonld_any_value_u64( + iri!("https://www.w3.org/ns/activitystreams#width") + ) + } } diff --git a/acrate_astreams/src/activitystreams/mod.rs b/acrate_astreams/src/activitystreams/mod.rs index 8021876..dff8614 100644 --- a/acrate_astreams/src/activitystreams/mod.rs +++ b/acrate_astreams/src/activitystreams/mod.rs @@ -13,9 +13,8 @@ use anyhow::Result as AResult; use json_ld::Direction; -use json_ld::syntax::{LangTag, LangTagBuf}; +use json_ld::syntax::LangTagBuf; use mediatype::MediaType; -use static_iref::iri; pub mod jsonld; @@ -36,4 +35,10 @@ pub trait StreamsLink { fn activitystreams_names(&self) -> impl Iterator, Option)>>; fn activitystreams_hreflang(&self) -> Option>; + + // FIXME: This doesn't accept numbers greater than u64 + fn activitystreams_height(&self) -> Option>; + + // FIXME: This doesn't accept numbers greater than u64 + fn activitystreams_width(&self) -> Option>; } diff --git a/acrate_astreams/tests/test_activitystreams.rs b/acrate_astreams/tests/test_activitystreams.rs index 72dd1b5..83bae47 100644 --- a/acrate_astreams/tests/test_activitystreams.rs +++ b/acrate_astreams/tests/test_activitystreams.rs @@ -1,5 +1,6 @@ use json_ld::syntax::LangTagBuf; use acrate_astreams::activitystreams::jsonld::LangTriple; +use acrate_astreams::activitystreams::StreamsLink; macro_rules! test_example { ($modname:ident, $filename:literal) => { @@ -340,9 +341,45 @@ async fn test_link_hreflang() { use acrate_astreams::activitystreams::StreamsLink; node.activitystreams_hreflang() - .expect("Property `mediaType` was not found") - .expect("Property `mediaType` failed to process") + .expect("Property `hreflang` was not found") + .expect("Property `hreflang` failed to process") }; assert_eq!(hreflang, "en") } + +#[tokio::test] +async fn test_link_height() { + let doc = e120::expand().await; + + let node = doc.main_node() + .expect("Main node was not found"); + + let height = { + use acrate_astreams::activitystreams::StreamsLink; + + node.activitystreams_width() + .expect("Property `height` was not found") + .expect("Property `height` failed to process") + }; + + assert_eq!(height, 100u64) +} + +#[tokio::test] +async fn test_link_width() { + let doc = e138::expand().await; + + let node = doc.main_node() + .expect("Main node was not found"); + + let width = { + use acrate_astreams::activitystreams::StreamsLink; + + node.activitystreams_width() + .expect("Property `width` was not found") + .expect("Property `width` failed to process") + }; + + assert_eq!(width, 100u64) +}