fetchium_core/multimodal/
chart.rs1use crate::error::FetchiumResult;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct TableData {
9 pub headers: Vec<String>,
10 pub rows: Vec<Vec<String>>,
11}
12
13pub fn extract_tables(html: &str) -> FetchiumResult<Vec<TableData>> {
15 let mut results = Vec::new();
16
17 let fragment = scraper::Html::parse_fragment(html);
18 let table_sel = scraper::Selector::parse("table").unwrap();
19 let tr_sel = scraper::Selector::parse("tr").unwrap();
20 let th_sel = scraper::Selector::parse("th").unwrap();
21 let td_sel = scraper::Selector::parse("td").unwrap();
22
23 for table in fragment.select(&table_sel) {
24 let mut headers = Vec::new();
25 let mut rows: Vec<Vec<String>> = Vec::new();
26
27 for (row_idx, row) in table.select(&tr_sel).enumerate() {
28 if row_idx == 0 {
29 headers = row
30 .select(&th_sel)
31 .map(|th| th.text().collect::<String>().trim().to_string())
32 .collect();
33 if headers.is_empty() {
34 let cells: Vec<String> = row
36 .select(&td_sel)
37 .map(|td| td.text().collect::<String>().trim().to_string())
38 .collect();
39 if !cells.is_empty() {
40 rows.push(cells);
41 }
42 continue;
43 }
44 }
45 let cells: Vec<String> = row
46 .select(&td_sel)
47 .map(|td| td.text().collect::<String>().trim().to_string())
48 .collect();
49 if !cells.is_empty() {
50 rows.push(cells);
51 }
52 }
53
54 if !rows.is_empty() {
55 results.push(TableData { headers, rows });
56 }
57 }
58
59 Ok(results)
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn extracts_simple_html_table() {
68 let html =
69 "<table><tr><th>Name</th><th>Value</th></tr><tr><td>Rust</td><td>95</td></tr></table>";
70 let tables = extract_tables(html).unwrap();
71 assert_eq!(tables.len(), 1);
72 assert_eq!(tables[0].headers, vec!["Name", "Value"]);
73 assert_eq!(tables[0].rows[0], vec!["Rust", "95"]);
74 }
75}