forked from junkurihara/httpsig-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_value.rs
More file actions
71 lines (65 loc) · 2.21 KB
/
Copy pathcomponent_value.rs
File metadata and controls
71 lines (65 loc) · 2.21 KB
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
/* ---------------------------------------------------------------- */
#[derive(Debug, Clone, PartialEq, Eq)]
/// Http message component value
pub struct HttpMessageComponentValue {
/// inner value originally from http message header or derived from http message
inner: HttpMessageComponentValueInner,
}
impl From<&str> for HttpMessageComponentValue {
fn from(val: &str) -> Self {
Self {
inner: HttpMessageComponentValueInner::String(val.to_string()),
}
}
}
impl From<(&str, &str)> for HttpMessageComponentValue {
fn from((key, val): (&str, &str)) -> Self {
Self {
inner: HttpMessageComponentValueInner::KeyValue((key.to_string(), val.to_string())),
}
}
}
impl std::fmt::Display for HttpMessageComponentValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Http message component value inner, simple string or key-value pair
enum HttpMessageComponentValueInner {
/// Simple string value
String(String),
/// Key value pair, typically used for the value like `sig1=:xxxxx:` of signature-input
KeyValue((String, String)),
}
impl std::fmt::Display for HttpMessageComponentValueInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(val) => write!(f, "{}", val),
Self::KeyValue((_, val)) => write!(f, "{}", val),
}
}
}
impl HttpMessageComponentValue {
/// Get key if pair, otherwise None
pub fn key(&self) -> Option<&str> {
match &self.inner {
HttpMessageComponentValueInner::String(_) => None,
HttpMessageComponentValueInner::KeyValue((key, _)) => Some(key.as_ref()),
}
}
/// Get key value connected with `=`, or just value
pub fn as_field_value(&self) -> String {
match &self.inner {
HttpMessageComponentValueInner::String(val) => val.to_owned(),
HttpMessageComponentValueInner::KeyValue((key, val)) => format!("{}={}", key, val),
}
}
/// Get value only
pub fn as_component_value(&self) -> &str {
match &self.inner {
HttpMessageComponentValueInner::String(val) => val.as_ref(),
HttpMessageComponentValueInner::KeyValue((_, val)) => val.as_ref(),
}
}
}