Skip to main content

map_to_javascript_html/maps/
b_tree_map.rs

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