From 034bb9cdda6c273aa21074f4248cd8fb36fa9093 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 26 Jul 2024 09:13:28 +0200 Subject: [PATCH] router: cli: add OutputFormat enum api type Signed-off-by: Wolfgang Bumiller --- proxmox-router/Cargo.toml | 3 ++- proxmox-router/src/cli/command.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml index f1d7fc87..17fdb0c5 100644 --- a/proxmox-router/Cargo.toml +++ b/proxmox-router/Cargo.toml @@ -22,8 +22,9 @@ http = { workspace = true, optional = true } hyper = { workspace = true, features = [ "full" ], optional = true } nix.workspace = true percent-encoding.workspace = true +serde = { workspace = true, features = [ "derive" ] } serde_json.workspace = true -serde.workspace = true +serde_plain.workspace = true unicode-width ="0.1.8" regex = { workspace = true, optional = true } diff --git a/proxmox-router/src/cli/command.rs b/proxmox-router/src/cli/command.rs index c1a26a6b..e7655dac 100644 --- a/proxmox-router/src/cli/command.rs +++ b/proxmox-router/src/cli/command.rs @@ -1,8 +1,10 @@ -use anyhow::{bail, format_err, Error}; -use serde_json::Value; use std::cell::RefCell; use std::sync::Arc; +use anyhow::{bail, format_err, Error}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + use proxmox_schema::format::DocumentationFormat; use proxmox_schema::*; @@ -14,6 +16,25 @@ use super::{ }; use crate::{ApiFuture, ApiHandler, ApiMethod, RpcEnvironment}; +/// Command line output format. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum OutputFormat { + /// Command specific plain text output. + #[default] + Text, + /// Single line JSON output. + Json, + /// Prettified JSON output. + JsonPretty, +} +serde_plain::derive_display_from_serialize!(OutputFormat); +serde_plain::derive_fromstr_from_deserialize!(OutputFormat); + +impl ApiType for OutputFormat { + const API_SCHEMA: Schema = OUTPUT_FORMAT; +} + /// Schema definition for ``--output-format`` parameter. /// /// - ``text``: command specific text format.