use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionResult {
pub status: String,
pub message: String,
}
pub fn parse_action_result(value: serde_json::Value) -> Result<ActionResult, super::TailFinError> {
let s = value
.as_str()
.unwrap_or(r#"{"status":"failed","message":"no response"}"#);
Ok(serde_json::from_str(s).unwrap_or(ActionResult {
status: "failed".into(),
message: s.to_string(),
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialize_produces_expected_json() {
let result = ActionResult {
status: "ok".into(),
message: "done".into(),
};
let json = serde_json::to_value(&result).unwrap();
assert_eq!(json["status"], "ok");
assert_eq!(json["message"], "done");
let obj = json.as_object().unwrap();
assert_eq!(obj.len(), 2);
}
#[test]
fn deserialize_from_json_string() {
let json = r#"{"status":"error","message":"not found"}"#;
let result: ActionResult = serde_json::from_str(json).unwrap();
assert_eq!(result.status, "error");
assert_eq!(result.message, "not found");
}
#[test]
fn roundtrip_serialize_deserialize() {
let original = ActionResult {
status: "success".into(),
message: "created 3 items".into(),
};
let serialized = serde_json::to_string(&original).unwrap();
let deserialized: ActionResult = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.status, original.status);
assert_eq!(deserialized.message, original.message);
}
#[test]
fn json_format_matches_expected_shape() {
let result = ActionResult {
status: "ok".into(),
message: "hi".into(),
};
let json_str = serde_json::to_string(&result).unwrap();
assert_eq!(json_str, r#"{"status":"ok","message":"hi"}"#);
}
}