plan_issue_cli/output/
json.rs1use serde::Serialize;
2use serde_json::Value;
3
4#[derive(Debug, Serialize)]
5struct JsonSuccessEnvelope<'a> {
6 schema_version: &'a str,
7 command: &'a str,
8 status: &'a str,
9 payload: &'a Value,
10}
11
12#[derive(Debug, Serialize)]
13struct JsonErrorEnvelope<'a> {
14 schema_version: &'a str,
15 command: &'a str,
16 status: &'a str,
17 error: JsonError<'a>,
18}
19
20#[derive(Debug, Serialize)]
21struct JsonError<'a> {
22 code: &'a str,
23 message: &'a str,
24}
25
26pub fn print_success(schema_version: &str, command: &str, payload: &Value) -> Result<(), String> {
27 let envelope = JsonSuccessEnvelope {
28 schema_version,
29 command,
30 status: "ok",
31 payload,
32 };
33
34 let rendered = serde_json::to_string(&envelope)
35 .map_err(|err| format!("failed to serialize JSON output: {err}"))?;
36 println!("{rendered}");
37 Ok(())
38}
39
40pub fn print_error(
41 schema_version: &str,
42 command: &str,
43 code: &str,
44 message: &str,
45) -> Result<(), String> {
46 let envelope = JsonErrorEnvelope {
47 schema_version,
48 command,
49 status: "error",
50 error: JsonError { code, message },
51 };
52
53 let rendered = serde_json::to_string(&envelope)
54 .map_err(|err| format!("failed to serialize JSON error output: {err}"))?;
55 println!("{rendered}");
56 Ok(())
57}