1use std::borrow::ToOwned;
7use std::default::Default;
8use std::hash::{Hash, Hasher};
9use std::ops::Deref;
10use std::str::FromStr;
11use std::{fmt, ops, slice, str};
12
13use js::context::JSContext;
14use js::gc::{HandleObject, HandleValue};
15use js::rust::wrappers2::ToJSON;
16
17pub use crate::domstring::DOMString;
18use crate::error::Error;
19
20#[derive(Clone, Debug, Default, Eq, JSTraceable, MallocSizeOf, PartialEq)]
22pub struct ByteString(Vec<u8>);
23
24impl ByteString {
25 pub fn new(value: Vec<u8>) -> ByteString {
27 ByteString(value)
28 }
29
30 pub fn as_str(&self) -> Option<&str> {
33 str::from_utf8(&self.0).ok()
34 }
35
36 pub fn len(&self) -> usize {
38 self.0.len()
39 }
40
41 pub fn is_empty(&self) -> bool {
43 self.0.is_empty()
44 }
45
46 pub fn to_lower(&self) -> ByteString {
48 ByteString::new(self.0.to_ascii_lowercase())
49 }
50}
51
52impl From<ByteString> for Vec<u8> {
53 fn from(byte_string: ByteString) -> Vec<u8> {
54 byte_string.0
55 }
56}
57
58impl Hash for ByteString {
59 fn hash<H: Hasher>(&self, state: &mut H) {
60 self.0.hash(state);
61 }
62}
63
64impl FromStr for ByteString {
65 type Err = ();
66 fn from_str(s: &str) -> Result<ByteString, ()> {
67 Ok(ByteString::new(s.to_owned().into_bytes()))
68 }
69}
70
71impl ops::Deref for ByteString {
72 type Target = [u8];
73 fn deref(&self) -> &[u8] {
74 &self.0
75 }
76}
77
78#[derive(Clone, Debug, Default, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd)]
81pub struct USVString(pub String);
82
83impl Deref for USVString {
84 type Target = str;
85
86 #[inline]
87 fn deref(&self) -> &str {
88 &self.0
89 }
90}
91
92impl AsRef<str> for USVString {
93 fn as_ref(&self) -> &str {
94 &self.0
95 }
96}
97
98impl fmt::Display for USVString {
99 #[inline]
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 fmt::Display::fmt(&self.0, f)
102 }
103}
104
105impl PartialEq<str> for USVString {
106 fn eq(&self, other: &str) -> bool {
107 self.0 == other
108 }
109}
110
111impl<'a> PartialEq<&'a str> for USVString {
112 fn eq(&self, other: &&'a str) -> bool {
113 self.0 == *other
114 }
115}
116
117impl From<String> for USVString {
118 fn from(contents: String) -> USVString {
119 USVString(contents)
120 }
121}
122
123impl From<USVString> for String {
124 fn from(value: USVString) -> Self {
125 value.0
126 }
127}
128
129impl From<USVString> for DOMString {
130 fn from(value: USVString) -> Self {
131 value.0.into()
132 }
133}
134
135pub fn is_token(s: &[u8]) -> bool {
138 if s.is_empty() {
139 return false; }
141 s.iter().all(|&x| {
142 match x {
144 0..=31 | 127 => false, 40 | 41 | 60 | 62 | 64 | 44 | 59 | 58 | 92 | 34 | 47 | 91 | 93 | 63 | 61 | 123 |
146 125 | 32 => false, x if x > 127 => false, _ => true,
149 }
150 })
151}
152
153pub fn serialize_jsval_to_json_utf8(
159 cx: &mut JSContext,
160 data: HandleValue,
161) -> Result<DOMString, Error> {
162 #[repr(C)]
163 struct ToJSONCallbackData {
164 string: Option<String>,
165 }
166
167 let mut out_str = ToJSONCallbackData { string: None };
168
169 #[expect(unsafe_code)]
170 unsafe extern "C" fn write_callback(
171 string: *const u16,
172 len: u32,
173 data: *mut std::ffi::c_void,
174 ) -> bool {
175 let data = data as *mut ToJSONCallbackData;
176 let string_chars = unsafe { slice::from_raw_parts(string, len as usize) };
177 unsafe { &mut *data }
178 .string
179 .get_or_insert_with(Default::default)
180 .push_str(&String::from_utf16_lossy(string_chars));
181 true
182 }
183
184 unsafe {
186 let stringify_result = ToJSON(
187 cx,
188 data,
189 HandleObject::null(),
190 HandleValue::null(),
191 Some(write_callback),
192 &mut out_str as *mut ToJSONCallbackData as *mut _,
193 );
194 if !stringify_result {
197 return Err(Error::JSFailed);
198 }
199 }
200
201 out_str
206 .string
207 .map(Into::into)
208 .ok_or_else(|| Error::Type(c"unable to serialize JSON".to_owned()))
209}