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
104
105
106
107
108
109
110
111
112
113
114
115
use core::fmt;

use crate::constants::{MAX_INF_LEN, MAX_MIN_LEN, MAX_NAN_LEN};
use crate::errors::Error;

/// Simple wrapper type for a `&str` to make sure its length is less than the maximum for
/// an infinity symbol (64 bytes).
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InfinityStr<'a>(&'a str);

impl<'a> InfinityStr<'a> {
    /// Constructs an `InfinityStr`, ensuring that the length is less than the maximum for
    /// an infinity symbol (64 bytes).
    ///
    /// # Errors
    ///
    /// Returns an `Error` if the provided `&str`'s length is more than 64 bytes.
    pub fn new(s: &'a str) -> Result<InfinityStr<'a>, Error> {
        if s.len() > MAX_INF_LEN {
            return Err(Error::capacity(MAX_INF_LEN));
        }
        Ok(InfinityStr(s))
    }

    /// Allows recovery of the initial / wrapped `&str`.
    pub fn into_str(self) -> &'a str {
        self.0
    }
}

impl<'a> fmt::Debug for InfinityStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl<'a> fmt::Display for InfinityStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Simple wrapper type for a `&str` to make sure its length is less than the maximum for
/// a minus sign (7 bytes).
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct MinusSignStr<'a>(&'a str);

impl<'a> MinusSignStr<'a> {
    /// Constructs a `MinusSignStr`, ensuring that the length is less than the maximum for
    /// a minus sign (7 bytes).
    ///
    /// # Errors
    ///
    /// Returns an `Error` if the provided `&str`'s length is more than 7 bytes.
    pub fn new(s: &'a str) -> Result<MinusSignStr<'a>, Error> {
        if s.len() > MAX_MIN_LEN {
            return Err(Error::capacity(MAX_MIN_LEN));
        }
        Ok(MinusSignStr(s))
    }

    /// Allows recovery of the initial / wrapped `&str`.
    pub fn into_str(self) -> &'a str {
        self.0
    }
}

impl<'a> fmt::Debug for MinusSignStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl<'a> fmt::Display for MinusSignStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Simple wrapper type for a `&str` to make sure its length is less than the maximum for
/// a nan symbol (64 bytes).
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NanStr<'a>(&'a str);

impl<'a> NanStr<'a> {
    /// Constructs an `NanStr`, ensuring that the length is less than the maximum for
    /// a nan symbol (64 bytes).
    ///
    /// # Errors
    ///
    /// Returns an `Error` if the provided `&str`'s length is more than 64 bytes.
    pub fn new(s: &'a str) -> Result<NanStr<'a>, Error> {
        if s.len() > MAX_NAN_LEN {
            return Err(Error::capacity(MAX_NAN_LEN));
        }
        Ok(NanStr(s))
    }

    /// Allows recovery of the initial / wrapped `&str`.
    pub fn into_str(self) -> &'a str {
        self.0
    }
}

impl<'a> fmt::Debug for NanStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl<'a> fmt::Display for NanStr<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}