#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
clippy::all
)]
use super::super::context::{LoadContext, SaveContext};
use super::redaction_metadata::RedactionMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HookStartScope {
Turn,
Session,
}
impl Default for HookStartScope {
fn default() -> Self {
Self::Turn
}
}
impl std::fmt::Display for HookStartScope {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Turn => write!(f, "turn"),
Self::Session => write!(f, "session"),
}
}
}
impl HookStartScope {
pub fn from_str_opt(s: &str) -> Option<Self> {
match s {
"turn" => Some(Self::Turn),
"session" => Some(Self::Session),
_ => None,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Turn => "turn",
Self::Session => "session",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct HookStartPayload {
pub hook_invocation_id: String,
pub hook_type: String,
pub scope: Option<HookStartScope>,
pub input: serde_json::Value,
pub redaction: Option<RedactionMetadata>,
}
impl HookStartPayload {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
Self {
hook_invocation_id: value
.get("hookInvocationId")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string(),
hook_type: value
.get("hookType")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string(),
scope: value
.get("scope")
.and_then(|v| v.as_str())
.and_then(|s| HookStartScope::from_str_opt(s)),
input: value
.get("input")
.cloned()
.unwrap_or(serde_json::Value::Null),
redaction: value
.get("redaction")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| RedactionMetadata::load_from_value(v, ctx)),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
if !self.hook_invocation_id.is_empty() {
result.insert(
"hookInvocationId".to_string(),
serde_json::Value::String(self.hook_invocation_id.clone()),
);
}
if !self.hook_type.is_empty() {
result.insert(
"hookType".to_string(),
serde_json::Value::String(self.hook_type.clone()),
);
}
if let Some(ref val) = self.scope {
result.insert(
"scope".to_string(),
serde_json::Value::String(val.to_string()),
);
}
if !self.input.is_null() {
result.insert("input".to_string(), self.input.clone());
}
if let Some(ref val) = self.redaction {
let nested = val.to_value(ctx);
if !nested.is_null() {
result.insert("redaction".to_string(), nested);
}
}
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
pub fn as_input_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
self.input.as_object()
}
}