zrx_id/id/selector/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//! Selector conversions.
27
28use std::borrow::Cow;
29
30use crate::id::{Id, Result};
31
32use super::Selector;
33
34// ----------------------------------------------------------------------------
35// Traits
36// ----------------------------------------------------------------------------
37
38/// Attempt conversion to [`Selector`].
39///
40/// This trait allows to convert an arbitrary value to a selector, using a
41/// [`Cow`] smart pointer to avoid unnecessary cloning, e.g. for references.
42pub trait TryToSelector {
43 /// Attempts to convert to a selector.
44 ///
45 /// # Errors
46 ///
47 /// In case conversion fails, an error should be returned.
48 fn try_to_selector(&self) -> Result<Cow<'_, Selector>>;
49}
50
51// ----------------------------------------------------------------------------
52// Trait implementations
53// ----------------------------------------------------------------------------
54
55impl TryToSelector for Selector {
56 /// Attempts to convert to a selector.
57 #[inline]
58 fn try_to_selector(&self) -> Result<Cow<'_, Selector>> {
59 Ok(Cow::Borrowed(self))
60 }
61}
62
63impl TryToSelector for Id {
64 /// Attempts to convert to a selector.
65 ///
66 /// Since all identifiers are also valid selectors, implementing this trait
67 /// ensures we can also pass identifier references to [`Builder::add`][].
68 ///
69 /// [`Builder::add`]: crate::id::matcher::Builder::add
70 ///
71 /// # Examples
72 ///
73 /// ```
74 /// # use std::error::Error;
75 /// # fn main() -> Result<(), Box<dyn Error>> {
76 /// use zrx_id::{Id, Selector, TryToSelector};
77 ///
78 /// // Create selector from identifier
79 /// let id: Id = "zri:file:::docs:index.md:".parse()?;
80 /// let selector = (&id).try_to_selector()?;
81 /// # Ok(())
82 /// # }
83 /// ```
84 #[inline]
85 fn try_to_selector(&self) -> Result<Cow<'_, Selector>> {
86 self.to_owned().try_into().map(Cow::Owned)
87 }
88}
89
90// ----------------------------------------------------------------------------
91// Blanket implementations
92// ----------------------------------------------------------------------------
93
94impl<T> TryToSelector for T
95where
96 T: AsRef<str>,
97{
98 /// Attempts to convert to a selector.
99 ///
100 /// # Errors
101 ///
102 /// Returns [`Error::Prefix`][] if the prefix isn't `zrs`. Low-level format
103 /// errors are returned as part of [`Error::Format`][].
104 ///
105 /// [`Error::Format`]: crate::id::Error::Format
106 /// [`Error::Prefix`]: crate::id::Error::Prefix
107 ///
108 /// # Examples
109 ///
110 /// ```
111 /// # use std::error::Error;
112 /// # fn main() -> Result<(), Box<dyn Error>> {
113 /// use zrx_id::{Selector, TryToSelector};
114 ///
115 /// // Create selector from string
116 /// let selector = "zrs:::::**/*.md:".try_to_selector()?;
117 /// # Ok(())
118 /// # }
119 /// ```
120 #[inline]
121 fn try_to_selector(&self) -> Result<Cow<'_, Selector>> {
122 self.as_ref().parse().map(Cow::Owned)
123 }
124}