Skip to main content

Id

Struct Id 

Source
pub struct Id { /* private fields */ }
Expand description

Identifier.

Identifiers are structured string-based representations which are used to uniquely identify artifacts as they move through streams and stores. They use a compact, yet human-readable format that is easy to generate and parse, and consists of the following six components:

  • provider, e.g., file or git.
  • resource, e.g., volume, branch or tag.
  • variant, e.g., language, version or format.
  • context, e.g., source or output directory.
  • location, e.g., file or folder.
  • fragment, e.g., line number or anchor.

Identifiers implement Eq, PartialEq and Hash, as well as Ord and PartialOrd, so they can be stored in ordered and unordered storages, as well as efficiently compared with each other. The structured string-based representation is defined as follows:

zri:<provider>:<resource>:<variant>:<context>:<location>:<fragment>

This ensures blazing fast cloning and editing. Additionally, identifiers are guaranteed to not contain backslashes or path traversals in components. An empty component, for those that are allowed to remain empty, is equal to the default in the context set by the given provider.

§Examples

Create an identifier:

use zrx_id::Id;

// Create identifier builder
let builder = Id::builder()
    .provider("file")
    .context("docs")
    .location("index.md");

// Create identifier from builder
let id = builder.build()?;
assert_eq!(id.as_str(), "zri:file:::docs:index.md:");

Create an identifier from a string:

use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;

Implementations§

Source§

impl Id

Source

pub fn builder<'a>() -> Builder<'a>

Creates an identifier builder.

§Examples
use zrx_id::Id;

// Create identifier builder
let builder = Id::builder();
Source

pub fn to_builder(&self) -> Builder<'_>

Creates a builder from the identifier.

This method creates a builder from the current identifier, which allows to modify components and build a new identifier from an existing one.

§Examples
use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;

// Create identifier builder
let builder = id.to_builder().location("README.md");

// Create identifier from builder
let id = builder.build()?;
assert_eq!(id.as_str(), "zri:file:::docs:README.md:");
Source§

impl Id

Source

pub fn to_path(&self) -> PathBuf

Converts the identifier to a relative file system path.

This method creates a relative PathBuf from both, the context and location components of the identifier, using platform-dependent path separators. The resulting path is always relative, and never absolute, since both, context and location, are always relative.

In order to resolve the path, the Id::resource needs to be taken into account, which is of course provider-specific. Note that for use of paths in URLs, Id::as_uri must be used, which guarantees that all path separators are forward slashes.

§Examples
use std::path::Path;
use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;

// Create path from identifier
let path = id.to_path();
assert_eq!(path, Path::new("docs/index.md"));
Source

pub fn as_str(&self) -> &str

Returns the string representation.

§Examples
use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;

// Obtain string representation
assert_eq!(id.as_str(), "zri:file:::docs:index.md:");
Source

pub fn as_uri(&self) -> Uri<'_>

Returns the URI representation.

This method creates a URI from Id::location, which is necessary for using the identifier in URLs, e.g., to construct relative links.

§Examples
use zrx_id::uri::Uri;
use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;

// Obtain URI representation
assert_eq!(id.as_uri(), Uri::from("index.md"));
Source§

impl Id

Source

pub fn provider(&self) -> Cow<'_, str>

Returns the provider component.

Source

pub fn resource(&self) -> Option<Cow<'_, str>>

Returns the resource component, if any.

Source

pub fn variant(&self) -> Option<Cow<'_, str>>

Returns the variant component, if any.

Source

pub fn context(&self) -> Cow<'_, str>

Returns the context component.

Source

pub fn location(&self) -> Cow<'_, str>

Returns the location component.

Source

pub fn fragment(&self) -> Option<Cow<'_, str>>

Returns the fragment component, if any.

Trait Implementations§

Source§

impl AsRef<Format<7>> for Id

Source§

fn as_ref(&self) -> &Format<7>

Returns the formatted string.

Note that it’s normally not necessary to access the formatted string directly, as all components can be accessed via the respective methods. We need to access the underlying formatted string in our internal APIs, e.g., to compute the Specificity for the given Id.

Source§

impl Clone for Id

Source§

fn clone(&self) -> Id

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Id

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the identifier for debugging.

Source§

impl Display for Id

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the identifier for display.

Source§

impl From<Id> for Term

Source§

fn from(id: Id) -> Self

Creates a term from an identifier.

Source§

impl FromStr for Id

Source§

fn from_str(value: &str) -> Result<Self>

Attempts to create an identifier from a string.

The string must adhere to the following format and include exactly six : separators, even in case some components are omitted. The optional components are resource, variant and fragment, and can be left empty, which is represented as empty strings internally.

zri:<provider>:<resource>:<variant>:<context>:<location>:<fragment>
§Errors

