tablo_test/protocol/
import.rs1use tablo_core::dom::import;
7
8use super::dom::{attr_value, tags_where};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum ImportRefusal {
14 NoFile,
16 Encoding,
18 Unterminated,
20 NoHeader,
22 NoRows,
24 TooManyRows,
26 TooManyCells,
28 UnknownColumn,
30 AmbiguousColumn,
32 RepeatedColumn,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct ImportError {
39 pub row: usize,
41 pub key: Option<String>,
43}
44
45pub fn import_refusal(html: &str) -> Option<ImportRefusal> {
52 let (_, attrs, _) = tags_where(html, |_, attrs| {
53 attr_value(attrs, "data-import-refusal").is_some()
54 })
55 .into_iter()
56 .next()?;
57 Some(match attr_value(&attrs, "data-import-refusal").as_deref() {
58 Some(import::NO_FILE) => ImportRefusal::NoFile,
59 Some(import::ENCODING) => ImportRefusal::Encoding,
60 Some(import::UNTERMINATED) => ImportRefusal::Unterminated,
61 Some(import::NO_HEADER) => ImportRefusal::NoHeader,
62 Some(import::NO_ROWS) => ImportRefusal::NoRows,
63 Some(import::TOO_MANY_ROWS) => ImportRefusal::TooManyRows,
64 Some(import::TOO_MANY_CELLS) => ImportRefusal::TooManyCells,
65 Some(import::UNKNOWN_COLUMN) => ImportRefusal::UnknownColumn,
66 Some(import::AMBIGUOUS_COLUMN) => ImportRefusal::AmbiguousColumn,
67 Some(import::REPEATED_COLUMN) => ImportRefusal::RepeatedColumn,
68 other => panic!("the import alert names no known refusal: {other:?}"),
69 })
70}
71
72pub fn import_errors(html: &str) -> Vec<ImportError> {
79 tags_where(html, |_, attrs| {
80 attr_value(attrs, "data-import-error").is_some()
81 })
82 .into_iter()
83 .map(|(_, attrs, _)| ImportError {
84 row: attr_value(&attrs, "data-import-error")
85 .and_then(|row| row.parse().ok())
86 .expect("an import error names its row"),
87 key: attr_value(&attrs, "data-import-key").filter(|key| !key.is_empty()),
88 })
89 .collect()
90}
91
92pub fn import_columns(html: &str) -> Vec<String> {
94 tags_where(html, |_, attrs| {
95 attr_value(attrs, "data-import-column").is_some()
96 })
97 .into_iter()
98 .filter_map(|(_, attrs, _)| attr_value(&attrs, "data-import-column"))
99 .collect()
100}