proxmox-api: further cleanups

- improve docs
- move  ConstRegexPattern to const_regex.rs
This commit is contained in:
Dietmar Maurer 2019-11-23 10:42:17 +01:00
parent fa83cbde13
commit e89a52c30b
4 changed files with 45 additions and 23 deletions

View File

@ -1,10 +1,33 @@
//! Allow to build Regex within `const_fn`
//!
//! The current Regex::new() function is not `const_fn`. Unless that
//! works, we use a macro to generate something we can use inside
//! `const_fn`.
use std::fmt;
/// Helper to represent const regular expressions
///
/// The current Regex::new() function is not `const_fn`. Unless that
/// works, we use `ConstRegexPattern` to represent static regular
/// expressions. Please use the `const_regex` macro to generate
/// instances of this type (uses lazy_static).
pub struct ConstRegexPattern {
/// This is only used for documentation and debugging
pub regex_string: &'static str,
/// This function return the the actual Regex
pub regex_obj: fn() -> &'static regex::Regex,
}
impl fmt::Debug for ConstRegexPattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.regex_string)
}
}
/// Macro to generate a ConstRegexPattern
///
/// ```ignore
/// const_regex!{
/// FILE_EXTENSION_REGEX = r".*\.([a-zA-Z]+)$";
/// pub SHA256_HEX_REGEX = r"^[a-f0-9]{64}$";
/// }
/// ```
#[macro_export]
macro_rules! const_regex {
() => {};

View File

@ -1,3 +1,4 @@
//! Module to generate and format API Documenation
use failure::*;
use std::io::Write;

View File

@ -9,6 +9,7 @@ use hyper::http::request::Parts;
use hyper::{Body, Response};
use serde_json::Value;
#[doc(hidden)]
pub mod const_regex;
#[doc(hidden)]
pub mod error;
@ -19,6 +20,9 @@ pub mod router;
pub mod rpc_environment;
pub mod schema;
#[doc(inline)]
pub use const_regex::ConstRegexPattern;
#[doc(inline)]
pub use rpc_environment::{RpcEnvironment, RpcEnvironmentType};

View File

@ -1,23 +1,31 @@
//! Data types to decscribe data types.
//!
//! This is loosly based on JSON Schema, but uses static RUST data
//! types. This way we can build completely static API
//! definitions included with the programs read-only text segment.
use std::fmt;
use failure::*;
use serde_json::{json, Value};
use url::form_urlencoded;
#[derive(Default, Debug, Fail)]
pub struct ParameterError {
error_list: Vec<Error>,
}
use crate::const_regex::ConstRegexPattern;
/// Error type for schema validation
///
/// The validation functions may produce several error message,
/// i.e. when validation objects, it can produce one message for each
/// erroneous object property.
#[derive(Default, Debug, Fail)]
pub struct ParameterError {
error_list: Vec<Error>,
}
// fixme: record parameter names, to make it usefull to display errord
// on HTML forms.
impl ParameterError {
pub fn new() -> Self {
Self {
error_list: Vec::new(),
@ -142,20 +150,6 @@ impl IntegerSchema {
}
}
/// Helper to represent const regular expressions
///
/// This is mostly a workaround, unless we can create const_fn Regex.
pub struct ConstRegexPattern {
pub regex_string: &'static str,
pub regex_obj: fn() -> &'static regex::Regex,
}
impl std::fmt::Debug for ConstRegexPattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.regex_string)
}
}
#[derive(Debug)]
pub struct StringSchema {
pub description: &'static str,