nstree/path/
into_conversions.rs

1//! Internal module, creating traits analogous to the [`IntoIterator`] trait, but for
2//! [`NamespacePath`] and [`RawNamespacePath`].
3
4use super::{IntoIntoRawNSPath, NamespacePath, RawNamespacePath};
5
6/// Anything that can be converted-into a [`RawNamespacePath`]. Automatically implemented for
7/// anything implementing [`RawNamespacePath`]. Analogous to [`IntoIterator`].
8///
9/// ## Interaction with [`IntoNamespacePath`]
10/// Anything implementing [`IntoNamespacePath`] - including all [`NamespacePath`]s - can be
11/// converted into an [`IntoRawNamespacePath`]. This is via [`IntoNamespacePath::into_into_raw`].
12///
13/// In most cases, this will use a [default implementor][iirnsp] that wraps the associated
14/// [`IntoNamespacePath::NSPath`] type with [`RawNamespacePathWrapper`][rnspw].
15///
16/// [rnspw]: `crate::path::RawNamespacePathWrapper`
17/// [iirnsp]: `IntoIntoRawNSPath`
18pub trait IntoRawNamespacePath {
19    type RawNSPath: RawNamespacePath;
20
21    /// Convert this [`IntoRawNamespacePath`] into a [`RawNamespacePath`]
22    fn into_raw_namespace_path(self) -> Self::RawNSPath;
23}
24
25impl<T: RawNamespacePath> IntoRawNamespacePath for T {
26    type RawNSPath = Self;
27
28    #[inline]
29    fn into_raw_namespace_path(self) -> Self::RawNSPath {
30        self
31    }
32}
33
34/// Anything that can be converted-into a [`NamespacePath`]/path/segment. Analogous to
35/// [`IntoIterator`]
36///
37/// This is automatically implemented for anything implementing [`NamespacePath`].
38///
39/// [irnsp]: `IntoRawNamespacePath`
40pub trait IntoNamespacePath {
41    type NSPath: NamespacePath;
42
43    /// Obtain the namespace path from this structure.
44    fn into_namespace_path(self) -> Self::NSPath;
45
46    /// Convert this into something convertible into a [`RawNamespacePath`].
47    ///
48    /// By default, the produced value will reuse [`IntoNamespacePath::NSPath`] as the
49    /// implementation for [`RawNamespacePath`], by wrapping it in
50    /// [`RawNamespacePathWrapper`][rnspw].
51    ///
52    /// This is almost certainly what you want.
53    ///
54    /// [rnspw]: `crate::path::RawNamespacePathWrapper`
55    #[inline(always)]
56    fn into_into_raw(self) -> impl IntoRawNamespacePath
57    where
58        Self: Sized,
59    {
60        IntoIntoRawNSPath(self)
61    }
62}
63
64impl<T: NamespacePath> IntoNamespacePath for T {
65    type NSPath = Self;
66
67    #[inline]
68    fn into_namespace_path(self) -> Self::NSPath {
69        self
70    }
71}
72
73// nstree - nested namespace string-generating abstraction library
74// Copyright (C) 2025  Matti <infomorphic-matti at protonmail dot com>
75//
76// This program is free software: you can redistribute it and/or modify
77// it under the terms of the GNU General Public License as published by
78// the Free Software Foundation, either version 3 of the License, or
79// (at your option) any later version.
80//
81// This program is distributed in the hope that it will be useful,
82// but WITHOUT ANY WARRANTY; without even the implied warranty of
83// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
84// GNU General Public License for more details.
85//
86// You should have received a copy of the GNU General Public License
87// along with this program.  If not, see <https://www.gnu.org/licenses/>.
88// ------
89// SPDX-License-Identifier: GPL-3.0-or-later