src/cli/command.rs: move doc generator code to src/api_schema/format.rs

This commit is contained in:
Dietmar Maurer 2019-06-04 08:24:50 +02:00
parent ef39bf95b3
commit 339ddfcbfa
5 changed files with 117 additions and 107 deletions

View File

@ -20,7 +20,7 @@ use crate::server::WorkerTask;
mod pxar;
mod upload;
mod backup;
pub mod backup;
fn group_backups(backup_list: Vec<BackupInfo>) -> HashMap<String, Vec<BackupInfo>> {

View File

@ -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(

View File

@ -17,3 +17,4 @@ pub mod registry;
#[macro_use]
pub mod router;
pub mod config;
pub mod format;

113
src/api_schema/format.rs Normal file
View File

@ -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("<null>"), // should not happen
Schema::String(_) => String::from("<string>"),
Schema::Boolean(_) => String::from("<boolean>"),
Schema::Integer(integer_schema) => {
match (integer_schema.minimum, integer_schema.maximum) {
(Some(min), Some(max)) => format!("<integer> ({} - {})", min, max),
(Some(min), None) => format!("<integer> ({} - N)", min),
(None, Some(max)) => format!("<integer> (-N - {})", max),
_ => String::from("<integer>"),
}
},
Schema::Object(_) => String::from("<object>"),
Schema::Array(_) => String::from("<array>"),
};
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
}
}

View File

@ -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("<null>"), // should not happen
Schema::String(_) => String::from("<string>"),
Schema::Boolean(_) => String::from("<boolean>"),
Schema::Integer(integer_schema) => {
match (integer_schema.minimum, integer_schema.maximum) {
(Some(min), Some(max)) => format!("<integer> ({} - {})", min, max),
(Some(min), None) => format!("<integer> ({} - N)", min),
(None, Some(max)) => format!("<integer> (-N - {})", max),
_ => String::from("<integer>"),
}
},
Schema::Object(_) => String::from("<object>"),
Schema::Array(_) => String::from("<array>"),
};
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,