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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! General CPE utilities
use language_tags::LanguageTag;

use crate::component::Component;
use crate::error::{CpeError, Result};
use crate::uri::*;
use crate::wfn::*;

use std::convert::TryFrom;
use std::str::FromStr;

/// CPE Language value
///
/// May be "ANY", or a valid RFC-5646 language tag.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub enum Language {
    #[default]
    Any,
    Language(LanguageTag),
}

impl FromStr for Language {
    type Err = CpeError;
    fn from_str(s: &str) -> Result<Self> {
        if s == "ANY" {
            Ok(Self::Any)
        } else {
            Ok(Self::Language(s.parse()?))
        }
    }
}

use std::fmt;
impl fmt::Display for Language {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Any => {
                if f.alternate() {
                    write!(f, "*")
                } else {
                    write!(f, "ANY")
                }
            }
            Self::Language(tag) => write!(f, "{}", tag),
        }
    }
}

/// Generic accesss to fields in a Uri or Wfn (either owned or borrowed).
/// If you have a borrowed `Uri` or `Wfn`, it is prefereble to use the methods
/// from the struct directly instead of this trait, as they return a `&Component`
/// instead of `Component`, and in the case that the string value of a component
/// required decoding, using this trait may result in a string clone.
pub trait Cpe {
    fn part(&self) -> CpeType;
    fn vendor(&self) -> Component;
    fn product(&self) -> Component;
    fn version(&self) -> Component;
    fn update(&self) -> Component;
    fn edition(&self) -> Component;
    fn language(&self) -> &Language;
    fn sw_edition(&self) -> Component;
    fn target_sw(&self) -> Component;
    fn target_hw(&self) -> Component;
    fn other(&self) -> Component;
}

macro_rules! impl_cpe {
    ($t:ty) => {
        impl Cpe for $t {
            fn part(&self) -> CpeType {
                self.part
            }
            fn vendor(&self) -> Component {
                self.vendor.as_component()
            }
            fn product(&self) -> Component {
                self.product.as_component()
            }
            fn version(&self) -> Component {
                self.version.as_component()
            }
            fn update(&self) -> Component {
                self.update.as_component()
            }
            fn edition(&self) -> Component {
                self.edition.as_component()
            }
            fn language(&self) -> &Language {
                &self.language
            }
            fn sw_edition(&self) -> Component {
                self.sw_edition.as_component()
            }
            fn target_sw(&self) -> Component {
                self.target_sw.as_component()
            }
            fn target_hw(&self) -> Component {
                self.target_hw.as_component()
            }
            fn other(&self) -> Component {
                self.other.as_component()
            }
        }
    };
    ($t:ty, $_l:lifetime) => {
        impl Cpe for $t {
            fn part(&self) -> CpeType {
                self.part
            }
            fn vendor(&self) -> Component {
                self.vendor.clone()
            }
            fn product(&self) -> Component {
                self.product.clone()
            }
            fn version(&self) -> Component {
                self.version.clone()
            }
            fn update(&self) -> Component {
                self.update.clone()
            }
            fn edition(&self) -> Component {
                self.edition.clone()
            }
            fn language(&self) -> &Language {
                &self.language
            }
            fn sw_edition(&self) -> Component {
                self.sw_edition.clone()
            }
            fn target_sw(&self) -> Component {
                self.target_sw.clone()
            }
            fn target_hw(&self) -> Component {
                self.target_hw.clone()
            }
            fn other(&self) -> Component {
                self.other.clone()
            }
        }
    };
}

impl_cpe!(OwnedUri);
impl_cpe!(OwnedWfn);
impl_cpe!(Uri<'_>, 'a);
impl_cpe!(Wfn<'_>, 'a);

/// A CPE Type Component
///
/// One of
/// * h - hardware
/// * o - operating system
/// * a - application
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CpeType {
    Any,
    Hardware,
    OperatingSystem,
    Application,
    Empty,
}

impl Default for CpeType {
    fn default() -> Self {
        Self::Any
    }
}

impl TryFrom<&str> for CpeType {
    type Error = CpeError;

    fn try_from(val: &str) -> Result<Self> {
        Ok(match val {
            "ANY" => Self::Any,
            "h" => Self::Hardware,
            "o" => Self::OperatingSystem,
            "a" => Self::Application,
            "" => Self::Empty,
            _ => {
                return Err(CpeError::InvalidCpeType {
                    value: val.to_owned(),
                })
            }
        })
    }
}

impl fmt::Display for CpeType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Any => {
                if f.alternate() {
                    write!(f, "*")
                } else {
                    write!(f, "ANY")
                }
            }
            Self::Hardware => write!(f, "h"),
            Self::OperatingSystem => write!(f, "o"),
            Self::Application => write!(f, "a"),
            Self::Empty => Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_missing_type() {
        assert!(CpeType::try_from("").is_ok());
    }

    #[test]
    fn test_invalid_type() {
        assert!(CpeType::try_from("x").is_err());
    }
}