#rdf #sophia #parser #parse #serialization

rdf_dynsyn

This crate provides sophia-compatible and sophia-based rdf parsers/serializers, that can be instantiated against any of supported syntaxes dynamically at run time

7 releases

0.3.1 Sep 28, 2023
0.3.0 Sep 28, 2023
0.2.1 Sep 28, 2022
0.2.0 Aug 12, 2022
0.1.0 Jan 25, 2022

#711 in Web programming

Download history 38/week @ 2024-07-19 54/week @ 2024-07-26 27/week @ 2024-08-02 28/week @ 2024-08-09 20/week @ 2024-08-16 24/week @ 2024-08-23 36/week @ 2024-08-30 16/week @ 2024-09-06 19/week @ 2024-09-13 46/week @ 2024-09-20 25/week @ 2024-09-27 11/week @ 2024-10-04 18/week @ 2024-10-11 13/week @ 2024-10-18 7/week @ 2024-10-25 15/week @ 2024-11-01

54 downloads per month
Used in 12 crates (7 directly)

MIT/Apache

160KB
3.5K SLoC

rdf_dynsyn

This crate provides sophia-compatible and sophia-based rdf parsers/serializers, that can be instantiated against any of supported syntaxes dynamically at run time.

Why?

Although sophia provides specialized parsers/serializers for each syntax, we have to know document syntax at code-time to practically use them. In many cases of web, we may know syntax of a doc only at runtime, like from content-type, file-extn, etc. As each specialized parser parses to corresponding stream types, etc.. it will be difficult to work with them in such dynamic cases. For Handling such cases this crate provides well-tested abstractions, that integrates into sophia eco-system.

Getting Started

Following is a short example on how to get syntax from media-types/ file-extensions, and instantiate parser for detected syntax, parse content,mutate it and serialize back into desired syntax. Also see examples for more.

use std::{collections::HashSet, str::FromStr};

use mime::Mime;
use sophia_api::{
    graph::MutableGraph,
    ns::Namespace,
    parser::TripleParser,
    serializer::{Stringifier, TripleSerializer},
    source::TripleSource,
    term::SimpleTerm,
};

use gdp_rs::proven::TryProven;
use rdf_dynsyn::{
    correspondence::Correspondent, parser::triples::*, serializer::triples::*, syntax::RdfSyntax,
};

pub fn try_main() -> Result<(), Box<dyn std::error::Error>> {
    //  let's say following are input params we got dynamically.
    // source media_type, content of source doc, and target media_type
    let src_doc_media_type = "text/turtle";
    let tgt_doc_media_type = "application/rdf+xml";
    let src_doc_content = r#"
        @prefix : <http://example.org/>.
        @prefix foaf: <http://xmlns.com/foaf/0.1/>.

        :alice foaf:name "Alice";
            foaf:mbox <mailto:alice@work.example> .

        :bob foaf:name "Bob".
    "#;

    // resolve syntaxes for media_types. Or one can use static constants exported by `syntax` module,
    let src_doc_syntax =
        Correspondent::<RdfSyntax>::try_from(&Mime::from_str(src_doc_media_type)?)?.value;
    let tgt_doc_syntax =
        Correspondent::<RdfSyntax>::try_from(&Mime::from_str(tgt_doc_media_type)?)?.value;

    // get parser for source syntax
    let parser_factory = DynSynTripleParserFactory::default();
    let parser = parser_factory.new_parser(src_doc_syntax.try_proven()?, None);

    // parse to a graph
    let mut graph: HashSet<[SimpleTerm; 3]> =
        parser.parse_str(src_doc_content).collect_triples()?;

    let ex = Namespace::new("http://example.org/")?;
    let foaf = Namespace::new("http://xmlns.com/foaf/0.1/")?;

    // mutate graph
    graph.insert_triple([&ex.get("bob")?, &foaf.get("knows")?, &ex.get("alice")?])?;

    // get serializer for target syntax
    let serializer_factory = DynSynTripleSerializerFactory::new(None); // Here we can pass optional formatting options. see documentation.

    let mut serializer = serializer_factory.new_stringifier(tgt_doc_syntax.try_proven()?);
    let serialized_doc = serializer.serialize_graph(&graph)?.as_str();

    println!("The resulting graph\n{}", serialized_doc);

    Ok(())
}
fn main() {
    try_main().unwrap();
}

License: MIT OR Apache-2.0

Dependencies

~4–12MB
~137K SLoC