nstree 1.0.0

construct branched 'namespace strings' for nested subcomponents, often for logging
Documentation
//! Internal module, creating traits analogous to the [`IntoIterator`] trait, but for
//! [`NamespacePath`] and [`RawNamespacePath`].

use super::{IntoIntoRawNSPath, NamespacePath, RawNamespacePath};

/// Anything that can be converted-into a [`RawNamespacePath`]. Automatically implemented for
/// anything implementing [`RawNamespacePath`]. Analogous to [`IntoIterator`].
///
/// ## Interaction with [`IntoNamespacePath`]
/// Anything implementing [`IntoNamespacePath`] - including all [`NamespacePath`]s - can be
/// converted into an [`IntoRawNamespacePath`]. This is via [`IntoNamespacePath::into_into_raw`].
///
/// In most cases, this will use a [default implementor][iirnsp] that wraps the associated
/// [`IntoNamespacePath::NSPath`] type with [`RawNamespacePathWrapper`][rnspw].
///
/// [rnspw]: `crate::path::RawNamespacePathWrapper`
/// [iirnsp]: `IntoIntoRawNSPath`
pub trait IntoRawNamespacePath {
    type RawNSPath: RawNamespacePath;

    /// Convert this [`IntoRawNamespacePath`] into a [`RawNamespacePath`]
    fn into_raw_namespace_path(self) -> Self::RawNSPath;
}

impl<T: RawNamespacePath> IntoRawNamespacePath for T {
    type RawNSPath = Self;

    #[inline]
    fn into_raw_namespace_path(self) -> Self::RawNSPath {
        self
    }
}

/// Anything that can be converted-into a [`NamespacePath`]/path/segment. Analogous to
/// [`IntoIterator`]
///
/// This is automatically implemented for anything implementing [`NamespacePath`].
///
/// [irnsp]: `IntoRawNamespacePath`
pub trait IntoNamespacePath {
    type NSPath: NamespacePath;

    /// Obtain the namespace path from this structure.
    fn into_namespace_path(self) -> Self::NSPath;

    /// Convert this into something convertible into a [`RawNamespacePath`].
    ///
    /// By default, the produced value will reuse [`IntoNamespacePath::NSPath`] as the
    /// implementation for [`RawNamespacePath`], by wrapping it in
    /// [`RawNamespacePathWrapper`][rnspw].
    ///
    /// This is almost certainly what you want.
    ///
    /// [rnspw]: `crate::path::RawNamespacePathWrapper`
    #[inline(always)]
    fn into_into_raw(self) -> impl IntoRawNamespacePath
    where
        Self: Sized,
    {
        IntoIntoRawNSPath(self)
    }
}

impl<T: NamespacePath> IntoNamespacePath for T {
    type NSPath = Self;

    #[inline]
    fn into_namespace_path(self) -> Self::NSPath {
        self
    }
}

// nstree - nested namespace string-generating abstraction library
// Copyright (C) 2025  Matti <infomorphic-matti at protonmail dot com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
// ------
// SPDX-License-Identifier: GPL-3.0-or-later