Skip to main content

map_to_javascript_html/
traits.rs

1use alloc::{string::String, vec::Vec};
2use core::{borrow::Borrow, fmt::Display, hash::Hash, str::from_utf8};
3#[cfg(feature = "std")]
4use std::io::{self, Write};
5
6/// Serializing a map to JavaScript code in HTML.
7pub trait MapToJavaScriptHTML<K> {
8    /// Convert this map to minified JavaScript code in HTML. Be careful of the `variable_name` which will not be encoded in HTML.
9    #[inline]
10    fn to_javascript_html<S: Display>(&self, variable_name: S) -> String {
11        let mut s = String::new();
12
13        self.to_javascript_html_to_string(variable_name, &mut s);
14
15        s
16    }
17
18    /// 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.
19    #[inline]
20    fn to_javascript_html_to_string<'a, S: Display>(
21        &self,
22        variable_name: S,
23        output: &'a mut String,
24    ) -> &'a str {
25        let mut buffer = Vec::new();
26        let html = self.to_javascript_html_to_vec(variable_name, &mut buffer);
27        let html = from_utf8(html).expect("MapToJavaScriptHTML implementations must return UTF-8");
28        let current_length = output.len();
29
30        output.push_str(html);
31
32        &output[current_length..]
33    }
34
35    /// 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.
36    fn to_javascript_html_to_vec<'a, S: Display>(
37        &self,
38        variable_name: S,
39        output: &'a mut Vec<u8>,
40    ) -> &'a [u8];
41
42    #[cfg(feature = "std")]
43    /// 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.
44    fn to_javascript_html_to_writer<S: Display, W: Write>(
45        &self,
46        variable_name: S,
47        output: &mut W,
48    ) -> Result<(), io::Error>;
49
50    /// 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.
51    #[inline]
52    fn to_javascript_html_with_keys<S: Display, KS: ?Sized + Display + Ord + Eq + Hash>(
53        &self,
54        variable_name: S,
55        keys: &[&KS],
56    ) -> String
57    where
58        K: Borrow<KS>, {
59        let mut s = String::new();
60
61        self.to_javascript_html_with_keys_to_string(variable_name, keys, &mut s);
62
63        s
64    }
65
66    /// 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.
67    fn to_javascript_html_with_keys_to_string<
68        'a,
69        S: Display,
70        KS: ?Sized + Display + Ord + Eq + Hash,
71    >(
72        &self,
73        variable_name: S,
74        keys: &[&KS],
75        output: &'a mut String,
76    ) -> &'a str
77    where
78        K: Borrow<KS>, {
79        let mut buffer = Vec::new();
80        let html = self.to_javascript_html_with_keys_to_vec(variable_name, keys, &mut buffer);
81        let html = from_utf8(html).expect("MapToJavaScriptHTML implementations must return UTF-8");
82        let current_length = output.len();
83
84        output.push_str(html);
85
86        &output[current_length..]
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}