Skip to main content

zrx_id/id/
convert.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Identifier conversions.
27
28use std::borrow::Cow;
29
30use super::error::Result;
31use super::Id;
32
33// ----------------------------------------------------------------------------
34// Traits
35// ----------------------------------------------------------------------------
36
37/// Attempt conversion to [`Id`].
38///
39/// This trait allows to convert an arbitrary value to an identifier, using a
40/// [`Cow`] smart pointer to avoid unnecessary cloning, e.g. for references.
41pub trait TryToId {
42    /// Attempts to convert to an identifier.
43    ///
44    /// # Errors
45    ///
46    /// In case conversion fails, an error should be returned.
47    fn try_to_id(&self) -> Result<Cow<'_, Id>>;
48}
49
50// ----------------------------------------------------------------------------
51// Trait implementations
52// ----------------------------------------------------------------------------
53
54impl TryToId for Id {
55    /// Attempts to convert to an identifier.
56    #[inline]
57    fn try_to_id(&self) -> Result<Cow<'_, Id>> {
58        Ok(Cow::Borrowed(self))
59    }
60}
61
62// ----------------------------------------------------------------------------
63// Blanket implementations
64// ----------------------------------------------------------------------------
65
66impl<T> TryToId for T
67where
68    T: AsRef<str>,
69{
70    /// Attempts to convert to an identifier.
71    ///
72    /// # Errors
73    ///
74    /// Returns [`Error::Component`] if any of the `provider`, `context` or
75    /// `location` components are not set, and [`Error::Prefix`] if the prefix
76    /// isn't `zri`. On low-level format errors, [`Error::Format`] is returned.
77    ///
78    /// [`Error::Component`]: crate::id::Error::Component
79    /// [`Error::Format`]: crate::id::Error::Format
80    /// [`Error::Prefix`]: crate::id::Error::Prefix
81    ///
82    /// # Examples
83    ///
84    /// ```
85    /// # use std::error::Error;
86    /// # fn main() -> Result<(), Box<dyn Error>> {
87    /// use zrx_id::{Id, TryToId};
88    ///
89    /// // Create identifier from string
90    /// let id = "zri:file:::docs:index.md:".try_to_id()?;
91    /// # Ok(())
92    /// # }
93    /// ```
94    #[inline]
95    fn try_to_id(&self) -> Result<Cow<'_, Id>> {
96        self.as_ref().parse().map(Cow::Owned)
97    }
98}