Update serialization to 0.10

This commit is contained in:
asonix 2022-12-26 12:55:31 -06:00
parent 9d1d532f94
commit f3edf1b908

View file

@ -7,7 +7,7 @@ use json_ld::{
};
use json_ld_syntax::Parse;
use locspan::Meta;
use rdf_types::Vocabulary;
use rdf_types::{Term, Vocabulary};
use smallvec::SmallVec;
use std::{cell::RefCell, hash::Hash, rc::Rc};
@ -26,8 +26,8 @@ use input_dataset::InputDataset;
use json_value::{subject_json, Array, JsonValue, Map, WithMeta};
use object_entry::ObjectEntry;
use rdf::{
expect_iri, get_iri, subject_matches, subject_string, NormalizingQuad, QuadValue, RDF_LANGUAGE,
RDF_LIST,
expect_iri, get_iri, subject_matches, subject_string, NormalizingQuad, QuadSubject, QuadValue,
RDF_LANGUAGE, RDF_LIST,
};
use referenced_once::{ReferencedOnce, ReferencedValue};
use subject_entry::SubjectEntry;
@ -126,18 +126,20 @@ where
}
// step 5.7.4
if let json_ld::rdf::Value::Reference(object) = triple.object() {
if let Some(object) = subject_object::<N>(triple.object()) {
node_map
.borrow_mut()
.object_entry(object, vocabulary)
.or_insert_with(|| Rc::new(RefCell::new(ObjectEntry::new(object, vocabulary))));
.object_entry(&object, vocabulary)
.or_insert_with(|| {
Rc::new(RefCell::new(ObjectEntry::new(&object, vocabulary)))
});
}
// step 5.7.5
if subject_matches(triple.predicate(), RDF_TYPE, vocabulary) && !use_rdf_type {
if let json_ld::rdf::Value::Reference(object) = triple.object() {
if node.borrow().contains_type(object, vocabulary) {
node.borrow_mut().insert_type(object, vocabulary);
if let Some(object) = subject_object::<N>(triple.object()) {
if node.borrow().contains_type(&object, vocabulary) {
node.borrow_mut().insert_type(&object, vocabulary);
}
continue;
@ -170,9 +172,9 @@ where
);
}
if let json_ld::rdf::Value::Reference(object) = triple.object() {
if let Some(object) = subject_object::<N>(triple.object()) {
// step 5.7.9
if subject_matches(object, RDF_NIL, vocabulary) {
if subject_matches(&object, RDF_NIL, vocabulary) {
// step 5.7.9.1
let mut node_map = node_map.borrow_mut();
let usages = node_map.usages_entry();
@ -188,17 +190,14 @@ where
}
drop(node_map);
} else if let Some(value) =
referenced_once.get_mut(&subject_string(object, vocabulary))
referenced_once.get_mut(&subject_string(&object, vocabulary))
{
// step 5.7.10
value.set_false();
} else if matches!(
triple.object(),
json_ld::rdf::Value::Reference(Subject::Blank(_))
) {
} else if matches!(triple.object(), json_ld::rdf::Value::Blank(_)) {
// step 5.7.11, 5.7.11.1
referenced_once.insert(
subject_string(object, vocabulary),
subject_string(&object, vocabulary),
ReferencedValue::new(node, triple.predicate(), value, vocabulary),
)
}
@ -419,6 +418,19 @@ where
Ok(WithMeta(result, meta).into())
}
fn subject_object<N>(object: &QuadValue<N>) -> Option<QuadSubject<N>>
where
N: Vocabulary,
N::BlankId: Clone,
N::Iri: Clone,
{
match object {
Term::Iri(iri) => Some(Subject::Iri(iri.clone())),
Term::Blank(blank) => Some(Subject::Blank(blank.clone())),
Term::Literal(_) => None,
}
}
fn rdf_to_object<N, M>(
value: QuadValue<N>,
meta: M,
@ -433,13 +445,22 @@ where
{
match value {
// step 1
json_ld::rdf::Value::Reference(subject) => {
Term::Iri(iri) => {
let mut map = Map::new();
map.insert("@id".into(), subject_json(&subject, vocabulary));
map.insert("@id".into(), subject_json(&Subject::Iri(iri), vocabulary));
Ok(Rc::new(RefCell::new(map)))
}
Term::Blank(blank) => {
let mut map = Map::new();
map.insert(
"@id".into(),
subject_json(&Subject::Blank(blank), vocabulary),
);
Ok(Rc::new(RefCell::new(map)))
}
// step 2
json_ld::rdf::Value::Literal(literal) => {
Term::Literal(literal) => {
// step 2.1
let mut result = Map::new();