tablo_test/protocol/
choice_create.rs1use super::dom::{attr_value, open_tags};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct CreateOptionLink {
9 pub select: String,
11 pub href: String,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum Picked {
18 Option {
20 value: String,
22 label: String,
24 },
25 Unchoosable,
27}
28
29pub fn create_option_links(html: &str) -> Vec<CreateOptionLink> {
31 let tags = open_tags(html);
32 tags.iter()
33 .filter(|(tag, _, _)| tag == "a")
34 .filter_map(|(_, attrs, _)| {
35 let dialog = attr_value(attrs, "data-create-option")?;
36 let select = tags.iter().find_map(|(tag, attrs, _)| {
37 (tag == "dialog" && attr_value(attrs, "id").as_deref() == Some(dialog.as_str()))
38 .then(|| attr_value(attrs, "data-create-option-select"))
39 .flatten()
40 })?;
41 Some(CreateOptionLink {
42 select,
43 href: attr_value(attrs, "href")?,
44 })
45 })
46 .collect()
47}
48
49pub fn picked(html: &str) -> Option<Picked> {
51 open_tags(html).into_iter().find_map(|(_, attrs, _)| {
52 if attr_value(&attrs, "data-picked-hidden").is_some() {
53 return Some(Picked::Unchoosable);
54 }
55 attr_value(&attrs, "data-picked")?;
56 Some(Picked::Option {
57 value: attr_value(&attrs, "data-picked-value")?,
58 label: attr_value(&attrs, "data-picked-label")?,
59 })
60 })
61}
62
63pub fn posts_picking(html: &str) -> bool {
66 open_tags(html).iter().any(|(tag, attrs, _)| {
67 tag == "form"
68 && attr_value(attrs, "data-form-guard").is_some()
69 && attr_value(attrs, "action").is_some_and(|action| {
70 action
71 .split_once('?')
72 .is_some_and(|(_, query)| query.split('&').any(|pair| pair == "pick=1"))
73 })
74 })
75}