Print collection 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 # Behavior - JSON: uses `println!()` (includes trailing newline) - YAML: uses `print!()` (no trailing newline, `serde_yml`
(
format: impl AsRef<str>,
items: &[T],
to_json: F,
)
| 52 | /// } |
| 53 | /// ``` |
| 54 | pub fn print_output_collection<T, F>( |
| 55 | format: impl AsRef<str>, |
| 56 | items: &[T], |
| 57 | to_json: F, |
| 58 | ) -> Result<bool> |
| 59 | where |
| 60 | F: Fn(&T) -> serde_json::Value, |
| 61 | { |
| 62 | match format.as_ref() { |
| 63 | "json" => { |
| 64 | let values: Vec<_> = items.iter().map(to_json).collect(); |
| 65 | println!( |
| 66 | "{}", |
| 67 | serde_json::to_string_pretty(&values).into_diagnostic()? |
| 68 | ); |
| 69 | Ok(true) |
| 70 | } |
| 71 | "yaml" => { |
| 72 | let values: Vec<_> = items.iter().map(to_json).collect(); |
| 73 | print!("{}", serde_yml::to_string(&values).into_diagnostic()?); |
| 74 | Ok(true) |
| 75 | } |
| 76 | "table" => Ok(false), |
| 77 | _ => Err(miette::miette!( |
| 78 | "unsupported output format: {}", |
| 79 | format.as_ref() |
| 80 | )), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// Print single item output in specified format (json/yaml/table). |
| 85 | /// |
no outgoing calls