#triple #sparql #semantic-web #graph #resources #description #framework

rdf

rdf is a library for the Resource Description Framework (RDF) and SPARQL implemented in Rust

5 releases

Uses old Rust 2015

0.1.4 Jul 26, 2018
0.1.3 May 28, 2018
0.1.2 May 11, 2018
0.1.1 Mar 20, 2018
0.1.0 Mar 20, 2018

#1558 in Web programming

Download history 12/week @ 2024-07-20 29/week @ 2024-07-27 21/week @ 2024-08-03 20/week @ 2024-08-10 9/week @ 2024-08-17 12/week @ 2024-08-24 85/week @ 2024-08-31 43/week @ 2024-09-07 11/week @ 2024-09-14 35/week @ 2024-09-21 28/week @ 2024-09-28 3/week @ 2024-10-05 9/week @ 2024-10-12 15/week @ 2024-10-19 5/week @ 2024-10-26 34/week @ 2024-11-02

63 downloads per month
Used in 4 crates

MIT license

160KB
3K SLoC

rdf-rs

Note: This project is work in progress and currently not stable.

rdf is a library for the Resource Description Framework (RDF) and SPARQL implemented in Rust.

This project is a way for me to learn Rust and combine it with my interests in semantic web technologies.

Basic Examples

RDF triples can be stored and represented in a graph.

use rdf::graph::Graph;
use rdf::uri::Uri;
use rdf::triple::Triple;

let mut graph = Graph::new(None);
let subject = graph.create_blank_node();
let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
let object = graph.create_blank_node();
let triple = Triple::new(&subject, &predicate, &object);

graph.add_triple(&triple);

RDF graphs can be serialized to a supported format.

use rdf::writer::n_triples_writer::NTriplesWriter;
use rdf::writer::rdf_writer::RdfWriter;
use rdf::graph::Graph;
use rdf::uri::Uri;
use rdf::triple::Triple;

let writer = NTriplesWriter::new();

let mut graph = Graph::new(None);
let subject = graph.create_blank_node();
let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
let object = graph.create_blank_node();
let triple = Triple::new(&subject, &predicate, &object);

graph.add_triple(&triple);
assert_eq!(writer.write_to_string(&graph).unwrap(),
           "_:auto0 <http://example.org/show/localName> _:auto1 .\n".to_string());

RDF syntax can also be parsed and transformed into an RDF graph.

use rdf::reader::turtle_parser::TurtleParser;
use rdf::reader::rdf_parser::RdfParser;
use rdf::uri::Uri;

let input = "@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://www.w3.org/2001/sw/RDFCore/ntriples/> rdf:type foaf:Document ;
        <http://purl.org/dc/terms/title> \"N-Triples\"@en-US ;
        foaf:maker _:art .";

let mut reader = TurtleParser::from_string(input.to_string());
match reader.decode() {
  Ok(graph) => {
    assert_eq!(graph.count(), 3);
    assert_eq!(graph.namespaces().len(), 2);
    assert_eq!(graph.base_uri(), &Some(Uri::new("http://example.org/".to_string())))
  },
  Err(_) => assert!(false)
}

Current State

Currently rdf-rs provides basic data structures for representing RDF graphs, triples and nodes. The following formats can be parsed and serialized:

  • Turtle
  • N-Triples

Future Work and Ideas

  • Support querying with SPARQL
  • Add support for more formats
  • More comprehensive Uri data structure

No runtime deps