map_to_javascript_html/maps/
hash_map.rs

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