map_to_javascript_html/maps/
hash_map.rs1use alloc::{string::String, vec::Vec};
2use core::{borrow::Borrow, fmt::Display, hash::Hash, str::from_utf8_unchecked};
3#[cfg(feature = "std")]
4use std::collections::HashMap;
5#[cfg(feature = "std")]
6use std::io::{self, Write};
7
8#[cfg(feature = "std")]
9use crate::encode::encode_display_to_writer;
10use crate::{encode::encode_display_to_vec, MapToJavaScriptHTML};
11
12impl<K: Display + Eq + Hash, V: Display> MapToJavaScriptHTML<K> for HashMap<K, V> {
13 fn to_javascript_html_to_vec<'a, S: Display>(
14 &self,
15 variable_name: S,
16 output: &'a mut Vec<u8>,
17 ) -> &'a [u8] {
18 let variable_name = format!("{}", variable_name);
19
20 let current_length = output.len();
21
22 output.reserve((variable_name.len() + 11) * self.len());
23 let mut scratch = Vec::new();
24
25 for (key, value) in self {
26 output.extend_from_slice(variable_name.as_bytes());
27 output.push(b'[');
28 encode_display_to_vec(key, &mut scratch, output);
29 output.extend_from_slice(b"]=");
30 encode_display_to_vec(value, &mut scratch, output);
31 output.push(b';');
32 }
33
34 &output[current_length..]
35 }
36
37 #[inline]
38 fn to_javascript_html_to_string<'a, S: Display>(
39 &self,
40 variable_name: S,
41 output: &'a mut String,
42 ) -> &'a str {
43 unsafe {
45 from_utf8_unchecked(self.to_javascript_html_to_vec(variable_name, output.as_mut_vec()))
46 }
47 }
48
49 #[cfg(feature = "std")]
50 fn to_javascript_html_to_writer<S: Display, W: Write>(
51 &self,
52 variable_name: S,
53 output: &mut W,
54 ) -> Result<(), io::Error> {
55 let variable_name = format!("{}", variable_name);
56 let mut scratch = Vec::new();
57
58 for (key, value) in self {
59 output.write_all(variable_name.as_bytes())?;
60 output.write_all(b"[")?;
61 encode_display_to_writer(key, &mut scratch, output)?;
62 output.write_all(b"]=")?;
63 encode_display_to_writer(value, &mut scratch, output)?;
64 output.write_all(b";")?;
65 }
66
67 Ok(())
68 }
69
70 #[inline]
71 fn to_javascript_html_with_keys_to_vec<'a, S: Display, KS: ?Sized + Display + Eq + Hash>(
72 &self,
73 variable_name: S,
74 keys: &[&KS],
75 output: &'a mut Vec<u8>,
76 ) -> &'a [u8]
77 where
78 K: Borrow<KS>, {
79 let variable_name = format!("{}", variable_name);
80
81 let current_length = output.len();
82
83 output.reserve((variable_name.len() + 11) * keys.len());
84 let mut scratch = Vec::new();
85
86 for key in keys {
87 output.extend_from_slice(variable_name.as_bytes());
88 output.push(b'[');
89 encode_display_to_vec(*key, &mut scratch, output);
90 output.extend_from_slice(b"]=");
91 match self.get(key) {
92 Some(value) => {
93 encode_display_to_vec(value, &mut scratch, output);
94 output.push(b';');
95 },
96 None => {
97 output.extend_from_slice(b"undefined;");
98 },
99 }
100 }
101
102 &output[current_length..]
103 }
104
105 #[inline]
106 fn to_javascript_html_with_keys_to_string<
107 'a,
108 S: Display,
109 KS: ?Sized + Display + Ord + Eq + Hash,
110 >(
111 &self,
112 variable_name: S,
113 keys: &[&KS],
114 output: &'a mut String,
115 ) -> &'a str
116 where
117 K: Borrow<KS>, {
118 unsafe {
120 from_utf8_unchecked(self.to_javascript_html_with_keys_to_vec(
121 variable_name,
122 keys,
123 output.as_mut_vec(),
124 ))
125 }
126 }
127
128 #[cfg(feature = "std")]
129 fn to_javascript_html_with_keys_to_writer<
130 S: Display,
131 W: Write,
132 KS: ?Sized + Display + Eq + Hash,
133 >(
134 &self,
135 variable_name: S,
136 keys: &[&KS],
137 output: &mut W,
138 ) -> Result<(), io::Error>
139 where
140 K: Borrow<KS>, {
141 let variable_name = format!("{}", variable_name);
142 let mut scratch = Vec::new();
143
144 for key in keys {
145 output.write_all(variable_name.as_bytes())?;
146 output.write_all(b"[")?;
147 encode_display_to_writer(*key, &mut scratch, output)?;
148 output.write_all(b"]=")?;
149 match self.get(key) {
150 Some(value) => {
151 encode_display_to_writer(value, &mut scratch, output)?;
152 output.write_all(b";")?;
153 },
154 None => {
155 output.write_all(b"undefined;")?;
156 },
157 }
158 }
159
160 Ok(())
161 }
162}