use serde_json::{Value, json};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
pub id: String,
pub name: String,
pub input: Value,
pub role: String,
pub agent_id: String,
}
impl Request {
pub fn parse(raw: &str) -> Option<Request> {
let value: Value = serde_json::from_str(raw).ok()?;
let text = |key: &str| value.get(key).and_then(Value::as_str).map(str::to_owned);
Some(Request {
id: text("id")?,
name: text("name")?,
input: value.get("input")?.clone(),
role: text("role")?,
agent_id: text("agent_id")?,
})
}
pub fn field(&self, key: &str) -> String {
self.input
.get(key)
.and_then(Value::as_str)
.unwrap_or_default()
.to_owned()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Verdict {
Pass,
Refuse(String),
Hold(String),
}
impl Verdict {
pub fn json(&self) -> String {
match self {
Verdict::Pass => json!({ "verdict": "pass" }).to_string(),
Verdict::Refuse(reason) => json!({ "verdict": "refuse", "reason": reason }).to_string(),
Verdict::Hold(reason) => json!({ "verdict": "hold", "reason": reason }).to_string(),
}
}
}