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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use crate::ZInt;
use core::fmt;
use std::{borrow::Cow, convert::TryFrom};

mod consts {
    pub(super) const MIMES: [&str; 21] = [
        /*  0 */ "",
        /*  1 */ "application/octet-stream",
        /*  2 */ "application/custom", // non iana standard
        /*  3 */ "text/plain",
        /*  4 */ "application/properties", // non iana standard
        /*  5 */ "application/json", // if not readable from casual users
        /*  6 */ "application/sql",
        /*  7 */ "application/integer", // non iana standard
        /*  8 */ "application/float", // non iana standard
        /*  9 */
        "application/xml", // if not readable from casual users (RFC 3023, sec 3)
        /* 10 */ "application/xhtml+xml",
        /* 11 */ "application/x-www-form-urlencoded",
        /* 12 */ "text/json", // non iana standard - if readable from casual users
        /* 13 */ "text/html",
        /* 14 */ "text/xml", // if readable from casual users (RFC 3023, section 3)
        /* 15 */ "text/css",
        /* 16 */ "text/csv",
        /* 17 */ "text/javascript",
        /* 18 */ "image/jpeg",
        /* 19 */ "image/png",
        /* 20 */ "image/gif",
    ];
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KnownEncoding {
    Empty = 0,
    AppOctetStream = 1,
    AppCustom = 2,
    TextPlain = 3,
    AppProperties = 4,
    AppJson = 5,
    AppSql = 6,
    AppInteger = 7,
    AppFloat = 8,
    AppXml = 9,
    AppXhtmlXml = 10,
    AppXWwwFormUrlencoded = 11,
    TextJson = 12,
    TextHtml = 13,
    TextXml = 14,
    TextCss = 15,
    TextCsv = 16,
    TextJavascript = 17,
    ImageJpeg = 18,
    ImagePng = 19,
    ImageGif = 20,
}

impl From<KnownEncoding> for u8 {
    fn from(val: KnownEncoding) -> Self {
        unsafe { std::mem::transmute(val) }
    }
}

impl From<KnownEncoding> for &str {
    fn from(val: KnownEncoding) -> Self {
        consts::MIMES[usize::from(val)]
    }
}

impl From<KnownEncoding> for usize {
    fn from(val: KnownEncoding) -> Self {
        u8::from(val) as usize
    }
}

impl std::convert::TryFrom<u8> for KnownEncoding {
    type Error = ();
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        if value < consts::MIMES.len() as u8 + 1 {
            Ok(unsafe { std::mem::transmute(value) })
        } else {
            Err(())
        }
    }
}

impl std::convert::TryFrom<ZInt> for KnownEncoding {
    type Error = ();

    fn try_from(value: ZInt) -> Result<Self, Self::Error> {
        if value < consts::MIMES.len() as ZInt + 1 {
            Ok(unsafe { std::mem::transmute(value as u8) })
        } else {
            Err(())
        }
    }
}

impl AsRef<str> for KnownEncoding {
    fn as_ref(&self) -> &str {
        consts::MIMES[usize::from(*self)]
    }
}
/// The encoding of a zenoh `zenoh::Value`.
///
/// A zenoh encoding is a HTTP Mime type represented, for wire efficiency,
/// as an integer prefix (that maps to a string) and a string suffix.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Encoding {
    Exact(KnownEncoding),
    WithSuffix(KnownEncoding, Cow<'static, str>),
}

impl Encoding {
    pub fn new<IntoCowStr>(prefix: ZInt, suffix: IntoCowStr) -> Option<Self>
    where
        IntoCowStr: Into<Cow<'static, str>> + AsRef<str>,
    {
        let prefix = KnownEncoding::try_from(prefix).ok()?;
        if suffix.as_ref().is_empty() {
            Some(Encoding::Exact(prefix))
        } else {
            Some(Encoding::WithSuffix(prefix, suffix.into()))
        }
    }

    /// Sets the suffix of this encoding.
    pub fn with_suffix<IntoCowStr>(self, suffix: IntoCowStr) -> Self
    where
        IntoCowStr: Into<Cow<'static, str>>,
    {
        match self {
            Encoding::Exact(e) => Encoding::WithSuffix(e, suffix.into()),
            Encoding::WithSuffix(e, s) => {
                Encoding::WithSuffix(e, Cow::Owned(format!("{}{}", s, suffix.into())))
            }
        }
    }