Returns Error::Component if any of the provider, context or location components are not set, and Error::Prefix if the prefix isn’t zri. On low-level format errors, Error::Format is returned.

§Examples
use zrx_id::Id;

// Create identifier from string
let id: Id = "zri:file:::docs:index.md:".parse()?;
Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Hash for Id

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Hashes the identifier.

Since identifiers are immutable, we can use a precomputed hash for fast hashing. This is especially useful when identifiers are used as keys in hash maps or hash sets, where hashing is a frequent operation, as the performance gains are significant with constant time.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Id

Source§

fn cmp(&self, other: &Self) -> Ordering

Orders two identifiers.

§Examples
use zrx_id::Id;

// Create and compare identifiers
let a: Id = "zri:file:::docs:index.md:".parse()?;
let b: Id = "zri:file:::docs:about.md:".parse()?;
assert!(a > b);
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Id

Source§

fn eq(&self, other: &Self) -> bool

Compares two identifiers for equality.

§Examples
use zrx_id::Id;

// Create and compare identifiers
let a: Id = "zri:file:::docs:index.md:".parse()?;
let b: Id = "zri:file:::docs:index.md:".parse()?;
assert_eq!(a, b);
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Id

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Orders two identifiers.

§Examples
use zrx_id::Id;

// Create and compare identifiers
let a: Id = "zri:file:::docs:index.md:".parse()?;
let b: Id = "zri:file:::docs:about.md:".parse()?;
assert!(a > b);
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToSpecificity for Id

Source§

fn to_specificity(&self) -> Specificity

Computes the specificity of the identifier.

§Examples
use zrx_id::id;
use zrx_id::specificity::ToSpecificity;

// Create identifier and compute specificity
let id = id!(provider = "file", context = ".", location = "index.md")?;
assert_eq!(id.to_specificity(), (3, 0, 0, 13).into());
Source§

impl TryFrom<Id> for Selector

Source§

fn try_from(id: Id) -> Result<Self>

Attempts to create a selector from an identifier.

An Id can be converted into a Selector because all identifiers are also valid selectors, as they represent exact matches. However, the reverse is not true, as selectors can contain wildcards, as well as optional components, which identifiers cannot.

§Examples
use zrx_id::{Id, Selector};

// Create selector from identifier
let id: Id = "zri:file:::docs:index.md:".parse()?;
let selector: Selector = id.try_into()?;
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

impl TryToId for Id

Source§

fn try_to_id(&self) -> Result<Cow<'_, Id>>

Attempts to convert to an identifier.

Source§

impl TryToSelector for Id

Source§

fn try_to_selector(&self) -> Result<Cow<'_, Selector>>

Attempts to convert to a selector.

Since all identifiers are also valid selectors, implementing this trait ensures we can also pass identifier references to Builder::add.

§Examples
use zrx_id::{Id, Selector, TryToSelector};

// Create selector from identifier
let id: Id = "zri:file:::docs:index.md:".parse()?;
let selector = (&id).try_to_selector()?;
Source§

impl Eq for Id

Source§

impl Value for Id

Auto Trait Implementations§

§

impl Freeze for Id

§

impl RefUnwindSafe for Id

§

impl Send for Id

§

impl Sync for Id

§

impl Unpin for Id

§

impl UnsafeUnpin for Id

§

impl UnwindSafe for Id

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T
where T: Value,

Source§

fn into_result(self) -> Result<T, Error>

Converts any value into a step result.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<K, V> TryAsStorage<K> for V
where K: Key, V: Value,

Source§

fn try_as_storage( item: &(dyn Any + 'static), ) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>

Attempts to convert into a storage reference.

§Errors

The following errors might be returned:

§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorage;
use zrx_storage::Storage;

// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);

// Obtain type-erased reference
let item: &dyn Any = &storage;

// Obtain storage reference
let storage = <i32>::try_as_storage(item)?;
Source§

type Target<'a> = &'a Storage<K, V>

Target type of conversion.
Source§

impl<K, V> TryAsStorageMut<K> for V
where K: Key, V: Value,

Source§

fn try_as_storage_mut( item: &mut (dyn Any + 'static), ) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>

Attempts to convert into a mutable storage reference.

§Errors

The following errors might be returned:

§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorageMut;
use zrx_storage::Storage;

// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);

// Obtain mutable type-erased reference
let item: &mut dyn Any = &mut storage;

// Obtain mutable storage reference
let storage = <i32>::try_as_storage_mut(item)?;
Source§

type Target<'a> = &'a mut Storage<K, V>

Target type of conversion.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Id for T
where T: Clone + Debug + Display + Eq + Hash + Ord + Send + Sync + 'static,

Source§

impl<T> Key for T
where T: Clone + Debug + Eq + Hash + Ord + 'static,

Source§

impl<T> Value for T
where T: Debug + Eq + 'static,