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

Function print_output_single

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

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)

Source from the content-addressed store, hash-verified

95/// }
96/// ```
97pub fn print_output_single<T, F>(format: impl AsRef<str>, item: &T, to_json: F) -> Result<bool>
98where
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///

Callers 2

Calls

no outgoing calls

Tested by 2