Print single item output in specified format (json/yaml/table). # Returns - `Ok(true)` if format was handled (json/yaml), caller should return - `Ok(false)` if format is "table", caller should continue to table rendering - `Err` for unsupported formats or serialization errors # Example ```ignore if print_output_single(output, &sandbox, sandbox_to_json)? { return Ok(()); } ```
(format: impl AsRef<str>, item: &T, to_json: F)
| 95 | /// } |
| 96 | /// ``` |
| 97 | pub fn print_output_single<T, F>(format: impl AsRef<str>, item: &T, to_json: F) -> Result<bool> |
| 98 | where |
| 99 | F: Fn(&T) -> serde_json::Value, |
| 100 | { |
| 101 | match format.as_ref() { |
| 102 | "json" => { |
| 103 | let value = to_json(item); |
| 104 | println!( |
| 105 | "{}", |
| 106 | serde_json::to_string_pretty(&value).into_diagnostic()? |
| 107 | ); |
| 108 | Ok(true) |
| 109 | } |
| 110 | "yaml" => { |
| 111 | let value = to_json(item); |
| 112 | print!("{}", serde_yml::to_string(&value).into_diagnostic()?); |
| 113 | Ok(true) |
| 114 | } |
| 115 | "table" => Ok(false), |
| 116 | _ => Err(miette::miette!( |
| 117 | "unsupported output format: {}", |
| 118 | format.as_ref() |
| 119 | )), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Print pre-formatted output in specified format (json/yaml/table). |
| 124 | /// |
no outgoing calls