Skip to main content

Crate rdftk_iri

Crate rdftk_iri 

Source
Expand description

iri This crate provides the Iri, Namespace, Name, and QName types and associated errors.

The primary reference for this crate is the SPARQL specification. The Turtle language as well as the OWL Functional Syntax and Manchester Syntax reference the SPARQL PNAME_NS, PNAME_LN, and PrefixedName productions.

§Specification Summary

From SPARQL §A.8 Grammar:

[68]  	PrefixedName	  ::=  	PNAME_LN | PNAME_NS

[71]  	PNAME_NS	  ::=  	PN_PREFIX? ':'
[72]  	PNAME_LN	  ::=  	PNAME_NS PN_LOCAL
[95]  	PN_CHARS_BASE ::=  	[A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF]
                            | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F]
                            | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD]
                            | [#x10000-#xEFFFF]
[96]  	PN_CHARS_U	  ::=  	PN_CHARS_BASE | '_'

[98]  	PN_CHARS	  ::=  	PN_CHARS_U | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
[99]  	PN_PREFIX	  ::=  	PN_CHARS_BASE ((PN_CHARS|'.')* PN_CHARS)?
[100]  	PN_LOCAL	  ::=  	( PN_CHARS_U | [0-9] ) ((PN_CHARS|'.')* PN_CHARS)?
        /* Note that SPARQL local names allow leading digits while XML local names do not. */

Where the following table provides a mapping from the BNF production rules above to types in this package.

Specification NameCrate TypeComments
IRIRefiri::IriRefAn enum with Iri and PrefixedName variants.
IRI_REFiri::IriA wrapper around the Url type from the url crate.
PrefixedNamepname::PrefixedNameAn enum, either a Namespace or a LocalName.
PNAME_NSpname::NamespaceThe prefix name used to reference the namespace, including ‘:’.
PN_LOCALpname::NameThe name of the locally-defined thing.
PNAME_LNpname::LocalNameThe tuple of Namespace and Name uniquely identifying a thing.

Note that the rules PN_CHARS_BASE, PN_CHARS_U, PN_CHARS, and PN_PREFIX, are only used internally as a part of the parser implementation.

§The Iri Type

The Iri type is a newtype wrapping the url::Url type, and as can be constructed in the same manner. Note that the default format for an IRI is the RDF form where the string value is enclosed in '<' and '>' characters. the from_str and try_from(&str) methods accept both the IRI form and bare strings, the Display implementation uses the IRI form by default but will display bare strings if the alternate flag is set.

§Examples

use rdftk_iri::Iri;
use std::str::FromStr;

let result = Iri::from_str(
    "<https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>",
).unwrap();

It also provides a number of additional methods useful to the IRI as a namespace and namespaced-name identifier.

use rdftk_iri::{Iri, Name};
use std::str::FromStr;

let namespace = Iri::from_str(
    "<https://example.org/ns/things#>",
).unwrap();
assert!(namespace.looks_like_namespace());

let name = namespace.make_name(Name::from_str("ThisThing").unwrap()).unwrap();
assert_eq!(
    name.to_string(),
    "<https://example.org/ns/things#ThisThing>".to_string(),
);

assert_eq!(
    name.namespace(),
    Some(namespace),
);

assert_eq!(
    name.name(),
    Some(Name::from_str("ThisThing").unwrap()),
);

§Curie

Specification: https://www.w3.org/TR/curie/

safe_curie  :=   '[' curie ']'

curie       :=   [ [ prefix ] ':' ] reference

prefix      :=   NCName

reference   :=   irelative-ref (as defined in IRI)

Note that while the empty string matches the production for curie above, an empty string is NOT a valid CURIE. The CURIE prefix ‘’ is reserved for use by languages that support RDF. For this reason, the prefix ‘’ SHOULD be avoided by authors.

§The Namespace Type

A Namespace, PNAME_NS from the SPARQL specification, is a prefix string used to reference a namespace IRI in languages such as OWL, Turtle, SPARQL and so forth.

use rdftk_iri::{LocalName, Name, Namespace};
use std::str::FromStr;

let ns = Namespace::from_str(":").unwrap();
assert!(ns.is_default());

let ns = Namespace::from_str("rdf:").unwrap();
assert!(!ns.is_default());

let name: LocalName = ns.qualify(&Name::from_str("type").unwrap());
assert_eq!("rdf:type".to_string(), name.to_string());

§Name Type

A Name, PN_LOCAL from the SPARQL specification, represents that portion of a LocalName that refers to the locally defined or referenced thing.

use rdftk_iri::{LocalName, Name, Namespace};
use std::str::FromStr;

assert!(Name::from_str("subClassOf").is_ok());

assert!(Name::from_str("rdf:").is_err());

let name = Name::from_str("type").unwrap();
let name: LocalName = name.qualify(&Namespace::from_str("rdf:").unwrap());
assert_eq!("rdf:type".to_string(), name.to_string());

§The LocalName Type

A LocalName, PNAME_LN from the SPARQL specification, represents the tuple of Namespace and Name, such that every name is qualified with the namespace it is defined within.

§Example

use rdftk_iri::{LocalName, Name, Namespace};
use std::str::FromStr;

let parsed_prefixed: LocalName = "prefix:name".parse().expect("parse error");
let constructed_prefixed: LocalName = LocalName::new(
    Namespace::new_unchecked("prefix"),
    Name::new_unchecked("name"),
);
assert_eq!(parsed_prefixed, constructed_prefixed);

let parsed_default: LocalName = ":name".parse().expect("parse error");
let constructed_default: LocalName =
    LocalName::new_in_default(Name::new_unchecked("name"));
assert_eq!(parsed_default, constructed_default);

assert!(LocalName::from_str("").is_err());
assert!(LocalName::from_str("hello world").is_err());

§Feature flags

  • std (enabled by default) — This feature implies the presence of the Rust standard library. If not present the no_std configuration attribute is set.
  • genid (enabled by default) — This feature enables the Iri::genid method, if not required it also removes the dependency on the uuid crate.
  • serde (enabled by default) — This feature provides serde serialization for all types defined in the crate.

Re-exports§

pub use error::Error;
pub use iri::Iri;
pub use iri::IriRef;
pub use map::IriPrefixMap;
pub use pname::LocalName;
pub use pname::Name;
pub use pname::Namespace;
pub use pname::PrefixedName;
pub use vocab::Vocabulary;

Modules§

error
Provides the Error type, and the specific NameParseError for errors in parsing values to Name, Namespace, and LocalName.
iri
This module provides the IriRef enum, and Iri newtype.
map
This module provides the IriPrefixMap type used to maintain mappings between Namespace prefix names and absolute Iri values.
pname
This module provides the types PrefixedName, Namespace, Name, and LocalName.
vocab
This module provides the Vocabulary type and a set of constant Vocabulary values for common RDF vocabularies and OWL ontologies.