Documentation
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

SJ

Copyright (C) 2019-2025  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2019-2025".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Shortcuts for `Json::String`

use {
    alloc::{
        borrow::Cow,
        string::{String, ToString},
    },
    crate::{Error, Json, Result},
};

/// # Shortcuts for [`String`](#variant.String)
impl Json {

    /// # If the value is a string, returns an immutable reference of it
    ///
    /// Returns an error if the value is not a string.
    pub fn as_str(&self) -> Result<&str> {
        match self {
            Json::String(s) => Ok(s),
            _ => Err(e!("Json is not a String")),
        }
    }

    /// # If the value is a string, returns a mutable reference of it
    ///
    /// Returns an error if the value is not a string.
    pub fn as_mut_str(&mut self) -> Result<&mut String> {
        match self {
            Json::String(s) => Ok(s),
            _ => Err(e!("Json is not a String")),
        }
    }

}

impl From<String> for Json {

    fn from(s: String) -> Self {
        Json::String(s)
    }

}

impl From<&str> for Json {

    fn from(s: &str) -> Self {
        Json::String(s.to_string())
    }

}

impl From<Cow<'_, str>> for Json {

    fn from(s: Cow<str>) -> Self {
        Self::from(s.into_owned())
    }

}

impl TryFrom<Json> for String {

    type Error = Error;

    fn try_from(value: Json) -> core::result::Result<Self, Self::Error> {
        match value {
            Json::String(s) => Ok(s),
            _ => Err(e!("Json is not a String")),
        }
    }

}

impl PartialEq<String> for Json {

    fn eq(&self, other: &String) -> bool {
        match self {
            Self::String(s) => s == other,
            _ => false,
        }
    }

}

impl PartialEq<Json> for String {

    fn eq(&self, other: &Json) -> bool {
        match other {
            Json::String(s) => self == s,
            _ => false,
        }
    }

}

impl PartialEq<str> for Json {

    fn eq(&self, other: &str) -> bool {
        match self {
            Self::String(s) => s == other,
            _ => false,
        }
    }

}

impl PartialEq<Json> for str {

    fn eq(&self, other: &Json) -> bool {
        match other {
            Json::String(s) => self == s,
            _ => false,
        }
    }

}