simple_table/
simple_table.rs1use html_export::composed::table::from_iterator;
2use html_export::composed::table::AsTable;
3use html_export::elem;
4use html_export::element::*;
5use html_export::head::Head;
6use html_export::tags::*;
7use html_export::thead;
8use html_export::*;
9extern crate html_export;
10
11pub struct Person {
12 name: String,
13 age: u8,
14 country: String,
15}
16
17impl Person {
18 pub fn new(name: String, age: u8, country: String) -> Self {
19 Self { name, age, country }
20 }
21}
22
23impl AsTable for Person {
24 fn as_table_head(&self) -> Option<html_export::element::Element> {
25 let mut head = thead!();
26 let mut row = tr!();
27 row += th!() + text!("Name");
28 row += th!() + text!("Age");
29 row += th!() + text!("Country");
30 head += row;
31 Some(head)
32 }
33
34 fn as_table_row(&self) -> Option<html_export::element::Element> {
35 let mut row = tr!();
36 row += td!() + text!(self.name.clone());
37 row += td!() + text!(self.age.to_string());
38 row += td!() + text!(self.country.clone());
39 Some(row)
40 }
41
42 fn as_table_foot(&self) -> Option<html_export::element::Element> {
43 None
44 }
45}
46
47fn main() {
48 let persons = vec![
49 Person::new("Johns James".to_string(), 20, "France".to_string()),
50 Person::new("Smith Jennifer".to_string(), 30, "England".to_string()),
51 Person::new("Brown Robert".to_string(), 40, "Australia".to_string()),
52 Person::new("Taylor Mary".to_string(), 50, "South Africa".to_string()),
53 ];
54 let table = from_iterator(
55 &persons,
56 HtmlElementConfig::new_empty(),
57 HtmlElementConfig::new_empty(),
58 )
59 .unwrap();
60 let head = Head::new().with_title("Simple table".to_string());
61 export_to_file(
62 "examples_output".to_string(),
63 "simple_table.html".to_string(),
64 head,
65 vec![table],
66 )
67 .unwrap();
68}