definition_list/
definition_list.rs

1use std::collections::HashMap;
2
3use html_export::composed::list::from_iterator;
4use html_export::composed::list::term_definition_list_from_map;
5use html_export::composed::list::AsList;
6use html_export::composed::list::ListType;
7use html_export::dd;
8use html_export::dt;
9use html_export::elem;
10use html_export::element::*;
11use html_export::export_to_file;
12use html_export::head::Head;
13use html_export::tags::*;
14
15pub struct ProgrammingLanguage {
16    name: String,
17    published_year: u32,
18}
19
20impl ProgrammingLanguage {
21    pub fn new(name: String, published_year: u32) -> Self {
22        Self {
23            name,
24            published_year,
25        }
26    }
27}
28
29impl AsList for ProgrammingLanguage {
30    fn to_list_item(&self, _mode: &ListType) -> Option<Element> {
31        None
32    }
33
34    fn to_definition_list_item(&self) -> Option<(Element, Element)> {
35        Some((
36            Element::Text(self.name.clone()),
37            Element::Text(self.published_year.to_string()),
38        ))
39    }
40}
41
42#[derive(Eq, Hash, PartialEq)]
43pub struct Country(String);
44pub struct Population(u64);
45
46impl AsList for Country {
47    fn to_list_item(&self, mode: &ListType) -> Option<Element> {
48        match mode {
49            ListType::Ordered | ListType::Unordered => None,
50            ListType::TermDefinition => Some(dt!() + Element::Text(self.0.clone())),
51        }
52    }
53
54    fn to_definition_list_item(&self) -> Option<(Element, Element)> {
55        None
56    }
57}
58
59impl AsList for Population {
60    fn to_list_item(&self, mode: &ListType) -> Option<Element> {
61        match mode {
62            ListType::Ordered | ListType::Unordered => None,
63            ListType::TermDefinition => Some(dd!() + Element::Text(self.0.to_string())),
64        }
65    }
66
67    fn to_definition_list_item(&self) -> Option<(Element, Element)> {
68        None
69    }
70}
71
72fn main() {
73    let mut map = HashMap::new();
74    map.insert(Country("India".to_string()), Population(1_450_935_791));
75    map.insert(Country("China".to_string()), Population(1_419_321_278));
76    map.insert(Country("Usa".to_string()), Population(345_426_571));
77    map.insert(Country("Indonesia".to_string()), Population(283_487_931));
78
79    let languages = vec![
80        ProgrammingLanguage::new("Rust".to_string(), 2006),
81        ProgrammingLanguage::new("C".to_string(), 1972),
82        ProgrammingLanguage::new("Python".to_string(), 1991),
83        ProgrammingLanguage::new("Haskell".to_string(), 1990),
84    ];
85
86    let head = Head::new().with_title("Definition list".to_string());
87
88    export_to_file(
89        "examples_output".to_string(),
90        "definition_list.html".to_string(),
91        head,
92        vec![
93            term_definition_list_from_map(&map, HtmlElementConfig::new_empty()).unwrap(),
94            from_iterator(
95                &languages,
96                &ListType::TermDefinition,
97                HtmlElementConfig::new_empty(),
98            )
99            .unwrap(),
100        ],
101    )
102    .unwrap();
103}