    pub fn as_ref<'a, T>(&'a self) -> T
    where
        &'a Self: Into<T>,
    {
        self.into()
    }

    /// Returns `true`if the string representation of this encoding starts with
    /// the string representation of ther given encoding.
    pub fn starts_with<T>(&self, with: T) -> bool
    where
        T: Into<Encoding>,
    {
        let with: Encoding = with.into();
        self.prefix() == with.prefix() && self.suffix().starts_with(with.suffix())
    }

    pub const fn prefix(&self) -> &KnownEncoding {
        match self {
            Encoding::Exact(e) | Encoding::WithSuffix(e, _) => e,
        }
    }

    pub fn suffix(&self) -> &str {
        match self {
            Encoding::Exact(_) => "",
            Encoding::WithSuffix(_, s) => s.as_ref(),
        }
    }
}

impl Encoding {
    pub const EMPTY: Encoding = Encoding::Exact(KnownEncoding::Empty);
    pub const APP_OCTET_STREAM: Encoding = Encoding::Exact(KnownEncoding::AppOctetStream);
    pub const APP_CUSTOM: Encoding = Encoding::Exact(KnownEncoding::AppCustom);
    pub const TEXT_PLAIN: Encoding = Encoding::Exact(KnownEncoding::TextPlain);
    pub const APP_PROPERTIES: Encoding = Encoding::Exact(KnownEncoding::AppProperties);
    pub const APP_JSON: Encoding = Encoding::Exact(KnownEncoding::AppJson);
    pub const APP_SQL: Encoding = Encoding::Exact(KnownEncoding::AppSql);
    pub const APP_INTEGER: Encoding = Encoding::Exact(KnownEncoding::AppInteger);
    pub const APP_FLOAT: Encoding = Encoding::Exact(KnownEncoding::AppFloat);
    pub const APP_XML: Encoding = Encoding::Exact(KnownEncoding::AppXml);
    pub const APP_XHTML_XML: Encoding = Encoding::Exact(KnownEncoding::AppXhtmlXml);
    pub const APP_XWWW_FORM_URLENCODED: Encoding =
        Encoding::Exact(KnownEncoding::AppXWwwFormUrlencoded);
    pub const TEXT_JSON: Encoding = Encoding::Exact(KnownEncoding::TextJson);
    pub const TEXT_HTML: Encoding = Encoding::Exact(KnownEncoding::TextHtml);
    pub const TEXT_XML: Encoding = Encoding::Exact(KnownEncoding::TextXml);
    pub const TEXT_CSS: Encoding = Encoding::Exact(KnownEncoding::TextCss);
    pub const TEXT_CSV: Encoding = Encoding::Exact(KnownEncoding::TextCsv);
    pub const TEXT_JAVASCRIPT: Encoding = Encoding::Exact(KnownEncoding::TextJavascript);
    pub const IMAGE_JPEG: Encoding = Encoding::Exact(KnownEncoding::ImageJpeg);
    pub const IMAGE_PNG: Encoding = Encoding::Exact(KnownEncoding::ImagePng);
    pub const IMAGE_GIF: Encoding = Encoding::Exact(KnownEncoding::ImageGif);
}

impl fmt::Display for Encoding {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Encoding::Exact(e) => f.write_str(e.as_ref()),
            Encoding::WithSuffix(e, s) => {
                f.write_str(e.as_ref())?;
                f.write_str(s)
            }
        }
    }
}

impl From<&'static str> for Encoding {
    fn from(s: &'static str) -> Self {
        for (i, v) in consts::MIMES.iter().enumerate().skip(1) {
            if let Some(suffix) = s.strip_prefix(v) {
                if suffix.is_empty() {
                    return Encoding::Exact(unsafe { std::mem::transmute(i as u8) });
                } else {
                    return Encoding::WithSuffix(
                        unsafe { std::mem::transmute(i as u8) },
                        suffix.into(),
                    );
                }
            }
        }
        if s.is_empty() {
            Encoding::Exact(KnownEncoding::Empty)
        } else {
            Encoding::WithSuffix(KnownEncoding::Empty, s.into())
        }
    }
}

impl From<String> for Encoding {
    fn from(mut s: String) -> Self {
        for (i, v) in consts::MIMES.iter().enumerate().skip(1) {
            if s.starts_with(v) {
                s.replace_range(..v.len(), "");
                if s.is_empty() {
                    return Encoding::Exact(unsafe { std::mem::transmute(i as u8) });
                } else {
                    return Encoding::WithSuffix(unsafe { std::mem::transmute(i as u8) }, s.into());
                }
            }
        }
        if s.is_empty() {
            Encoding::Exact(KnownEncoding::Empty)
        } else {
            Encoding::WithSuffix(KnownEncoding::Empty, s.into())
        }
    }
}

impl From<&KnownEncoding> for Encoding {
    fn from(e: &KnownEncoding) -> Encoding {
        Encoding::Exact(*e)
    }
}

impl From<KnownEncoding> for Encoding {
    fn from(e: KnownEncoding) -> Encoding {
        Encoding::Exact(e)
    }
}

impl Default for Encoding {
    fn default() -> Self {
        KnownEncoding::Empty.into()
    }
}