aboutsummaryrefslogtreecommitdiffstats
path: root/rust/fatcat-openapi/src/header.rs
blob: 7589ab08901b6d37a3b022a6c76c28e941c4bcc6 (plain)
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
use chrono::{DateTime, Utc};
use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt;
use std::ops::Deref;

/// A struct to allow homogeneous conversion into a HeaderValue. We can't
/// implement the From/Into trait on HeaderValue because we don't own
/// either of the types.
#[derive(Debug, Clone)]
pub(crate) struct IntoHeaderValue<T>(pub T);

// Generic implementations

impl<T> Deref for IntoHeaderValue<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

// Derive for each TryFrom<T> in hyper::header::HeaderValue

macro_rules! ihv_generate {
    ($t:ident) => {
        impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
            type Error = String;

            fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
                match hdr_value.to_str() {
                    Ok(hdr_value) => match hdr_value.parse::<$t>() {
                        Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
                        Err(e) => Err(format!(
                            "Unable to parse {} as a string: {}",
                            stringify!($t),
                            e
                        )),
                    },
                    Err(e) => Err(format!(
                        "Unable to parse header {:?} as a string - {}",
                        hdr_value, e
                    )),
                }
            }
        }

        impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
            type Error = String;

            fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
                Ok(hdr_value.0.into())
            }
        }
    };
}

ihv_generate!(u64);
ihv_generate!(i64);
ihv_generate!(i16);
ihv_generate!(u16);
ihv_generate!(u32);
ihv_generate!(usize);
ihv_generate!(isize);
ihv_generate!(i32);

// Custom derivations

// Vec<String>

impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
    type Error = String;

    fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
        match hdr_value.to_str() {
            Ok(hdr_value) => Ok(IntoHeaderValue(
                hdr_value
                    .split(',')
                    .filter_map(|x| match x.trim() {
                        "" => None,
                        y => Some(y.to_string()),
                    })
                    .collect(),
            )),
            Err(e) => Err(format!(
                "Unable to parse header: {:?} as a string - {}",
                hdr_value, e
            )),
        }
    }
}

impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
    type Error = String;

    fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
        match HeaderValue::from_str(&hdr_value.0.join(", ")) {
            Ok(hdr_value) => Ok(hdr_value),
            Err(e) => Err(format!(
                "Unable to convert {:?} into a header - {}",
                hdr_value, e
            )),
        }
    }
}

// String

impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
    type Error = String;

    fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
        match hdr_value.to_str() {
            Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
            Err(e) => Err(format!("Unable to convert header {:?} to {}", hdr_value, e)),
        }
    }
}

impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
    type Error = String;

    fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
        match HeaderValue::from_str(&hdr_value.0) {
            Ok(hdr_value) => Ok(hdr_value),
            Err(e) => Err(format!(
                "Unable to convert {:?} from a header {}",
                hdr_value, e
            )),
        }
    }
}

// bool
impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
    type Error = String;

    fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
        match hdr_value.to_str() {
            Ok(hdr_value) => match hdr_value.parse() {
                Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
                Err(e) => Err(format!("Unable to parse bool from {} - {}", hdr_value, e)),
            },
            Err(e) => Err(format!(
                "Unable to convert {:?} from a header {}",
                hdr_value, e
            )),
        }
    }
}

impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
    type Error = String;

    fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
        match HeaderValue::from_str(&hdr_value.0.to_string()) {
            Ok(hdr_value) => Ok(hdr_value),
            Err(e) => Err(format!(
                "Unable to convert: {:?} into a header: {}",
                hdr_value, e
            )),
        }
    }
}

// DateTime

impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
    type Error = String;

    fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
        match hdr_value.to_str() {
            Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
                Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
                Err(e) => Err(format!("Unable to parse: {} as date - {}", hdr_value, e)),
            },
            Err(e) => Err(format!(
                "Unable to convert header {:?} to string {}",
                hdr_value, e
            )),
        }
    }
}

impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
    type Error = String;

    fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
        match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
            Ok(hdr_value) => Ok(hdr_value),
            Err(e) => Err(format!(
                "Unable to convert {:?} to a header: {}",
                hdr_value, e
            )),
        }
    }
}