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
impl Id
Sourcepub fn builder<'a>() -> Builder<'a>
pub fn builder<'a>() -> Builder<'a>
Creates an identifier builder.
§Examples
use zrx_id::Id;
// Create identifier builder
let builder = Id::builder();Sourcepub fn to_builder(&self) -> Builder<'_>
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
impl Id
Sourcepub fn to_path(&self) -> PathBuf
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"));Sourcepub fn as_str(&self) -> &str
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:");Sourcepub fn as_uri(&self) -> Uri<'_>
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"));Trait Implementations§
Source§impl AsRef<Format<7>> for Id
impl AsRef<Format<7>> for Id
Source§fn as_ref(&self) -> &Format<7>
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 FromStr for Id
impl FromStr for Id
Source§fn from_str(value: &str) -> Result<Self>
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§impl Hash for Id
impl Hash for Id
Source§fn hash<H>(&self, state: &mut H)where
H: Hasher,
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.
Source§impl Ord for Id
impl Ord for Id
Source§fn cmp(&self, other: &Self) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Id
impl PartialEq for Id
Source§impl PartialOrd for Id
impl PartialOrd for Id
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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);Source§impl ToSpecificity for Id
impl ToSpecificity for Id
Source§fn to_specificity(&self) -> Specificity
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
impl TryFrom<Id> for Selector
Source§fn try_from(id: Id) -> Result<Self>
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§impl TryToSelector for Id
impl TryToSelector for Id
Source§fn try_to_selector(&self) -> Result<Cow<'_, Selector>>
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()?;impl Eq for Id
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoResult<T> for Twhere
T: Value,
impl<T> IntoResult<T> for Twhere
T: Value,
Source§fn into_result(self) -> Result<T, Error>
fn into_result(self) -> Result<T, Error>
Converts any value into a step result.
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<K, V> TryAsStorage<K> for V
impl<K, V> TryAsStorage<K> for V
Source§fn try_as_storage(
item: &(dyn Any + 'static),
) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
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:
Error::Downcast: Item cannot be downcast.
§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§impl<K, V> TryAsStorageMut<K> for V
impl<K, V> TryAsStorageMut<K> for V
Source§fn try_as_storage_mut(
item: &mut (dyn Any + 'static),
) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
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:
Error::Downcast: Item cannot be downcast.
§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)?;