curie 0.0.4

CURIE or Compact URIs as used in JSON-LD, RDF, SPARQL, XML and other applications. This manages mapping prefixes to URIs or IRIs as well as expanding CURIEs to the complete URI form.

Crate curie [] [src]

CURIEs: Compact URIs

CURIEs, defined by the W3C, are a compact way of representing a URI. A CURIE consists of an optional prefix and a reference, separated by a colon.

They are commonly used in JSON-LD, RDF, SPARQL, XML namespaces and other applications.

Example CURIEs:

  • "foaf:Person" -- Results in a URI in the namespace represented by the "foaf" prefix.
  • ":Person" -- Results in a URI in the namespace represented by the "" prefix.
  • "Person" -- Results in a URI in the default namespace.

The last example relies upon there being a default mapping providing a default base URI, while the example before it relies upon there being a prefix which is an empty string.

See the specification for further details.

Compact URIs in the Real World

In SPARQL (from Wikipedia):

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
       ?email
WHERE
  {
    ?person  a          foaf:Person .
    ?person  foaf:name  ?name .
    ?person  foaf:mbox  ?email .
  }

In the Turtle serialization for RDF (from the specification):

@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<#green-goblin>
    rel:enemyOf <#spiderman> ;
    a foaf:Person ;    # in the context of the Marvel universe
    foaf:name "Green Goblin" .

<#spiderman>
    rel:enemyOf <#green-goblin> ;
    a foaf:Person ;
    foaf:name "Spiderman", "Человек-паук"@ru .

Usage

use curie::PrefixMapping;

// Initialize a prefix mapper.
let mut mapper = PrefixMapping::default();
mapper.add_prefix("foaf", "http://xmlns.com/foaf/0.1/").unwrap();

// Set a default prefix
mapper.set_default("http://example.com/");

// Expand a CURIE and get back the full URI.
assert_eq!(mapper.expand_curie_string("Entity"),
           Ok(String::from("http://example.com/Entity")));
assert_eq!(mapper.expand_curie_string("foaf:Agent"),
           Ok(String::from("http://xmlns.com/foaf/0.1/Agent")));

When parsing a file, it is likely that the distinction between the prefix and the reference portions of the CURIE will be clear, so to save time during expansion, the Curie struct can also be used:

use curie::{Curie, PrefixMapping};

// Initialize a prefix mapper.
let mut mapper = PrefixMapping::default();
mapper.add_prefix("foaf", "http://xmlns.com/foaf/0.1/").unwrap();

let curie = Curie::new("foaf", "Agent");

assert_eq!(mapper.expand_curie(&curie),
           Ok(String::from("http://xmlns.com/foaf/0.1/Agent")));

Structs

Curie

A prefix and reference, already parsed into separate components.

PrefixMapping

Maps prefixes to base URIs and allows for the expansion of CURIEs (Compact URIs).

Enums

ExpansionError

Errors that might occur during CURIE expansion.

InvalidPrefixError

Errors that might occur when adding a prefix to a PrefixMapping.