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
use super::{validate_static, validate_with_normalized_percent_encoding, InvalidQuery};
use shared_bytes::SharedStr;
use std::{hash::Hash, sync::Arc};
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Query(SharedStr);
impl Query {
#[track_caller]
#[inline]
pub const fn from_static(string: &'static str) -> Self {
match validate_static(string.as_bytes()) {
Ok(()) => Self(SharedStr::from_static(string)),
Err(_e) => panic!("invalid static Query"),
}
}
#[inline]
pub fn as_shared_str(&self) -> &SharedStr {
&self.0
}
#[inline]
pub fn into_shared_str(self) -> SharedStr {
self.0
}
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl PartialEq<str> for Query {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<String> for Query {
fn eq(&self, other: &String) -> bool {
self.as_str() == other
}
}
impl AsRef<str> for Query {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Eq for Query {}
impl From<Query> for SharedStr {
fn from(x: Query) -> Self {
x.into_shared_str()
}
}
impl From<Query> for String {
fn from(x: Query) -> Self {
x.into_shared_str().into_string()
}
}
impl<T> PartialEq<&'_ T> for Query
where
Self: PartialEq<T>,
T: ?Sized,
{
fn eq(&self, other: &&T) -> bool {
self.eq(*other)
}
}
impl TryFrom<&'static str> for Query {
type Error = InvalidQuery;
fn try_from(value: &'static str) -> Result<Self, Self::Error> {
let inner = validate_with_normalized_percent_encoding(value)?
.unwrap_or_else(|| SharedStr::from(value));
Ok(Self(inner))
}
}
impl TryFrom<String> for Query {
type Error = InvalidQuery;
fn try_from(value: String) -> Result<Self, Self::Error> {
let inner = validate_with_normalized_percent_encoding(&value)?
.unwrap_or_else(|| SharedStr::from(value));
Ok(Self(inner))
}
}
impl TryFrom<Arc<String>> for Query {
type Error = InvalidQuery;
fn try_from(value: Arc<String>) -> Result<Self, Self::Error> {
let inner = validate_with_normalized_percent_encoding(&value)?
.unwrap_or_else(|| SharedStr::from(value));
Ok(Self(inner))
}
}
impl TryFrom<SharedStr> for Query {
type Error = InvalidQuery;
fn try_from(value: SharedStr) -> Result<Self, Self::Error> {
let inner = validate_with_normalized_percent_encoding(&value)?.unwrap_or(value);
Ok(Self(inner))
}
}