Print pre-formatted output to a custom writer in specified format (json/yaml/table). Writer variant for commands that need custom output destinations and use pre-formatted converters returning `Result `. # 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 format
(
format: impl AsRef<str>,
writer: &mut W,
json_fn: J,
yaml_fn: Y,
)
| 267 | /// - `Ok(false)` if format is "table", caller should continue to table rendering |
| 268 | /// - `Err` for unsupported formats, conversion errors, or write errors |
| 269 | pub fn print_output_direct_to_writer<W, J, Y>( |
| 270 | format: impl AsRef<str>, |
| 271 | writer: &mut W, |
| 272 | json_fn: J, |
| 273 | yaml_fn: Y, |
| 274 | ) -> Result<bool> |
| 275 | where |
| 276 | W: Write, |
| 277 | J: FnOnce() -> Result<String>, |
| 278 | Y: FnOnce() -> Result<String>, |
| 279 | { |
| 280 | match format.as_ref() { |
| 281 | "json" => { |
| 282 | let json = json_fn()?; |
| 283 | writeln!(writer, "{json}").into_diagnostic()?; |
| 284 | Ok(true) |
| 285 | } |
| 286 | "yaml" => { |
| 287 | let yaml = yaml_fn()?; |
| 288 | write!(writer, "{yaml}").into_diagnostic()?; |
| 289 | Ok(true) |
| 290 | } |
| 291 | "table" => Ok(false), |
| 292 | _ => Err(miette::miette!( |
| 293 | "unsupported output format: {}", |
| 294 | format.as_ref() |
| 295 | )), |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | #[cfg(test)] |
| 300 | mod tests { |
no outgoing calls