1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

use crate::Error;
use crate::TinyStr16;

#[cfg(any(feature = "std", test))]
pub use std::string::String;

#[cfg(all(not(feature = "std"), not(test)))]
extern crate alloc;

#[cfg(all(not(feature = "std"), not(test)))]
pub use alloc::string::String;

/// An ASCII string that is tiny when <= 16 chars and a String otherwise.
///
/// # Examples
///
/// ```
/// use tinystr::TinyStrAuto;
///
/// let s1: TinyStrAuto = "Testing".parse()
///     .expect("Failed to parse.");
///
/// assert_eq!(s1, "Testing");
/// ```
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum TinyStrAuto {
    /// Up to 16 characters stored on the stack.
    Tiny(TinyStr16),
    /// 17 or more characters stored on the heap.
    Heap(String),
}

impl fmt::Display for TinyStrAuto {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.deref().fmt(f)
    }
}

impl Deref for TinyStrAuto {
    type Target = str;

    #[inline(always)]
    fn deref(&self) -> &str {
        use TinyStrAuto::*;
        match self {
            Tiny(value) => value.deref(),
            Heap(value) => value.deref(),
        }
    }
}

impl PartialEq<&str> for TinyStrAuto {
    #[inline(always)]
    fn eq(&self, other: &&str) -> bool {
        self.deref() == *other
    }
}

impl FromStr for TinyStrAuto {
    type Err = Error;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        if text.len() <= 16 {
            TinyStr16::from_str(text).map(TinyStrAuto::Tiny)
        } else if text.is_ascii() {
            Ok(TinyStrAuto::Heap(text.into()))
        } else {
            Err(Error::NonAscii)
        }
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for TinyStrAuto {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for TinyStrAuto {
    fn deserialize<D>(deserializer: D) -> Result<TinyStrAuto, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error as SerdeError;
        use alloc::borrow::Cow;
        #[cfg(not(feature = "std"))]
        use alloc::string::ToString;

        let x: Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
        x.parse()
            .map_err(|e: Error| SerdeError::custom(e.to_string()))
    }
}