Crate curie

source ·
Expand description

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(Some("foaf"), "Agent");

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

Given an IRI is also possible to derive an CURIE.

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(Some("foaf"), "Agent");

assert_eq!(Ok(curie),
           mapper.shrink_iri("http://xmlns.com/foaf/0.1/Agent"));

Structs

  • A prefix and reference, already parsed into separate components.
  • Maps prefixes to base URIs and allows for the expansion of CURIEs (Compact URIs).

Enums