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
use std::ops::Deref;

use soft_ascii_string::{ SoftAsciiStr, SoftAsciiString};

use super::input::Input;
use super::inner_item::{ InnerAscii, InnerUtf8 };

#[cfg(feature="serde")]
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub enum SimpleItem {
    /// specifies that the Item is valid Ascii, nothing more
    Ascii( InnerAscii ),
    /// specifies that the Item is valid Utf8, nothing more
    Utf8( InnerUtf8 )
}

impl SimpleItem {

    pub fn as_str( &self ) -> &str {
        use self::SimpleItem::*;
        match *self {
            Ascii( ref value ) => value.as_str(),
            Utf8( ref value ) => value.as_str()
        }
    }

    pub fn is_ascii( &self ) -> bool {
        use self::SimpleItem::*;
        match *self {
            Ascii( .. ) => true,
            Utf8( .. ) => false
        }
    }

    pub fn from_utf8_input( s: Input ) -> Self {
        SimpleItem::Utf8( s.0 )
    }

    pub fn from_utf8( s: String ) -> Self {
        SimpleItem::Utf8( InnerUtf8::Owned( s ) )
    }


}

impl Deref for SimpleItem {
    type Target = str;

    fn deref( &self ) -> &str {
        use self::SimpleItem::*;
        match *self {
            Ascii( ref astr ) => astr.as_str(),
            Utf8( ref utf8 ) => &**utf8
        }
    }
}


impl Into<String> for SimpleItem {
    fn into(self) -> String {
        use self::SimpleItem::*;
        match self {
            Ascii( aitem ) => {
                let astring: SoftAsciiString = aitem.into();
                astring.into()
            },
            Utf8( string ) => string.into()
        }
    }
}

impl<'a> From<&'a str> for SimpleItem {
    fn from( string: &'a str ) -> Self {
        Self::from( String::from( string ) )
    }
}

impl From<String> for SimpleItem {
    fn from( string: String ) -> Self {
        match SoftAsciiString::from_string( string ) {
            Ok( astring ) => SimpleItem::Ascii( InnerAscii::Owned( astring ) ),
            Err( err ) => SimpleItem::Utf8( InnerUtf8::Owned( err.into_source() ) )
        }
    }
}

impl From<SoftAsciiString> for SimpleItem {
    fn from( astring: SoftAsciiString ) -> Self {
        SimpleItem::Ascii( InnerAscii::Owned( astring ) )
    }
}

impl From<Input> for SimpleItem {
    fn from(input: Input) -> Self {
        match input {
            Input( InnerUtf8::Owned( string ) ) => match SoftAsciiString::from_string( string ) {
                Ok( ascii ) => SimpleItem::Ascii( InnerAscii::Owned( ascii ) ),
                Err( err ) => SimpleItem::Utf8( InnerUtf8::Owned( err.into_source() ) )
            },
            Input( InnerUtf8::Shared( shared ) ) => {
                if shared.is_ascii() {
                    let a_shared = shared.map(|s| SoftAsciiStr::from_unchecked(s));
                    SimpleItem::Ascii(InnerAscii::Shared(a_shared))
                } else {
                    SimpleItem::Utf8(InnerUtf8::Shared(shared))
                }
            }
        }
    }
}