astreams: i am losing my mind

This commit is contained in:
Steffo 2025-02-11 09:19:31 +01:00
parent 0c5dc9e5ca
commit c7f66036a2
Signed by: steffo
GPG key ID: 6B8E18743E7E1F86
6 changed files with 115 additions and 502 deletions

View file

@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Test">
<option name="buildProfileId" value="dev" />
<option name="buildProfileId" value="test" />
<option name="command" value="test" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs>

View file

@ -271,7 +271,7 @@ impl HasLinkedDataInterface for Node {
let direction = v.direction();
let langdir = match (language, direction) {
(None, None) => return Err(anyhow!("Expected property to have either a valid language tag, or a valid direction")),
(None, None) => LangDir::Neither,
(Some(language), None) => LangDir::Language(language),
(None, Some(direction)) => LangDir::Direction(direction),
(Some(language), Some(direction)) => LangDir::Both(language, direction),
@ -501,17 +501,27 @@ impl HasLinkedDataInterface for Node {
let string = SynString::from(string);
let (language, direction) = match langdir {
LangDir::Neither => (None, None),
LangDir::Language(language) => (Some(language), None),
LangDir::Direction(direction) => (None, Some(direction)),
LangDir::Both(language, direction) => (Some(language), Some(direction))
};
let language = language.map(|l| l.into());
let langstring = json_ld::LangString::new(string, language, direction)
.unwrap(); // We've enforced the language and direction constraints via enum.
let value = CoreValue::LangString(langstring);
let value = match (&language, &direction) {
(None, None) => {
let json = SynValue::String(string);
CoreValue::Json(json)
},
(_, _) => {
let language = language.map(|l| l.into());
let langstring = json_ld::LangString::new(string, language, direction)
.unwrap(); // We've enforced the language and direction constraints via enum.
CoreValue::LangString(langstring)
},
};
let object: Object = Object::Value(value);

View file

@ -6,7 +6,9 @@ use json_ld::Direction;
mod jsonld;
#[derive(Debug, Clone)]
pub enum LangDir {
Neither,
Language(LangTagBuf),
Direction(Direction),
Both(LangTagBuf, Direction),

View file

@ -93,6 +93,7 @@ macro_rules! vocab {
) => {
$( #[ $vocab_ref_meta ] )*
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct $vocab_ref<'b, B>
where B: $crate::linkeddata::HasLinkedDataInterface
{
@ -101,6 +102,7 @@ macro_rules! vocab {
$( #[ $vocab_mut_meta ] )*
#[repr(transparent)]
#[derive(Debug)]
pub struct $vocab_mut<'b, B>
where B: $crate::linkeddata::HasLinkedDataInterface
{

View file

@ -1,494 +0,0 @@
macro_rules! fixture_example {
($filename:literal) => {
use json_ld::syntax::Parse;
use json_ld::Expand;
pub const DATA: &'static str = include_str!($filename);
pub fn get_parsed_document() -> json_ld::RemoteDocument {
let (value, _codemap) = json_ld::syntax::Value::parse_str(DATA).expect("Failed to parse example");
let document = json_ld::RemoteDocument::new(
None,
None,
value,
);
document
}
pub async fn get_expanded_document() -> json_ld::ExpandedDocument {
let mut loader = json_ld::ReqwestLoader::new();
let doc = get_parsed_document();
let expanded = doc.expand(&mut loader)
.await
.expect("Failed to expand JSON-LD document");
expanded
}
#[test]
fn parse() {
let doc = get_parsed_document();
println!("{doc:#?}");
}
#[tokio::test]
async fn expand() {
let doc = get_expanded_document().await;
println!("{doc:#?}");
}
}
}
macro_rules! test_example {
($modname:ident: $filename:literal) => {
mod $modname {
fixture_example!($filename);
}
};
}
macro_rules! test_example_property_any {
($modname:ident: $filename:literal, $entity:path, $meth:ident == $value:expr) => {
mod $modname {
fixture_example!($filename);
#[tokio::test]
async fn $meth() {
let doc = get_expanded_document().await;
let node = doc.main_node()
.expect("Main node was not found");
let property = {
use $entity;
node.$meth()
.expect("Property was not found")
.expect("Property failed to process")
};
println!("{property:#?}");
assert_eq!(property, $value);
}
}
};
}
macro_rules! test_example_property_iter {
($modname:ident: $filename:literal, $entity:path, $meth:ident == $value:expr) => {
mod $modname {
fixture_example!($filename);
#[tokio::test]
async fn $meth() {
let doc = get_expanded_document().await;
let node = doc.main_node()
.expect("Main node was not found");
let mut vec = vec![];
{
use $entity;
vec.extend(
node.$meth()
.map(|v| v
.expect("Property failed to process")
)
)
}
println!("{vec:#?}");
assert_eq!(vec, $value);
}
}
};
}
test_example!(e001: "./activitystreams/examples/1.json");
test_example!(e002: "./activitystreams/examples/2.json");
test_example!(e003: "./activitystreams/examples/3.json");
test_example!(e004: "./activitystreams/examples/4.json");
test_example!(e005: "./activitystreams/examples/5.json");
test_example!(e006: "./activitystreams/examples/6.json");
test_example!(e007: "./activitystreams/examples/7.json");
test_example!(e008: "./activitystreams/examples/8.json");
test_example!(e009: "./activitystreams/examples/9.json");
test_example!(e010: "./activitystreams/examples/10.json");
test_example!(e011: "./activitystreams/examples/11.json");
test_example!(e012: "./activitystreams/examples/12.json");
test_example!(e013: "./activitystreams/examples/13.json");
test_example!(e014: "./activitystreams/examples/14.json");
test_example!(e015: "./activitystreams/examples/15.json");
test_example!(e016: "./activitystreams/examples/16.json");
test_example!(e017: "./activitystreams/examples/17.json");
test_example!(e018: "./activitystreams/examples/18.json");
test_example!(e019: "./activitystreams/examples/19.json");
test_example!(e020: "./activitystreams/examples/20.json");
test_example!(e021: "./activitystreams/examples/21.json");
test_example!(e022: "./activitystreams/examples/22.json");
test_example!(e023: "./activitystreams/examples/23.json");
test_example!(e024: "./activitystreams/examples/24.json");
test_example!(e025: "./activitystreams/examples/25.json");
test_example!(e026: "./activitystreams/examples/26.json");
test_example!(e027: "./activitystreams/examples/27.json");
test_example!(e028: "./activitystreams/examples/28.json");
test_example!(e029: "./activitystreams/examples/29.json");
test_example!(e030: "./activitystreams/examples/30.json");
test_example!(e031: "./activitystreams/examples/31.json");
test_example!(e032: "./activitystreams/examples/32.json");
test_example!(e033: "./activitystreams/examples/33.json");
test_example!(e034: "./activitystreams/examples/34.json");
test_example!(e035: "./activitystreams/examples/35.json");
test_example!(e036: "./activitystreams/examples/36.json");
test_example!(e037: "./activitystreams/examples/37.json");
test_example!(e038: "./activitystreams/examples/38.json");
test_example!(e039: "./activitystreams/examples/39.json");
test_example!(e040: "./activitystreams/examples/40.json");
test_example!(e041: "./activitystreams/examples/41.json");
test_example!(e042: "./activitystreams/examples/42.json");
test_example!(e043: "./activitystreams/examples/43.json");
test_example!(e044: "./activitystreams/examples/44.json");
test_example!(e045: "./activitystreams/examples/45.json");
test_example!(e046: "./activitystreams/examples/46.json");
test_example!(e047: "./activitystreams/examples/47.json");
test_example!(e048: "./activitystreams/examples/48.json");
test_example!(e049: "./activitystreams/examples/49.json");
test_example!(e050: "./activitystreams/examples/50.json");
test_example!(e051: "./activitystreams/examples/51.json");
test_example!(e052: "./activitystreams/examples/52.json");
test_example!(e053: "./activitystreams/examples/53.json");
test_example!(e054: "./activitystreams/examples/54.json");
test_example!(e055: "./activitystreams/examples/55.json");
test_example!(e056: "./activitystreams/examples/56.json");
test_example!(e057: "./activitystreams/examples/57.json");
test_example!(e058: "./activitystreams/examples/58.json");
test_example!(e059: "./activitystreams/examples/59.json");
test_example!(e060: "./activitystreams/examples/60.json");
test_example!(e061: "./activitystreams/examples/61.json");
test_example!(e062: "./activitystreams/examples/62.json");
test_example!(e063: "./activitystreams/examples/63.json");
test_example!(e064: "./activitystreams/examples/64.json");
test_example!(e065: "./activitystreams/examples/65.json");
test_example!(e066: "./activitystreams/examples/66.json");
test_example!(e067: "./activitystreams/examples/67.json");
test_example!(e068: "./activitystreams/examples/68.json");
test_example!(e069: "./activitystreams/examples/69.json");
test_example!(e070: "./activitystreams/examples/70.json");
test_example!(e071: "./activitystreams/examples/71.json");
test_example!(e072: "./activitystreams/examples/72.json");
test_example!(e073: "./activitystreams/examples/73.json");
test_example!(e074: "./activitystreams/examples/74.json");
test_example!(e075: "./activitystreams/examples/75.json");
test_example!(e076: "./activitystreams/examples/76.json");
test_example!(e077: "./activitystreams/examples/77.json");
test_example!(e078: "./activitystreams/examples/78.json");
test_example!(e079: "./activitystreams/examples/79.json");
test_example!(e080: "./activitystreams/examples/80.json");
test_example!(e081: "./activitystreams/examples/81.json");
test_example!(e082: "./activitystreams/examples/82.json");
test_example!(e083: "./activitystreams/examples/83.json");
test_example!(e084: "./activitystreams/examples/84.json");
test_example!(e085: "./activitystreams/examples/85.json");
test_example!(e086: "./activitystreams/examples/86.json");
test_example!(e087: "./activitystreams/examples/87.json");
test_example!(e088: "./activitystreams/examples/88.json");
test_example!(e089: "./activitystreams/examples/89.json");
test_example!(e090: "./activitystreams/examples/90.json");
test_example!(e091: "./activitystreams/examples/91.json");
test_example!(e092: "./activitystreams/examples/92.json");
test_example!(e093: "./activitystreams/examples/93.json");
test_example!(e094: "./activitystreams/examples/94.json");
test_example!(e095: "./activitystreams/examples/95.json");
test_example!(e096: "./activitystreams/examples/96.json");
test_example!(e097: "./activitystreams/examples/97.json");
test_example!(e098: "./activitystreams/examples/98.json");
test_example!(e099: "./activitystreams/examples/99.json");
test_example!(e100: "./activitystreams/examples/100.json");
test_example!(e101: "./activitystreams/examples/101.json");
test_example!(e102: "./activitystreams/examples/102.json");
test_example!(e103: "./activitystreams/examples/103.json");
test_example!(e104: "./activitystreams/examples/104.json");
test_example!(e105: "./activitystreams/examples/105.json");
test_example!(e106: "./activitystreams/examples/106.json");
test_example!(e107: "./activitystreams/examples/107.json");
test_example!(e108: "./activitystreams/examples/108.json");
test_example!(e109: "./activitystreams/examples/109.json");
test_example!(e110: "./activitystreams/examples/110.json");
test_example!(e111: "./activitystreams/examples/111.json");
test_example!(e112: "./activitystreams/examples/112.json");
test_example!(e113: "./activitystreams/examples/113.json");
test_example_property_iter!(e114: "./activitystreams/examples/114.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_content == &[
("A <em>simple</em> note".to_string(), None, None)
]
);
test_example_property_iter!(e115: "./activitystreams/examples/115.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_content == &[
("A <em>simple</em> note".to_string(), Some(json_ld::syntax::LangTagBuf::new("en".to_string()).unwrap()), None),
("Una nota <em>sencilla</em>".to_string(), Some(json_ld::syntax::LangTagBuf::new("es".to_string()).unwrap()), None),
("一段<em>简单的</em>笔记".to_string(), Some(json_ld::syntax::LangTagBuf::new("zh-Hans".to_string()).unwrap()), None),
]
);
test_example_property_iter!(e116: "./activitystreams/examples/116.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_content == &[
("## A simple note\nA simple markdown `note`".to_string(), None, None)
]
);
test_example_property_iter!(e117: "./activitystreams/examples/117.json",
acrate_astreams::activitystreams::StreamsEntity,
activitystreams_names == &[
("A simple note".to_string(), None, None)
]
);
test_example_property_iter!(e118: "./activitystreams/examples/118.json",
acrate_astreams::activitystreams::StreamsEntity,
activitystreams_names == &[
("A simple note".to_string(), Some(json_ld::syntax::LangTagBuf::new("en".to_string()).unwrap()), None),
("Una nota sencilla".to_string(), Some(json_ld::syntax::LangTagBuf::new("es".to_string()).unwrap()), None),
("一段简单的笔记".to_string(), Some(json_ld::syntax::LangTagBuf::new("zh-Hans".to_string()).unwrap()), None),
]
);
test_example_property_any!(e119: "./activitystreams/examples/119.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_duration == chrono::TimeDelta::new(2 * 60 * 60, 0).unwrap()
);
test_example_property_any!(e120: "./activitystreams/examples/120.json",
acrate_astreams::activitystreams::StreamsLink,
activitystreams_width == 100u64
);
test_example_property_any!(e121: "./activitystreams/examples/121.json",
acrate_astreams::activitystreams::StreamsLink,
activitystreams_href == "http://example.org/abc"
);
test_example_property_any!(e122: "./activitystreams/examples/122.json",
acrate_astreams::activitystreams::StreamsLink,
activitystreams_hreflang == "en"
);
test_example!(e123: "./activitystreams/examples/123.json");
test_example!(e124: "./activitystreams/examples/124.json");
test_example!(e125: "./activitystreams/examples/125.json");
test_example_property_any!(e126: "./activitystreams/examples/126.json",
acrate_astreams::activitystreams::StreamsEntity,
activitystreams_mediatype == mediatype::media_type!(TEXT/HTML)
);
test_example!(e127: "./activitystreams/examples/127.json");
test_example!(e128: "./activitystreams/examples/128.json");
test_example!(e129: "./activitystreams/examples/129.json");
test_example!(e130: "./activitystreams/examples/130.json");
test_example_property_iter!(e131: "./activitystreams/examples/131.json",
acrate_astreams::activitystreams::StreamsLink,
activitystreams_rels_lenient == &[
"canonical",
"preview",
]
);
test_example!(e132: "./activitystreams/examples/132.json");
test_example_property_iter!(e133: "./activitystreams/examples/133.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_summary == &[
("A simple <em>note</em>".to_string(), None, None)
]
);
test_example_property_iter!(e134: "./activitystreams/examples/134.json",
acrate_astreams::activitystreams::StreamsObject,
activitystreams_summary == &[
("A simple <em>note</em>".to_string(), Some(json_ld::syntax::LangTagBuf::new("en".to_string()).unwrap()), None),
("Una <em>nota</em> sencilla".to_string(), Some(json_ld::syntax::LangTagBuf::new("es".to_string()).unwrap()), None),
("一段<em>简单的</em>笔记".to_string(), Some(json_ld::syntax::LangTagBuf::new("zh-Hans".to_string()).unwrap()), None),
]
);
test_example!(e135: "./activitystreams/examples/135.json");
test_example!(e136: "./activitystreams/examples/136.json");
test_example!(e137: "./activitystreams/examples/137.json");
test_example_property_any!(e138: "./activitystreams/examples/138.json",
acrate_astreams::activitystreams::StreamsLink,
activitystreams_width == 100u64
);
test_example!(e139: "./activitystreams/examples/139.json");
test_example!(e140: "./activitystreams/examples/140.json");
test_example!(e141: "./activitystreams/examples/141.json");
test_example!(e142: "./activitystreams/examples/142.json");
test_example!(e143: "./activitystreams/examples/143.json");
test_example!(e144: "./activitystreams/examples/144.json");
test_example!(e145: "./activitystreams/examples/145.json");
test_example!(e146: "./activitystreams/examples/146.json");
test_example!(e147: "./activitystreams/examples/147.json");
test_example!(e148: "./activitystreams/examples/148.json");
test_example!(e149: "./activitystreams/examples/149.json");
test_example!(e150: "./activitystreams/examples/150.json");
test_example!(e151: "./activitystreams/examples/151.json");
test_example!(e152: "./activitystreams/examples/152.json");
test_example!(e153: "./activitystreams/examples/153.json");
test_example!(e154: "./activitystreams/examples/154.json");
test_example!(e155: "./activitystreams/examples/155.json");
test_example!(e156: "./activitystreams/examples/156.json");
test_example!(e157: "./activitystreams/examples/157.json");
test_example!(e158: "./activitystreams/examples/158.json");
test_example!(e159: "./activitystreams/examples/159.json");

View file

@ -0,0 +1,93 @@
use std::str::FromStr;
use iref::IriBuf;
use json_ld::{BlankIdBuf, Compact, Flatten, Print, RemoteDocumentReference};
use json_ld::rdf_types::generator::Blank;
use json_ld::rdf_types::vocabulary::{BlankIdIndex, IndexVocabulary, IriIndex, IriVocabularyMut, LiteralIndex};
use json_ld::syntax::{Context, IntoJsonWithContext, LangTagBuf};
use json_ld::context_processing::{Process};
use static_iref::{iri, iri_ref};
use acrate_astreams::linkeddata::LangDir;
use acrate_astreams::linkeddata::IsLinkedDataWriteWrapper;
use acrate_astreams::vocabulary::activitystreams::ObjectMut;
macro_rules! jsonld_document {
($modname:ident: $filename:literal) => {
mod $modname {
pub const TEXT: &'static str = include_str!($filename);
pub fn value() -> json_ld::syntax::Value {
use json_ld::syntax::Parse;
let (value, _codemap) = json_ld::syntax::Value::parse_str(TEXT)
.expect("Failed to parse example");
value
}
pub fn remote_document() -> json_ld::RemoteDocument {
let document = json_ld::RemoteDocument::new(None, None, value());
document
}
pub async fn expanded_document() -> json_ld::ExpandedDocument {
use json_ld::Expand;
let mut loader = json_ld::ReqwestLoader::new();
let document = remote_document()
.expand(&mut loader)
.await
.expect("Failed to expand JSON-LD document");
document
}
pub async fn main_node() -> json_ld::Node {
let node = expanded_document()
.await
.into_main_node()
.expect("Failed to get the main node of the expanded document");
node
}
}
}
}
jsonld_document!(ex: "activitystreams/examples/118.json");
#[tokio::test]
async fn test_something() -> anyhow::Result<()> {
let mut node = ex::main_node().await;
{
let mut object = ObjectMut::from_mut(&mut node);
object.set_name(vec![
("NoLanguage1".to_string(), LangDir::Neither),
("English1".to_string(), LangDir::Language(LangTagBuf::from_str("en")?)),
("Italian1".to_string(), LangDir::Language(LangTagBuf::from_str("it")?))
])?;
}
let doc = json_ld::ser::serialize(&node)?;
let mut generator = Blank::new();
let flattened = doc.flatten(&mut generator, true)?;
let mut loader = json_ld::ReqwestLoader::new();
let mut vocabulary = IndexVocabulary::new();
let context = Context::iri_ref(iri_ref!("https://www.w3.org/ns/activitystreams").to_owned());
let processed = context.process(&mut vocabulary, &loader, None).await?;
let processed_ref = processed.as_ref();
let value = flattened.compact(processed_ref, &mut loader).await?;
let print = value.pretty_print();
println!("{print}");
Ok(())
}