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

Function print_output_single_to_writer

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

Print single item output to a custom writer in specified format (json/yaml/table). Writer variant for commands that need custom output destinations. # 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, serialization errors, or write errors

(
    format: impl AsRef<str>,
    writer: &mut W,
    item: &T,
    to_json: F,
)

Source from the content-addressed store, hash-verified

219/// - `Ok(false)` if format is "table", caller should continue to table rendering
220/// - `Err` for unsupported formats, serialization errors, or write errors
221pub fn print_output_single_to_writer<W, T, F>(
222 format: impl AsRef<str>,
223 writer: &mut W,
224 item: &T,
225 to_json: F,
226) -> Result<bool>
227where
228 W: Write,
229 F: Fn(&T) -> serde_json::Value,
230{
231 match format.as_ref() {
232 "json" => {
233 let value = to_json(item);
234 writeln!(
235 writer,
236 "{}",
237 serde_json::to_string_pretty(&value).into_diagnostic()?
238 )
239 .into_diagnostic()?;
240 Ok(true)
241 }
242 "yaml" => {
243 let value = to_json(item);
244 write!(
245 writer,
246 "{}",
247 serde_yml::to_string(&value).into_diagnostic()?
248 )
249 .into_diagnostic()?;
250 Ok(true)
251 }
252 "table" => Ok(false),
253 _ => Err(miette::miette!(
254 "unsupported output format: {}",
255 format.as_ref()
256 )),
257 }
258}
259
260/// Print pre-formatted output to a custom writer in specified format (json/yaml/table).
261///

Calls

no outgoing calls

Tested by 1