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
use super::*;
use crate::PATH_PERCENT_ENCODE_SET;

use std::fmt::{Display, Write};

use validators::{Validated, ValidatedWrapper};

// TODO: not implement yet, refer to [RFC2045]

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AttributeValue {
    attribute: String,
    value: String,
}

#[derive(Clone, Debug, PartialEq)]
pub enum AttributeValueError {}

impl AttributeValue {
    pub fn from_str(attribute: &str, value: &str) -> Result<AttributeValue, AttributeValueError> {
        Ok(AttributeValue {
            attribute: attribute.to_string(),
            value: value.to_string(),
        })
    }

    pub fn from_string(
        attribute: String,
        value: String,
    ) -> Result<AttributeValue, AttributeValueError> {
        Ok(AttributeValue {
            attribute,
            value,
        })
    }

    pub fn is_empty(&self) -> bool {
        self.attribute.is_empty() || self.value.is_empty()
    }
}

impl AttributeValue {
    pub fn get_attribute(&self) -> &str {
        &self.attribute
    }

    pub fn value(&self) -> &str {
        &self.value
    }
}

impl Value for AttributeValue {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        if self.is_empty() {
            return Ok(());
        }
        f.write_char(';')?;
        f.write_str(
            &percent_encoding::utf8_percent_encode(
                self.attribute.as_str(),
                PATH_PERCENT_ENCODE_SET,
            )
            .to_string(),
        )?;
        f.write_char('=')?;
        f.write_str(
            &percent_encoding::utf8_percent_encode(self.value.as_str(), PATH_PERCENT_ENCODE_SET)
                .to_string(),
        )?;

        Ok(())
    }
}

impl Display for AttributeValue {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        Value::fmt(self, f)
    }
}

impl Validated for AttributeValue {}

impl ValidatedWrapper for AttributeValue {
    type Error = &'static str;

    fn from_string(_from_string_input: String) -> Result<Self, Self::Error> {
        unimplemented!();
    }

    fn from_str(_from_str_input: &str) -> Result<Self, Self::Error> {
        unimplemented!();
    }
}