astreams
: Implement u64 processing and Link.height and Link.width
This commit is contained in:
parent
690afcd15f
commit
678daac2eb
3 changed files with 108 additions and 4 deletions
|
@ -17,6 +17,8 @@ pub trait StreamsJsonLD {
|
||||||
fn jsonld_any_value_mediatype(&self, id: &Iri) -> Option<AResult<MediaType>>;
|
fn jsonld_any_value_mediatype(&self, id: &Iri) -> Option<AResult<MediaType>>;
|
||||||
fn jsonld_any_node_string(&self, id: &Iri) -> Option<AResult<String>>;
|
fn jsonld_any_node_string(&self, id: &Iri) -> Option<AResult<String>>;
|
||||||
fn jsonld_any_value_langtag(&self, id: &Iri) -> Option<AResult<LangTagBuf>>;
|
fn jsonld_any_value_langtag(&self, id: &Iri) -> Option<AResult<LangTagBuf>>;
|
||||||
|
fn jsonld_any_value_u32(&self, id: &Iri) -> Option<AResult<u32>>;
|
||||||
|
fn jsonld_any_value_u64(&self, id: &Iri) -> Option<AResult<u64>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamsJsonLD for &json_ld::Node {
|
impl StreamsJsonLD for &json_ld::Node {
|
||||||
|
@ -172,6 +174,54 @@ impl StreamsJsonLD for &json_ld::Node {
|
||||||
|
|
||||||
Some(Ok(langtag))
|
Some(Ok(langtag))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn jsonld_any_value_u32(&self, id: &Iri) -> Option<AResult<u32>> {
|
||||||
|
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<AResult<u64>> {
|
||||||
|
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 {
|
impl StreamsLink for &json_ld::Node {
|
||||||
|
@ -204,4 +254,16 @@ impl StreamsLink for &json_ld::Node {
|
||||||
iri!("https://www.w3.org/ns/activitystreams#hreflang")
|
iri!("https://www.w3.org/ns/activitystreams#hreflang")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn activitystreams_height(&self) -> Option<AResult<u64>> {
|
||||||
|
self.jsonld_any_value_u64(
|
||||||
|
iri!("https://www.w3.org/ns/activitystreams#height")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn activitystreams_width(&self) -> Option<AResult<u64>> {
|
||||||
|
self.jsonld_any_value_u64(
|
||||||
|
iri!("https://www.w3.org/ns/activitystreams#width")
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,8 @@
|
||||||
|
|
||||||
use anyhow::Result as AResult;
|
use anyhow::Result as AResult;
|
||||||
use json_ld::Direction;
|
use json_ld::Direction;
|
||||||
use json_ld::syntax::{LangTag, LangTagBuf};
|
use json_ld::syntax::LangTagBuf;
|
||||||
use mediatype::MediaType;
|
use mediatype::MediaType;
|
||||||
use static_iref::iri;
|
|
||||||
|
|
||||||
pub mod jsonld;
|
pub mod jsonld;
|
||||||
|
|
||||||
|
@ -36,4 +35,10 @@ pub trait StreamsLink {
|
||||||
fn activitystreams_names(&self) -> impl Iterator<Item = AResult<(String, Option<LangTagBuf>, Option<Direction>)>>;
|
fn activitystreams_names(&self) -> impl Iterator<Item = AResult<(String, Option<LangTagBuf>, Option<Direction>)>>;
|
||||||
|
|
||||||
fn activitystreams_hreflang(&self) -> Option<AResult<LangTagBuf>>;
|
fn activitystreams_hreflang(&self) -> Option<AResult<LangTagBuf>>;
|
||||||
|
|
||||||
|
// FIXME: This doesn't accept numbers greater than u64
|
||||||
|
fn activitystreams_height(&self) -> Option<AResult<u64>>;
|
||||||
|
|
||||||
|
// FIXME: This doesn't accept numbers greater than u64
|
||||||
|
fn activitystreams_width(&self) -> Option<AResult<u64>>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use json_ld::syntax::LangTagBuf;
|
use json_ld::syntax::LangTagBuf;
|
||||||
use acrate_astreams::activitystreams::jsonld::LangTriple;
|
use acrate_astreams::activitystreams::jsonld::LangTriple;
|
||||||
|
use acrate_astreams::activitystreams::StreamsLink;
|
||||||
|
|
||||||
macro_rules! test_example {
|
macro_rules! test_example {
|
||||||
($modname:ident, $filename:literal) => {
|
($modname:ident, $filename:literal) => {
|
||||||
|
@ -340,9 +341,45 @@ async fn test_link_hreflang() {
|
||||||
use acrate_astreams::activitystreams::StreamsLink;
|
use acrate_astreams::activitystreams::StreamsLink;
|
||||||
|
|
||||||
node.activitystreams_hreflang()
|
node.activitystreams_hreflang()
|
||||||
.expect("Property `mediaType` was not found")
|
.expect("Property `hreflang` was not found")
|
||||||
.expect("Property `mediaType` failed to process")
|
.expect("Property `hreflang` failed to process")
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(hreflang, "en")
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue