From 339ddfcbfa0b3f88a49f51e750a3160aa47649fd Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 4 Jun 2019 08:24:50 +0200 Subject: [PATCH] src/cli/command.rs: move doc generator code to src/api_schema/format.rs --- src/api2/admin/datastore.rs | 2 +- src/api2/admin/datastore/backup.rs | 2 +- src/api_schema.rs | 1 + src/api_schema/format.rs | 113 +++++++++++++++++++++++++++++ src/cli/command.rs | 106 +-------------------------- 5 files changed, 117 insertions(+), 107 deletions(-) create mode 100644 src/api_schema/format.rs diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 7471a847..f55b1974 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -20,7 +20,7 @@ use crate::server::WorkerTask; mod pxar; mod upload; -mod backup; +pub mod backup; fn group_backups(backup_list: Vec) -> HashMap> { diff --git a/src/api2/admin/datastore/backup.rs b/src/api2/admin/datastore/backup.rs index 4e5e701a..a9380457 100644 --- a/src/api2/admin/datastore/backup.rs +++ b/src/api2/admin/datastore/backup.rs @@ -147,7 +147,7 @@ fn upgrade_to_backup_protocol( Ok(Box::new(futures::future::ok(response))) } -fn backup_api() -> Router { +pub fn backup_api() -> Router { let router = Router::new() .subdir( diff --git a/src/api_schema.rs b/src/api_schema.rs index 27156df3..c41af58b 100644 --- a/src/api_schema.rs +++ b/src/api_schema.rs @@ -17,3 +17,4 @@ pub mod registry; #[macro_use] pub mod router; pub mod config; +pub mod format; diff --git a/src/api_schema/format.rs b/src/api_schema/format.rs new file mode 100644 index 00000000..b475cb4a --- /dev/null +++ b/src/api_schema/format.rs @@ -0,0 +1,113 @@ +use crate::api_schema::*; + +#[derive(Copy, Clone)] +pub enum ParameterDisplayStyle { + Config, + //SonfigSub, + Arg, + Fixed, +} + +/// CLI usage information format +#[derive(Copy, Clone, PartialEq)] +pub enum DocumentationFormat { + /// text, command line only (one line) + Short, + /// text, list all options + Long, + /// text, include description + Full, + /// like full, but in reStructuredText format + ReST, +} + +pub fn get_schema_type_text(schema: &Schema, _style: ParameterDisplayStyle) -> String { + + let type_text = match schema { + Schema::Null => String::from(""), // should not happen + Schema::String(_) => String::from(""), + Schema::Boolean(_) => String::from(""), + Schema::Integer(integer_schema) => { + match (integer_schema.minimum, integer_schema.maximum) { + (Some(min), Some(max)) => format!(" ({} - {})", min, max), + (Some(min), None) => format!(" ({} - N)", min), + (None, Some(max)) => format!(" (-N - {})", max), + _ => String::from(""), + } + }, + Schema::Object(_) => String::from(""), + Schema::Array(_) => String::from(""), + }; + + type_text +} + +pub fn get_property_description( + name: &str, + schema: &Schema, + style: ParameterDisplayStyle, + format: DocumentationFormat, +) -> String { + + let type_text = get_schema_type_text(schema, style); + + let (descr, default) = match schema { + Schema::Null => ("null", None), + Schema::String(ref schema) => (schema.description, schema.default.map(|v| v.to_owned())), + Schema::Boolean(ref schema) => (schema.description, schema.default.map(|v| v.to_string())), + Schema::Integer(ref schema) => (schema.description, schema.default.map(|v| v.to_string())), + Schema::Object(ref schema) => (schema.description, None), + Schema::Array(ref schema) => (schema.description, None), + }; + + let default_text = match default { + Some(text) => format!(" (default={})", text), + None => String::new(), + }; + + if format == DocumentationFormat::ReST { + + let mut text = match style { + ParameterDisplayStyle::Config => { + format!(":``{} {}{}``: ", name, type_text, default_text) + } + ParameterDisplayStyle::Arg => { + format!(":``--{} {}{}``: ", name, type_text, default_text) + } + ParameterDisplayStyle::Fixed => { + format!(":``<{}> {}{}``: ", name, type_text, default_text) + } + }; + + text.push_str(descr); + text.push('\n'); + text.push('\n'); + + text + + } else { + + let display_name = match style { + ParameterDisplayStyle::Config => { + format!("{}:", name) + } + ParameterDisplayStyle::Arg => { + format!("--{}", name) + } + ParameterDisplayStyle::Fixed => { + format!("<{}>", name) + } + }; + + // fixme: wrap text + let mut text = format!(" {:-10} {}{}", display_name, type_text, default_text); + let indent = " "; + text.push('\n'); + text.push_str(indent); + text.push_str(descr); + text.push('\n'); + text.push('\n'); + + text + } +} diff --git a/src/cli/command.rs b/src/cli/command.rs index 57c8fa8e..7c472b3a 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -4,116 +4,12 @@ use std::collections::HashSet; use crate::api_schema::*; use crate::api_schema::router::*; +use crate::api_schema::format::*; //use crate::api_schema::config::*; use super::environment::CliEnvironment; use super::getopts; -#[derive(Copy, Clone)] -enum ParameterDisplayStyle { - //Config, - //SonfigSub, - Arg, - Fixed, -} - -/// CLI usage information format -#[derive(Copy, Clone, PartialEq)] -enum DocumentationFormat { - /// text, command line only (one line) - Short, - /// text, list all options - Long, - /// text, include description - Full, - /// like full, but in reStructuredText format - ReST, -} - -fn get_schema_type_text(schema: &Schema, _style: ParameterDisplayStyle) -> String { - - let type_text = match schema { - Schema::Null => String::from(""), // should not happen - Schema::String(_) => String::from(""), - Schema::Boolean(_) => String::from(""), - Schema::Integer(integer_schema) => { - match (integer_schema.minimum, integer_schema.maximum) { - (Some(min), Some(max)) => format!(" ({} - {})", min, max), - (Some(min), None) => format!(" ({} - N)", min), - (None, Some(max)) => format!(" (-N - {})", max), - _ => String::from(""), - } - }, - Schema::Object(_) => String::from(""), - Schema::Array(_) => String::from(""), - }; - - type_text -} - -fn get_property_description( - name: &str, - schema: &Schema, - style: ParameterDisplayStyle, - format: DocumentationFormat, -) -> String { - - let type_text = get_schema_type_text(schema, style); - - let (descr, default) = match schema { - Schema::Null => ("null", None), - Schema::String(ref schema) => (schema.description, schema.default.map(|v| v.to_owned())), - Schema::Boolean(ref schema) => (schema.description, schema.default.map(|v| v.to_string())), - Schema::Integer(ref schema) => (schema.description, schema.default.map(|v| v.to_string())), - Schema::Object(ref schema) => (schema.description, None), - Schema::Array(ref schema) => (schema.description, None), - }; - - let default_text = match default { - Some(text) => format!(" (default={})", text), - None => String::new(), - }; - - if format == DocumentationFormat::ReST { - - let mut text = match style { - ParameterDisplayStyle::Arg => { - format!(":``--{} {}{}``: ", name, type_text, default_text) - } - ParameterDisplayStyle::Fixed => { - format!(":``<{}> {}{}``: ", name, type_text, default_text) - } - }; - - text.push_str(descr); - text.push('\n'); - text.push('\n'); - - text - - } else { - - let display_name = match style { - ParameterDisplayStyle::Arg => { - format!("--{}", name) - } - ParameterDisplayStyle::Fixed => { - format!("<{}>", name) - } - }; - - // fixme: wrap text - let mut text = format!(" {:-10} {}{}", display_name, type_text, default_text); - let indent = " "; - text.push('\n'); - text.push_str(indent); - text.push_str(descr); - text.push('\n'); - text.push('\n'); - - text - } -} fn generate_usage_str( prefix: &str,