| 1 | use super::parse::*; |
| 2 | use alloc::{borrow::Cow, fmt, string::String, vec}; |
| 3 | use core::{ |
| 4 | cmp::Ordering, |
| 5 | hash::{Hash, Hasher}, |
| 6 | iter, |
| 7 | }; |
| 8 | |
| 9 | /// A media-type parameter value. |
| 10 | /// |
| 11 | /// The constructor accepts both an unquoted string and a quoted string like `"Hello Wold!"`. |
| 12 | /// If the string is not quoted, the allowed characters are |
| 13 | /// alphabets, numbers and `!#$&-^_.+%*'`. |
| 14 | /// |
| 15 | /// ``` |
| 16 | /// # use mediatype::Value; |
| 17 | /// let utf8 = Value::new("UTF-8").unwrap(); |
| 18 | /// let utf8_quoted = Value::new("\"UTF-8\"").unwrap(); |
| 19 | /// assert_eq!(utf8, utf8_quoted); |
| 20 | /// assert_eq!(utf8_quoted.as_str(), "\"UTF-8\""); |
| 21 | /// assert_eq!(utf8_quoted.unquoted_str(), "UTF-8"); |
| 22 | /// |
| 23 | /// let double_quoted = Value::new("\" \\\" \"").unwrap(); |
| 24 | /// assert_eq!(double_quoted.as_str(), "\" \\\" \""); |
| 25 | /// assert_eq!(double_quoted.unquoted_str(), " \" "); |
| 26 | /// ``` |
| 27 | #[derive(Debug, Copy, Clone)] |
| 28 | pub struct Value<'a>(&'a str); |
| 29 | |
| 30 | impl<'a> Value<'a> { |
| 31 | /// Constructs a `Value`. |
| 32 | /// |
| 33 | /// If the string is not valid as a value, returns `None`. |
| 34 | #[must_use] |
| 35 | pub fn new(s: &'a str) -> Option<Self> { |
| 36 | if let Some(quoted) = s.strip_prefix('\"') { |
| 37 | if quoted.len() > 1 && parse_quoted_value(quoted) == Ok(quoted.len()) { |
| 38 | return Some(Self(s)); |
| 39 | } |
| 40 | } else if is_restricted_str(s) { |
| 41 | return Some(Self(s)); |
| 42 | } |
| 43 | None |
| 44 | } |
| 45 | |
| 46 | /// Returns the underlying string. |
| 47 | #[must_use] |
| 48 | pub const fn as_str(&self) -> &'a str { |
| 49 | self.0 |
| 50 | } |
| 51 | |
| 52 | /// Returns the unquoted string. |
| 53 | #[must_use] |
| 54 | pub fn unquoted_str(&self) -> Cow<'_, str> { |
| 55 | if self.0.starts_with('"') { |
| 56 | let inner = &self.0[1..self.0.len() - 1]; |
| 57 | if inner.contains('\\') { |
| 58 | let mut s = String::with_capacity(inner.len()); |
| 59 | let mut quoted = false; |
| 60 | for c in inner.chars() { |
| 61 | match c { |
| 62 | _ if quoted => { |
| 63 | quoted = false; |
| 64 | s.push(c); |
| 65 | } |
| 66 | '\\' => { |
| 67 | quoted = true; |
| 68 | } |
| 69 | _ => { |
| 70 | s.push(c); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | Cow::Owned(s) |
| 75 | } else { |
| 76 | Cow::Borrowed(inner) |
| 77 | } |
| 78 | } else { |
| 79 | Cow::Borrowed(self.0) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Generates a quoted string if necessary. |
| 84 | /// |
| 85 | /// ``` |
| 86 | /// # use mediatype::Value; |
| 87 | /// assert_eq!(Value::quote("UTF-8"), "UTF-8"); |
| 88 | /// assert_eq!(Value::quote("Hello world!"), "\"Hello world!\""); |
| 89 | /// assert_eq!( |
| 90 | /// Value::quote(r#" "What's wrong?" "#), |
| 91 | /// "\" \\\"What's wrong\\?\\\" \"" |
| 92 | /// ); |
| 93 | /// ``` |
| 94 | #[must_use] |
| 95 | pub fn quote(s: &str) -> Cow<'_, str> { |
| 96 | if is_restricted_str(s) { |
| 97 | Cow::Borrowed(s) |
| 98 | } else { |
| 99 | let inner = s.chars().flat_map(|c| { |
| 100 | if is_restricted_char(c) || c == ' ' { |
| 101 | vec![c] |
| 102 | } else { |
| 103 | vec!['\\', c] |
| 104 | } |
| 105 | }); |
| 106 | let quoted = iter::once('"').chain(inner).chain(iter::once('"')); |
| 107 | Cow::Owned(quoted.collect()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | pub(crate) const fn new_unchecked(s: &'a str) -> Self { |
| 112 | Self(s) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl fmt::Display for Value<'_> { |
| 117 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | f.write_str(self.0) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl PartialEq for Value<'_> { |
| 123 | fn eq(&self, other: &Self) -> bool { |
| 124 | self.unquoted_str() == other.unquoted_str() |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl Eq for Value<'_> {} |
| 129 | |
| 130 | impl PartialOrd for Value<'_> { |
| 131 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 132 | Some(self.cmp(other)) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | impl Ord for Value<'_> { |
| 137 | fn cmp(&self, other: &Self) -> Ordering { |
| 138 | self.unquoted_str().cmp(&other.unquoted_str()) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl Hash for Value<'_> { |
| 143 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 144 | self.unquoted_str().hash(state); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl PartialEq<String> for Value<'_> { |
| 149 | fn eq(&self, other: &String) -> bool { |
| 150 | self.eq(other.as_str()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl PartialOrd<String> for Value<'_> { |
| 155 | fn partial_cmp(&self, other: &String) -> Option<Ordering> { |
| 156 | self.partial_cmp(other.as_str()) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | impl PartialEq<&String> for Value<'_> { |
| 161 | fn eq(&self, other: &&String) -> bool { |
| 162 | self.eq(other.as_str()) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | impl PartialOrd<&String> for Value<'_> { |
| 167 | fn partial_cmp(&self, other: &&String) -> Option<Ordering> { |
| 168 | self.partial_cmp(other.as_str()) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl PartialEq<str> for Value<'_> { |
| 173 | fn eq(&self, other: &str) -> bool { |
| 174 | self.unquoted_str() == other |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | impl PartialOrd<str> for Value<'_> { |
| 179 | fn partial_cmp(&self, other: &str) -> Option<Ordering> { |
| 180 | Some(self.unquoted_str().as_ref().cmp(other)) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | impl PartialEq<&str> for Value<'_> { |
| 185 | fn eq(&self, other: &&str) -> bool { |
| 186 | self.eq(*other) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | impl PartialOrd<&str> for Value<'_> { |
| 191 | fn partial_cmp(&self, other: &&str) -> Option<Ordering> { |
| 192 | self.partial_cmp(*other) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl PartialEq<Cow<'_, str>> for Value<'_> { |
| 197 | fn eq(&self, other: &Cow<'_, str>) -> bool { |
| 198 | self.eq(other.as_ref()) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl PartialOrd<Cow<'_, str>> for Value<'_> { |
| 203 | fn partial_cmp(&self, other: &Cow<'_, str>) -> Option<Ordering> { |
| 204 | self.partial_cmp(other.as_ref()) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | impl PartialEq<&Cow<'_, str>> for Value<'_> { |
| 209 | fn eq(&self, other: &&Cow<'_, str>) -> bool { |
| 210 | self.eq(other.as_ref()) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl PartialOrd<&Cow<'_, str>> for Value<'_> { |
| 215 | fn partial_cmp(&self, other: &&Cow<'_, str>) -> Option<Ordering> { |
| 216 | self.partial_cmp(other.as_ref()) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod tests { |
| 222 | use super::*; |
| 223 | |
| 224 | #[test] |
| 225 | fn unquoted_str() { |
| 226 | assert_eq!(Value::new("\"\\a\\\\\"").unwrap().unquoted_str(), "a\\"); |
| 227 | assert_eq!(Value::new("\"\\\"\"").unwrap().unquoted_str(), "\""); |
| 228 | assert_eq!(Value::new("\"\\a\\b\\c\"").unwrap().unquoted_str(), "abc"); |
| 229 | assert_eq!( |
| 230 | Value::new("\" \\\"What's wrong\\?\\\" \"") |
| 231 | .unwrap() |
| 232 | .unquoted_str(), |
| 233 | r#" "What's wrong?" "# |
| 234 | ); |
| 235 | } |
| 236 | } |