map_to_javascript_html/maps/
serde_json_map.rs

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