use std::hash::Hash;
use jsonld_core::{ExpandedDocument, Node, Object};
use ld_core::{LinkedData, LinkedDataResource, LinkedDataSubject};
use rdfx::{
Interpretation,
interpretation::{ReverseInterpretation, ReverseLocalInterpretation},
vocabulary::Vocabulary,
};
mod expanded;
use expanded::SerializeExpandedDocument;
pub use expanded::{serialize_node_with, serialize_object_with};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid graph label")]
InvalidGraph,
#[error("invalid predicate")]
InvalidPredicate,
#[error("invalid node object")]
InvalidNode,
#[error("reverse properties on lists are not supported")]
ListReverseProperty,
#[error("included nodes on lists are not supported")]
ListInclude,
}
type DefaultInterpretation = rdfx::generator::LocalGeneratorInterpretation<rdfx::generator::Blank>;
fn default_interpretation() -> DefaultInterpretation {
rdfx::generator::LocalGeneratorInterpretation::new(rdfx::generator::Blank::new())
}
pub fn serialize(value: &impl LinkedData<DefaultInterpretation>) -> Result<ExpandedDocument, Error> {
serialize_with(&mut (), &mut default_interpretation(), value)
}
pub fn serialize_with<V, I>(vocabulary: &mut V, interpretation: &mut I, value: &impl LinkedData<I>) -> Result<ExpandedDocument<V::Iri, V::BlankId>, Error>
where
V: Vocabulary + rdfx::vocabulary::VocabularyMut,
V::Iri: Clone + Eq + Hash,
V::BlankId: Clone + Eq + Hash,
I: Interpretation + ReverseInterpretation + ReverseLocalInterpretation,
{
let serializer = SerializeExpandedDocument::new(vocabulary, interpretation);
value.visit(serializer)
}
pub fn serialize_object(value: &(impl LinkedDataSubject<DefaultInterpretation> + LinkedDataResource<DefaultInterpretation>)) -> Result<Object, Error> {
serialize_object_with(&mut (), &mut default_interpretation(), value)
}
pub fn serialize_node(value: &(impl LinkedDataSubject<DefaultInterpretation> + LinkedDataResource<DefaultInterpretation>)) -> Result<Node, Error> {
serialize_node_with(&mut (), &mut default_interpretation(), value)
}