Skip to main content

Crate n_quads

Crate n_quads 

Source
Expand description

N-Quads parsing.

N-Quads is a line-based, plain text format for encoding an RDF dataset. This library provides a W3C Recommendation compliant parser built on rdfx terms. Parsing also returns a CodeMap recording the source span of every quad and of each of its components, so a downstream validation error can be reported against the exact byte range that caused it.

Both the standard grammar and the generalized (gRDF) grammar are supported. gRDF lifts the restrictions on which term kinds may appear where: any term may be a subject, predicate, object or graph label.

§RDF version

The grammar implemented here is the RDF 1.1 one. rdfx models RDF 1.2, so its term types can represent triple terms and directional language strings, but this parser never produces them — the three RDF 1.2 syntax additions are rejected:

SyntaxExampleStatus
Triple terms<<( :a :b :c )>>rejected
Directional language strings"x"@en--ltrrejected
Reifiers:s :p "x" ~ :rrejected

§Usage

let source = r#"<http://example.com/#a> <http://example.com/#b> "value"@en ."#;

let (document, code_map) = n_quads::document_from_str(source)?;
assert_eq!(document.len(), 1);

// Span of the whole quad, then of its subject, predicate and object.
let (quad_span, components) = code_map[0];
assert_eq!(&source[quad_span.range()], source);
assert_eq!(&source[components.0.range()], "<http://example.com/#a>");
assert_eq!(&source[components.2.range()], r#""value"@en"#);

For the generalized grammar, use grdf_document_from_str. For input that is not already a &str — a fallible byte stream, UTF-16, a custom decoder — use the Parse trait directly.

Re-exports§

pub use lexing::LexingError;

Modules§

lexing
N-Quads lexing.

Structs§

BlankIdBuf
Owned blank node identifier.
Parser
N-Quads parser.

Enums§

Error
Parse error.

Traits§

Parse
N-Quads parsing methods.

Functions§

document_from_str
Parses an N-Quads document from a string.
grdf_document_from_str
Parses a generalized (gRDF) N-Quads document from a string.

Type Aliases§

CodeMap
Source spans of every quad in a document, in source order.
Document
N-Quads document.
GrdfDocument
gRDF N-Quads document.
QuadSpans
Source spans of a single quad.