use std::fmt::Display;
use crate::action::transform::field::Replace;
#[derive(Clone, Debug)]
pub enum ElementKind {
Tag(String),
Class(String),
Attr {
name: String,
value: String,
},
}
#[derive(Clone, Debug)]
pub enum DataLocation {
Text,
Attr(String),
}
#[derive(Clone, Debug)]
pub struct ElementQuery {
pub kind: ElementKind,
pub ignore: Option<Vec<ElementKind>>,
}
#[derive(Debug)]
pub struct ElementDataQuery {
pub optional: bool,
pub query: Vec<ElementQuery>,
pub data_location: DataLocation,
pub regex: Option<Replace>,
}
pub trait ElementQuerySliceExt {
fn display(&self) -> ElementQuerySliceDisplay<'_>;
}
pub struct ElementQuerySliceDisplay<'a> {
slice: &'a [ElementQuery],
}
impl ElementQuerySliceExt for [ElementQuery] {
fn display(&self) -> ElementQuerySliceDisplay<'_> {
ElementQuerySliceDisplay { slice: self }
}
}
impl Display for ElementQuerySliceDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.slice.is_empty() {
return write!(f, "[]");
}
writeln!(f, "[")?;
for (i, elem) in self.slice.iter().enumerate() {
write!(f, " #{}: ", i + 1)?;
match &elem.kind {
ElementKind::Tag(t) => write!(f, "<{t}/>")?,
ElementKind::Class(c) => write!(f, "<tag class=\"{c}\">")?,
ElementKind::Attr { name, value } => write!(f, "<tag {name}=\"{value}\"/>")?,
}
writeln!(f, ",")?;
}
writeln!(f, "]")?;
Ok(())
}
}