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
use super::super::{IanaToken, XName};
use super::super::values::Value;
use super::super::values::parameter_value::ParameterValues;
use super::super::Set;
use super::Parameter;

use std::fmt::{self, Display, Formatter, Write};
use std::hash::{Hash, Hasher};

use validators::{Validated, ValidatedWrapper};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Any {
    IanaToken(IanaToken, ParameterValues),
    XName(XName, ParameterValues),
}

impl Any {
    pub fn is_empty(&self) -> bool {
        match self {
            Any::IanaToken(_, v) => {
                v.is_empty()
            }
            Any::XName(_, v) => {
                v.is_empty()
            }
        }
    }
}

impl Parameter for Any {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        if self.is_empty() {
            return Ok(());
        }

        f.write_char(';')?;

        let set = match self {
            Any::IanaToken(a, b) => {
                f.write_str(a.as_str())?;
                b
            }
            Any::XName(a, b) => {
                f.write_str(a.as_str())?;
                b
            }
        };

        f.write_char('=')?;

        Value::fmt(set, f)?;

        Ok(())
    }
}

impl Parameter for Set<Any> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        for e in self.as_hash_set() {
            Parameter::fmt(e, f)?;
        }

        Ok(())
    }
}

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

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

impl Hash for Any {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Any::XName(a, b) => {
                state.write(a.as_str().as_bytes());
                b.hash(state);
            }
            Any::IanaToken(a, b) => {
                state.write(a.as_str().as_bytes());
                b.hash(state);
            }
        }
    }
}

impl Validated for Any {}

impl ValidatedWrapper for Any {
    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!();
    }
}