browsr-types 0.4.0

Shared data models and schemas for Browsr browser automation flows.
Documentation
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::CommandType;

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct CommandResult {
    pub success: bool,
    pub command_type: CommandType,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub data: Option<CommandData>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub debug: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "content_type", content = "data", rename_all = "snake_case")]
pub enum CommandData {
    Markdown(String),
    Html(String),
    String(String),
    Data(Value),
}

impl CommandResult {
    pub fn content(&self) -> Value {
        match &self.data {
            Some(CommandData::Markdown(s))
            | Some(CommandData::Html(s))
            | Some(CommandData::String(s)) => Value::String(s.to_string()),
            Some(CommandData::Data(value)) => value.clone(),
            None => Value::Null,
        }
    }
}