1use super::*;
4
5#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
7pub struct HTML;
8impl Format for HTML {
9 fn escape(f: &mut Formatter, mut s: &str) -> Result<(), Error> {
10 let badstuff = "<>&\"'/";
11 while let Some(idx) = s.find(|c| badstuff.contains(c)) {
12 let (first, rest) = s.split_at(idx);
13 let (badchar, tail) = rest.split_at(1);
14 f.write_str(first)?;
15 f.write_str(match badchar {
16 "<" => "<",
17 ">" => ">",
18 "&" => "&",
19 "\"" => """,
20 "'" => "'",
21 "/" => "/",
22 _ => unreachable!(),
23 })?;
24 s = tail;
25 }
26 f.write_str(s)
27 }
28 fn mime() -> mime::Mime {
30 return mime::TEXT_HTML_UTF_8;
31 }
32 fn this_format() -> Self {
33 HTML
34 }
35}
36
37macro_rules! display_as_from_display {
38 ($format:ty, $type:ty) => {
39 impl DisplayAs<$format> for $type {
40 fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
41 (&self as &dyn Display).fmt(f)
42 }
43 }
44 };
45}
46
47#[macro_export]
49macro_rules! display_integers_as {
50 ($format:ty) => {
51 display_as_from_display!($format, i8);
52 display_as_from_display!($format, u8);
53 display_as_from_display!($format, i16);
54 display_as_from_display!($format, u16);
55 display_as_from_display!($format, i32);
56 display_as_from_display!($format, u32);
57 display_as_from_display!($format, i64);
58 display_as_from_display!($format, u64);
59 display_as_from_display!($format, i128);
60 display_as_from_display!($format, u128);
61 display_as_from_display!($format, isize);
62 display_as_from_display!($format, usize);
63 };
64}
65
66display_integers_as!(HTML);
67
68#[macro_export]
100macro_rules! display_floats_as {
101 ($format:ty, $e:expr, $after_e:expr, $e_cost:expr, $power_ten:expr) => {
102 impl $crate::DisplayAs<$format> for f64 {
103 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
104 $crate::float::Floating::from(*self).fmt_with(f, $e, $after_e, $e_cost, $power_ten)
105 }
106 }
107 impl $crate::DisplayAs<$format> for f32 {
108 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
109 $crate::float::Floating::from(*self).fmt_with(f, $e, $after_e, $e_cost, $power_ten)
110 }
111 }
112 };
113}
114display_floats_as!(HTML, "×10<sup>", "</sup>", 3, Some("10<sup>"));
115
116#[test]
117fn escaping() {
118 assert_eq!(&format_as!(HTML, ("&")).into_string(), "&");
119 assert_eq!(
120 &format_as!(HTML, ("hello &>this is cool")).into_string(),
121 "hello &>this is cool"
122 );
123 assert_eq!(
124 &format_as!(HTML, ("hello &>this is 'cool")).into_string(),
125 "hello &>this is 'cool"
126 );
127}
128#[test]
129fn floats() {
130 assert_eq!(&format_as!(HTML, 3.0).into_string(), "3");
131 assert_eq!(&format_as!(HTML, 3e5).into_string(), "3×10<sup>5</sup>");
132 assert_eq!(&format_as!(HTML, 1e-6).into_string(), "10<sup>-6</sup>");
133 assert_eq!(&format_as!(HTML, 3e4).into_string(), "30000");
134}