urn-rs
Rust crate for parsing, building, comparing, and percent-encoding RFC 8141 / RFC 2141 URNs.
Fork of urn by chayleaf. Nearly all of the design and implementation is his work. This fork adds pluggable namespaces, performance work, Ord impls, benches, and Rust 2024 edition uplift. See Attribution below.
Highlights vs upstream
- Pluggable namespaces (
src/namespace.rs):UrnNamespacetrait for typed NSS parsing/building. Built-in impls behind features:ngsi-ld—urn:ngsi-ld:<Type>:<Id>, addsas_ngsi_ld,ngsi_ld_type,ngsi_ld_id,Urn::try_from_ngsi_ld.uuid— canonicalurn:uuid:<8-4-4-4-12>validation, borrowed string form (as_uuid_str,try_from_uuid_str).uuid-typed— same + typed::uuid::Uuidround-trip (as_uuid,try_from_uuid). Pulls in theuuidcrate.
- Performance: SWAR fast path for plain pchar runs, hex lookup tables for percent decode/encode, reduced allocations in builder / accessors / setters / serde paths. Criterion bench suite under
benches/(parse,percent,builder_accessors,setters,serde). Ord/PartialOrdonUrnandUrnSlice(lexicographic on the canonical form).- Rust 2024 edition, MSRV 1.85.
- Crate renamed to
urn-rs(library imported asuse urn_rs::…).
Everything else, RFC parsing semantics, equivalence rules, percent-encoding behavior, no_std/alloc story, Serde support, matches upstream.
Parsing & equivalence
Parsing and equality follow the spec: only the significant portion of the URN is compared (NID is ASCII-case-insensitive; NSS percent-encoding is normalized for comparison; r-/q-/f-components do not affect equality). Per-namespace lexical equivalence rules defined by individual RFCs are not applied.
Features
| Feature | Default | Effect |
|---|---|---|
std |
yes | enables alloc, adds std::error::Error impls |
alloc |
owned Urn, builder, String-returning APIs |
|
serde |
Serialize / Deserialize for Urn and UrnSlice |
|
ngsi-ld |
NGSI-LD namespace helpers | |
uuid |
UUID NSS validation (string form) | |
uuid-typed |
uuid + typed ::uuid::Uuid round-trip |
no_std build: disable default features. With neither std nor alloc you get UrnSlice<'a> (borrowed, zero-alloc). Add alloc back for owned Urn and the builder.
Types
UrnSlice<'a>— borrowed URN, available withoutalloc.Urn— owned URN (requiresalloc).UrnBuilder— validating builder (requiresalloc).UrnNamespace— trait for structured NSS types.
Examples
Parse and inspect:
use Urn;
let u: Urn = "urn:example:foo?=bar#frag".parse?;
assert_eq!;
assert_eq!;
Build:
use UrnBuilder;
let u = new.build?;
assert_eq!;
NGSI-LD (feature ngsi-ld):
use Urn;
let u = try_from_ngsi_ld?;
assert_eq!;
let p = u.as_ngsi_ld.unwrap;
assert_eq!;
UUID typed (feature uuid-typed):
use Urn;
let raw: Uuid = "f47ac10b-58cc-4372-a567-0e02b2c3d479".parse?;
let u = try_from_uuid?;
assert_eq!;
Custom namespace:
use ;
;
let u = try_from?;
assert_eq!;
Benches
Criterion output lands in target/criterion/.
MSRV
Rust 1.85 (edition 2024). Bumping MSRV is a minor-version bump.
Attribution
Original crate: urn by chayleaf. All upstream commits are preserved in this repo's history under their original authorship.
License
Triple-licensed BSD0 / MIT / Apache-2.0, same as upstream. At your choice: