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)]
#[non_exhaustive]
pub struct XhrCall {
pub url: String,
pub method: String,
pub status: u16,
pub content_type: Option<String>,
pub body: Option<String>,
pub body_omitted: Option<String>,
}
impl XhrCall {
pub(crate) fn from_value(item: &Value) -> Self {
let text = |key: &str| item.get(key).and_then(Value::as_str).map(str::to_owned);
XhrCall {
url: text("url").unwrap_or_default(),
method: text("method").unwrap_or_else(|| "GET".to_owned()),
status: item
.get("status")
.and_then(Value::as_u64)
.and_then(|s| u16::try_from(s).ok())
.unwrap_or(0),
content_type: text("content_type"),
body: text("body"),
body_omitted: text("body_omitted"),
}
}
pub fn json(&self) -> serde_json::Result<Value> {
serde_json::from_str(self.body.as_deref().unwrap_or(""))
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CapturedPage {
pub html: Option<String>,
pub xhr: Vec<XhrCall>,
pub xhr_truncated: bool,
pub proxy: Option<String>,
pub raw: Value,
}
impl CapturedPage {
pub(crate) fn from_value(payload: Value) -> Self {
CapturedPage {
html: payload
.get("html")
.and_then(Value::as_str)
.map(str::to_owned),
xhr: xhr_calls(payload.get("xhr")).unwrap_or_default(),
xhr_truncated: payload.get("xhr_truncated") == Some(&Value::Bool(true)),
proxy: payload
.get("proxy")
.and_then(Value::as_str)
.map(str::to_owned),
raw: payload,
}
}
}
pub(crate) fn xhr_calls(value: Option<&Value>) -> Option<Vec<XhrCall>> {
value
.and_then(Value::as_array)
.map(|items| items.iter().map(XhrCall::from_value).collect())
}
#[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,
}
}
}