2 releases

Uses old Rust 2015

0.1.1 Apr 7, 2017
0.1.0 Apr 7, 2017

#5 in #n-triples

Download history 348/week @ 2023-12-18 76/week @ 2023-12-25 1/week @ 2024-01-22 22/week @ 2024-02-12 28/week @ 2024-02-19 65/week @ 2024-02-26 8/week @ 2024-03-04 26/week @ 2024-03-11 24/week @ 2024-03-18 25/week @ 2024-03-25 57/week @ 2024-04-01

134 downloads per month
Used in 2 crates (via hdt)

AFL-3.0 license

12KB
190 lines

ntriple

Build Status

A parser that parses a single RDF statement, in RDF N-Triples format.

It is written in Rust and uses rust-peg for parsing the triple to an object structure. Literals are decomposed into their RDF lexical form and language or data type.

The purpose of this library is to process triples in a streaming way, so it doesn't build an RDF Graph. If you are looking for libraries that do, take a look at rust-ntriples or rome.

Example:


extern crate ntriple;

use ntriple::parser::triple_line;
use ntriple::{Subject, Predicate, Object};

fn main() {
  
  // Here's some input in n-triples format. Unicode escape sequences are resolved
  // so \u30AA becomes オ.
  let input = "_:subject <http://example.org/predicate> \"\\u30AAオブジェクト\".";
  
  // parse the input:
  let parse_result = triple_line(&input);
  
  // The result contains an option, or an error when parsing the input failed.
  match parse_result {
  
    // Ok if the input is a triple, a comment, an empty string or whitespace(s).
    Ok(triple_option) => {
      match triple_option {
        Some(triple) => { // a valid triple is found.
          match triple.subject {
            // In this case we expect a blank node label
            Subject::BNode(subject) => println!("Subject: blank node: {}", subject),
            _ => println!("Weird, a blank node is expected here.")
          };
          match triple.predicate {
            Predicate::IriRef(iri) => println!("Predicate: {}", iri)
          };
          match triple.object {
            Object::Lit(literal) => println!("Object: literal: {} ", literal.data),
            _ => println!("Weird, a literal is expected here.")
          }
        },
        None => { println!("Skipped [{}]", input); }
      };
    },
    // a parse error: the input is no valid triple, comment, empty string or whitespace(s)
    Err(error) => println!("{}\n{}", input, error),
  };
}

No runtime deps

~12KB