use serde::Serialize;
use serde_json::{Map, Value};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ParsedPage {
pub page_type: Option<String>,
pub source: Option<String>,
pub data: Value,
pub raw: Value,
pub data_extracted: bool,
pub html: Option<String>,
pub detail: Option<String>,
}
impl ParsedPage {
pub(crate) fn from_value(payload: Value) -> Self {
let inner = match payload.get("data") {
Some(v) if !v.is_null() => v.clone(),
Some(_) => Value::Object(Map::new()),
None => payload.clone(),
};
let text = |v: &Value, key: &str| v.get(key).and_then(Value::as_str).map(str::to_owned);
ParsedPage {
page_type: text(&inner, "page_type"),
source: text(&inner, "source"),
data: inner.get("data").cloned().unwrap_or(Value::Null),
data_extracted: payload.get("data_extracted") != Some(&Value::Bool(false)),
html: text(&payload, "html"),
detail: text(&payload, "detail"),
raw: payload,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct PageResult {
pub html: Option<String>,
pub cookies: Value,
pub proxy: Option<String>,
pub raw: Value,
}
impl PageResult {
pub(crate) fn from_value(payload: Value) -> Self {
let first = |keys: &[&str]| {
keys.iter()
.filter_map(|k| payload.get(*k).and_then(Value::as_str))
.find(|s| !s.is_empty())
.map(str::to_owned)
};
PageResult {
html: first(&["html", "page_source", "content"]),
cookies: payload.get("cookies").cloned().unwrap_or(Value::Null),
proxy: first(&["proxy", "proxy_address"]),
raw: payload,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(transparent)]
pub struct Step(Map<String, Value>);
impl Step {
fn new(action: &str) -> Self {
let mut map = Map::new();
map.insert("action".into(), action.into());
Step(map)
}
fn with(mut self, key: &str, value: impl Into<Value>) -> Self {
self.0.insert(key.into(), value.into());
self
}
pub fn wait_for(selector: impl Into<String>) -> Self {
Self::new("wait_for").with("selector", selector.into())
}
pub fn wait_for_text(text: impl Into<String>) -> Self {
Self::new("wait_for_text").with("value", text.into())
}
pub fn wait(ms: u64) -> Self {
Self::new("wait").with("value", ms)
}
pub fn click(selector: impl Into<String>) -> Self {
Self::new("click").with("selector", selector.into())
}
pub fn type_text(selector: impl Into<String>, value: impl Into<String>) -> Self {
Self::new("type")
.with("selector", selector.into())
.with("value", value.into())
}
pub fn select(selector: impl Into<String>, value: impl Into<String>) -> Self {
Self::new("select")
.with("selector", selector.into())
.with("value", value.into())
}
pub fn press_key(key: impl Into<String>) -> Self {
Self::new("press_key").with("value", key.into())
}
pub fn scroll_to_bottom() -> Self {
Self::new("scroll").with("value", "bottom")
}
pub fn scroll_by(pixels: i64) -> Self {
Self::new("scroll").with("value", pixels)
}
pub fn selector_type(self, selector_type: impl Into<String>) -> Self {
self.with("selector_type", selector_type.into())
}
pub fn timeout_ms(self, ms: u64) -> Self {
self.with("timeout_ms", ms)
}
pub fn clear(self, clear: bool) -> Self {
self.with("clear", clear)
}
pub fn from_json(value: Value) -> Option<Self> {
match value {
Value::Object(map) => Some(Step(map)),
_ => None,
}
}
}