map_to_javascript_html/maps/
b_tree_map.rs1use core::borrow::Borrow;
2use core::fmt::Display;
3
4use alloc::collections::BTreeMap;
5use alloc::vec::Vec;
6
7#[cfg(feature = "std")]
8use std::io::{self, Write};
9
10use crate::MapToJavaScriptHTML;
11
12impl<K: Display + Ord, V: Display> MapToJavaScriptHTML<K> for BTreeMap<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
24 for (key, value) in self {
25 output.extend_from_slice(variable_name.as_bytes());
26 output.extend_from_slice(b"['");
27 html_escape::encode_script_single_quoted_text_to_vec(format!("{}", key), output);
28 output.extend_from_slice(b"']='");
29 html_escape::encode_script_single_quoted_text_to_vec(format!("{}", value), output);
30 output.extend_from_slice(b"';");
31 }
32
33 &output[current_length..]
34 }
35
36 #[cfg(feature = "std")]
37 fn to_javascript_html_to_writer<S: Display, W: Write>(
38 &self,
39 variable_name: S,
40 output: &mut W,
41 ) -> Result<(), io::Error> {
42 let variable_name = format!("{}", variable_name);
43
44 for (key, value) in self {
45 output.write_all(variable_name.as_bytes())?;
46 output.write_all(b"['")?;
47 html_escape::encode_script_single_quoted_text_to_writer(format!("{}", key), output)?;
48 output.write_all(b"']='")?;
49 html_escape::encode_script_single_quoted_text_to_writer(format!("{}", value), output)?;
50 output.write_all(b"';")?;
51 }
52
53 Ok(())
54 }
55
56 #[inline]
57 fn to_javascript_html_with_keys_to_vec<'a, S: Display, KS: ?Sized + Display + Ord>(
58 &self,
59 variable_name: S,
60 keys: &[&KS],
61 output: &'a mut Vec<u8>,
62 ) -> &'a [u8]
63 where
64 K: Borrow<KS>, {
65 let variable_name = format!("{}", variable_name);
66
67 let current_length = output.len();
68
69 output.reserve((variable_name.len() + 11) * self.len());
70
71 for key in keys.iter() {
72 output.extend_from_slice(variable_name.as_bytes());
73 output.extend_from_slice(b"['");
74 html_escape::encode_script_single_quoted_text_to_vec(format!("{}", key), output);
75 output.extend_from_slice(b"']=");
76 match self.get(key) {
77 Some(value) => {
78 output.push(b'\'');
79 html_escape::encode_script_single_quoted_text_to_vec(
80 format!("{}", value),
81 output,
82 );
83 output.extend_from_slice(b"';");
84 }
85 None => {
86 output.extend_from_slice(b"undefined;");
87 }
88 }
89 }
90
91 unsafe { output.get_unchecked(current_length..) }
92 }
93
94 #[cfg(feature = "std")]
95 fn to_javascript_html_with_keys_to_writer<S: Display, W: Write, KS: ?Sized + Display + Ord>(
96 &self,
97 variable_name: S,
98 keys: &[&KS],
99 output: &mut W,
100 ) -> Result<(), io::Error>
101 where
102 K: Borrow<KS>, {
103 let variable_name = format!("{}", variable_name);
104
105 for key in keys.iter() {
106 output.write_all(variable_name.as_bytes())?;
107 output.write_all(b"['")?;
108 html_escape::encode_script_single_quoted_text_to_writer(format!("{}", key), output)?;
109 output.write_all(b"']=")?;
110 match self.get(key) {
111 Some(value) => {
112 output.write_all(b"'")?;
113 html_escape::encode_script_single_quoted_text_to_writer(
114 format!("{}", value),
115 output,
116 )?;
117 output.write_all(b"';")?;
118 }
119 None => {
120 output.write_all(b"undefined;")?;
121 }
122 }
123 }
124
125 Ok(())
126 }
127}