iri-rs-enum 3.3.4

Derive macro that maps vocabulary IRIs onto enum variants, with compile-time prefix resolution and const-friendly output.
Documentation

iri-rs-enum

Crate Docs MSRV License

#[derive(IriEnum)] for IRI-valued enum types, part of the iri-rs workspace. Fork of iref-enum by Timothée Haudebourg — reworked to emit const-friendly output via iri-rs-core's from_raw_parts.

Storing and comparing full IRIs is expensive when you only ever use a known vocabulary. IriEnum turns a plain Rust enum into a two-way map between an Iri and its variant — TryFrom<&Iri<T>> in, From<&Variant> to Iri<&'static str> out.

use iri_rs_core::Iri;
use iri_rs_enum::IriEnum;
use iri_rs_static::iri;

#[derive(IriEnum, PartialEq, Debug)]
#[iri_prefix("schema" = "https://schema.org/")]
pub enum Vocab {
    #[iri("schema:name")]  Name,
    #[iri("schema:knows")] Knows,
}

let term = Vocab::try_from(&iri!("https://schema.org/name")).unwrap();
assert_eq!(term, Vocab::Name);

let back: Iri<&'static str> = (&Vocab::Knows).into();
assert_eq!(back.as_str(), "https://schema.org/knows");

Prefixes resolve at macro expansion. Each variant's IRI is validated once against the real parser in iri-rs-core and emitted as a const from pre-computed component positions — no runtime parsing, no allocation.


Install

Enable the enum feature on the facade crate (recommended):

cargo add iri-rs --features enum

or depend on this crate directly:

cargo add iri-rs-core iri-rs-enum iri-rs-static

The generated code references ::iri_rs_core::*, so downstream crates must have iri-rs-core as a direct dependency.

Attributes

Attribute Where Meaning
#[iri_prefix("ns" = "https://...")] Enum Declare a prefix usable in #[iri(...)]
#[iri("ns:term")] Variant IRI for this variant (prefixed or absolute)

Multiple #[iri_prefix] attributes may be given on the same enum.

Fallback variants

A variant with a single unnamed field acts as a catch-all. The inner type must implement TryFrom<&Iri<T>> and From<&Inner> for Iri<&'static str> — both are generated by this derive on nested enums, so fallbacks compose:

#[derive(IriEnum, PartialEq, Debug)]
#[iri_prefix("schema" = "https://schema.org/")]
pub enum Person {
    #[iri("schema:Person")] Base,
    Other(OtherVocab),   // fallback: try OtherVocab first
}

Workspace

Part of the iri-rs workspace. See the root README for the overall picture, motivation, and fork attribution.

MSRV

Rust 1.85 (edition 2024).

License

Dual-licensed: