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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::result::{ Result as StdResult };
use std::fmt::{self, Display};

use soft_ascii_string::SoftAsciiString;

use ::HeaderTryFrom;
use ::error::ComponentCreationError;

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

/// a Input is similar to Item a container data container used in different
/// context's with different restrictions, but different to an Item it
/// might contain characters which require encoding (e.g. encoded words)
/// to represent them
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Input( pub InnerUtf8 );


impl Input {

    pub fn into_shared( self ) -> Self {
        Input( self.0.into_shared() )
    }


    pub fn into_ascii_item( self ) -> StdResult<InnerAscii, Input> {
        match self {
            Input( InnerUtf8::Owned( string ) ) => {
                match SoftAsciiString::from_string(string) {
                    Ok(asciied) => Ok(InnerAscii::Owned(asciied)),
                    Err(err) => Err(Input(InnerUtf8::Owned(err.into_source())))
                }
            }
            Input( InnerUtf8::Shared( shared ) ) => {
                if shared.is_ascii() {
                    Ok(InnerAscii::Owned(SoftAsciiString::from_unchecked(&*shared)))
                } else {
                    Err(Input(InnerUtf8::Shared(shared)))
                }
            }
        }
    }

    pub fn into_ascii_item_unchecked( self ) -> InnerAscii {
        match self {
            Input( InnerUtf8::Owned( string ) ) =>
                InnerAscii::Owned( SoftAsciiString::from_unchecked( string ) ),
            Input( InnerUtf8::Shared( shared ) ) =>
                InnerAscii::Owned(
                    SoftAsciiString::from_unchecked(&*shared) )
        }
    }

    pub fn into_utf8_item( self ) -> InnerUtf8 {
        self.0
    }
}

impl<'a> From<&'a str> for Input {
    fn from( s: &'a str ) -> Self {
        Input( InnerUtf8::Owned( s.into() ) )
    }
}

impl From<String> for Input {
    fn from( s: String ) -> Self {
        Input( InnerUtf8::Owned( s ) )
    }
}

impl<'a> HeaderTryFrom<&'a str> for Input
{
    fn try_from(val: &'a str) -> Result<Self, ComponentCreationError> {
        Ok(val.into())
    }
}
impl HeaderTryFrom<String> for Input
{
    fn try_from(val: String) -> Result<Self, ComponentCreationError> {
        Ok(val.into())
    }
}

impl Into<String> for Input {
    fn into(self) -> String {
        self.0.into()
    }
}

impl Display for Input {
    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
        fter.write_str(self.as_str())
    }
}

deref0!( +mut Input => InnerUtf8 );



#[cfg(test)]
mod test {
    use std::sync::Arc;
    use owning_ref::OwningRef;

    use super::*;

    #[test]
    fn input_eq() {
        let a = Input( InnerUtf8::Owned( "same".into() ) );
        let b = Input( InnerUtf8::Shared(
            OwningRef::new(
                Arc::new( String::from( "same" ) ) )
                .map(|v| &**v)
        ) );
        assert_eq!( a, b );
    }

    #[test]
    fn input_neq() {
        let a = Input( InnerUtf8::Owned( "not same".into() ) );
        let b = Input( InnerUtf8::Shared(
            OwningRef::new(
                Arc::new( String::from( "not at all same" ) ) )
                .map(|v| &**v)
        ) );
        assert_ne!( a, b );
    }



}