Skip to main content

nexo_plugin_browser/
command.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Deserialize)]
5#[serde(tag = "command", rename_all = "snake_case")]
6pub enum BrowserCmd {
7    Navigate { url: String },
8    Click { target: String },
9    Fill { target: String, value: String },
10    Screenshot,
11    Evaluate { script: String },
12    Snapshot,
13    ScrollTo { target: String },
14}
15
16#[derive(Debug, Serialize)]
17pub struct BrowserResult {
18    pub ok: bool,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub result: Option<Value>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub data: Option<String>, // base64 PNG for screenshot
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub snapshot: Option<String>, // text snapshot with @eN refs
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub error: Option<String>,
27}
28
29impl BrowserResult {
30    pub fn ok() -> Self {
31        Self {
32            ok: true,
33            result: None,
34            data: None,
35            snapshot: None,
36            error: None,
37        }
38    }
39
40    pub fn ok_value(v: Value) -> Self {
41        Self {
42            ok: true,
43            result: Some(v),
44            data: None,
45            snapshot: None,
46            error: None,
47        }
48    }
49
50    pub fn ok_screenshot(data: String) -> Self {
51        Self {
52            ok: true,
53            result: None,
54            data: Some(data),
55            snapshot: None,
56            error: None,
57        }
58    }
59
60    pub fn ok_snapshot(snapshot: String) -> Self {
61        Self {
62            ok: true,
63            result: None,
64            data: None,
65            snapshot: Some(snapshot),
66            error: None,
67        }
68    }
69
70    pub fn err(msg: impl Into<String>) -> Self {
71        Self {
72            ok: false,
73            result: None,
74            data: None,
75            snapshot: None,
76            error: Some(msg.into()),
77        }
78    }
79}