MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / print_output_direct_to_writer

Function print_output_direct_to_writer

crates/openshell-cli/src/output.rs:269–297  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

267/// - `Ok(false)` if format is "table", caller should continue to table rendering
268/// - `Err` for unsupported formats, conversion errors, or write errors
269pub 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>
275where
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)]
300mod tests {

Calls

no outgoing calls

Tested by 1