map_to_javascript_html/
traits.rs

1use core::borrow::Borrow;
2use core::fmt::Display;
3use core::hash::Hash;
4use core::str::from_utf8_unchecked;
5
6use alloc::string::String;
7use alloc::vec::Vec;
8
9#[cfg(feature = "std")]
10use std::io::{self, Write};
11
12/// Serializing a map to JavaScript code in HTML.
13pub trait MapToJavaScriptHTML<K> {
14    /// Convert this map to minified JavaScript code in HTML. Be careful of the `variable_name` which will not be encoded in HTML.
15    #[inline]
16    fn to_javascript_html<S: Display>(&self, variable_name: S) -> String {
17        let mut s = String::new();
18
19        self.to_javascript_html_to_string(variable_name, &mut s);
20
21        s
22    }
23
24    /// Convert this map to minified JavaScript code in HTML. Write it to an existing `String` and return a string slice of the written HTML. Be careful of the `variable_name` which will not be encoded in HTML.
25    #[inline]
26    fn to_javascript_html_to_string<'a, S: Display>(
27        &self,
28        variable_name: S,
29        output: &'a mut String,
30    ) -> &'a str {
31        unsafe {
32            from_utf8_unchecked(self.to_javascript_html_to_vec(variable_name, output.as_mut_vec()))
33        }
34    }
35
36    /// Convert this map to minified JavaScript code in HTML. Write it to an existing `Vec<u8>` and return a `u8` slice of the written HTML. Be careful of the `variable_name` which will not be encoded in HTML.
37    fn to_javascript_html_to_vec<'a, S: Display>(
38        &self,
39        variable_name: S,
40        output: &'a mut Vec<u8>,
41    ) -> &'a [u8];
42
43    #[cfg(feature = "std")]
44    /// Convert this map to minified JavaScript code in HTML. Write it to a writer. Be careful of the `variable_name` which will not be encoded in HTML.
45    fn to_javascript_html_to_writer<S: Display, W: Write>(
46        &self,
47        variable_name: S,
48        output: &mut W,
49    ) -> Result<(), io::Error>;
50
51    /// Convert this map to minified JavaScript code in HTML by given keys. If the key doesn't exist, the output value will be `undefined`. Be careful of the `variable_name` which will not be encoded in HTML.
52    #[inline]
53    fn to_javascript_html_with_keys<S: Display, KS: ?Sized + Display + Ord + Eq + Hash>(
54        &self,
55        variable_name: S,
56        keys: &[&KS],
57    ) -> String
58    where
59        K: Borrow<KS>, {
60        let mut s = String::new();
61
62        self.to_javascript_html_with_keys_to_string(variable_name, keys, &mut s);
63
64        s
65    }
66
67    /// Convert this map to minified JavaScript code in HTML by given keys. Write it to an existing `String` and return a string slice of the written HTML. If the key doesn't exist, the output value will be `undefined`. Be careful of the `variable_name` which will not be encoded in HTML.
68    fn to_javascript_html_with_keys_to_string<
69        'a,
70        S: Display,
71        KS: ?Sized + Display + Ord + Eq + Hash,
72    >(
73        &self,
74        variable_name: S,
75        keys: &[&KS],
76        output: &'a mut String,
77    ) -> &'a str
78    where
79        K: Borrow<KS>, {
80        unsafe {
81            from_utf8_unchecked(self.to_javascript_html_with_keys_to_vec(
82                variable_name,
83                keys,
84                output.as_mut_vec(),
85            ))
86        }
87    }
88
89    /// Convert this map to minified JavaScript code in HTML by given keys. Write it to an existing `Vec<u8>` and return a `u8` slice of the written HTML. If the key doesn't exist, the output value will be `undefined`. Be careful of the `variable_name` which will not be encoded in HTML.
90    fn to_javascript_html_with_keys_to_vec<'a, S: Display, KS: ?Sized + Display + Ord + Eq + Hash>(
91        &self,
92        variable_name: S,
93        keys: &[&KS],
94        output: &'a mut Vec<u8>,
95    ) -> &'a [u8]
96    where
97        K: Borrow<KS>;
98
99    #[cfg(feature = "std")]
100    /// Convert this map to minified JavaScript code in HTML. Write it to a writer. If the key doesn't exist, the output value will be `undefined`. Be careful of the `variable_name` which will not be encoded in HTML.
101    fn to_javascript_html_with_keys_to_writer<
102        S: Display,
103        W: Write,
104        KS: ?Sized + Display + Ord + Eq + Hash,
105    >(
106        &self,
107        variable_name: S,
108        keys: &[&KS],
109        output: &mut W,
110    ) -> Result<(), io::Error>
111    where
112        K: Borrow<KS>;
113}