Skip to main content

command_to_json_with_fields

Function command_to_json_with_fields 

Source
pub fn command_to_json_with_fields(
    cmd: &Command,
    fields: &[&str],
) -> Result<String, QueryError>
Expand description

Serialize a single Command to a pretty-printed JSON string, filtering the output to only include the requested top-level fields.

Behaves like Registry::to_json_with_fields but for a single command rather than the whole registry.

If fields is empty the method serializes the full command (equivalent to serde_json::to_string_pretty(cmd)).

§Errors

Returns QueryError::Serialization if serde_json fails.

§Examples

let cmd = Command::builder("deploy")
    .summary("Deploy the app")
    .build()
    .unwrap();

let json = command_to_json_with_fields(&cmd, &["canonical", "summary"]).unwrap();
let v: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(v["canonical"], "deploy");
assert_eq!(v["summary"], "Deploy the app");
assert!(v.get("examples").is_none());