commit d8d978ebc4f51b30f85bcc66eb9135f01b593ebb Author: Dietmar Maurer Date: Tue Oct 30 10:04:30 2018 +0100 initial version diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..537e3b25 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "apitest" +version = "0.1.0" +authors = ["Dietmar Maurer "] + +[dependencies] +failure = "0.1.3" +phf = "0.7.23" +phf_macros = "0.7.23" +derive-new = "0.5.5" + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 00000000..a9cc9af8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,183 @@ +#![feature(plugin)] +#![plugin(phf_macros)] + +extern crate failure; + +extern crate phf; + + +use failure::Error; + +type StaticPropertyMap = phf::Map<&'static str, ApiTypeDef>; + +#[derive(Debug)] +struct ApiTypeDefBoolean { + description: &'static str, + optional: Option, + default: Option, +} + +#[derive(Debug)] +struct ApiTypeDefInteger { + description: &'static str, + optional: Option, + minimum: Option, + maximum: Option, + default: Option, +} + +#[derive(Debug)] +struct ApiTypeDefString { + description: &'static str, + optional: Option, + default: Option<&'static str>, + min_length: Option, + max_length: Option, +} + +#[derive(Debug)] +struct ApiTypeDefArray { + description: &'static str, + optional: Option, + items: &'static ApiTypeDef, +} + +#[derive(Debug)] +struct ApiTypeDefObject { + description: &'static str, + optional: Option, + additional_properties: Option, + properties: &'static StaticPropertyMap, +} + +#[derive(Debug)] +enum ApiTypeDef { + Null, + Boolean(ApiTypeDefBoolean), + Integer(ApiTypeDefInteger), + String(ApiTypeDefString), + Object(ApiTypeDefObject), + Array(ApiTypeDefArray), + Reference { reference: &'static ApiTypeDef }, +} + +static DEFAULTBOOL: ApiTypeDefBoolean = ApiTypeDefBoolean { + description: "", + optional: None, + default: None, +}; + +macro_rules! Boolean { + ($($name:ident => $e:expr),*) => {{ + ApiTypeDef::Boolean(ApiTypeDefBoolean { $($name: $e, )* ..DEFAULTBOOL}) + }} +} + +static DEFAULTINTEGER: ApiTypeDefInteger = ApiTypeDefInteger { + description: "", + optional: None, + default: None, + minimum: None, + maximum: None, +}; + +macro_rules! Integer { + ($($name:ident => $e:expr),*) => {{ + ApiTypeDef::Integer(ApiTypeDefInteger { $($name: $e, )* ..DEFAULTINTEGER}) + }} +} + +static DEFAULTSTRING: ApiTypeDefString = ApiTypeDefString { + description: "", + optional: None, + default: None, + min_length: None, + max_length: None, +}; + +macro_rules! ApiString { + ($($name:ident => $e:expr),*) => {{ + ApiTypeDef::String(ApiTypeDefString { $($name: $e, )* ..DEFAULTSTRING}) + }} +} + +static DEFAULTARRAY: ApiTypeDefArray = ApiTypeDefArray { + description: "", + optional: None, + items: &ApiTypeDef::Null, // is this a reasonable default?? +}; + +macro_rules! Array { + ($($name:ident => $e:expr),*) => {{ + ApiTypeDef::Array(ApiTypeDefArray { $($name: $e, )* ..DEFAULTARRAY}) + }} +} + +static EMPTYOBJECT: StaticPropertyMap = phf_map!{}; + +static DEFAULTOBJECT: ApiTypeDefObject = ApiTypeDefObject { + description: "", + optional: None, + additional_properties: None, + properties: &EMPTYOBJECT, // is this a reasonable default?? +}; + +macro_rules! Object { + ($($name:ident => $e:expr),*) => {{ + ApiTypeDef::Object(ApiTypeDefObject { $($name: $e, )* ..DEFAULTOBJECT}) + }} +} + + +// Standard Option Definitions +static PVE_VMID: ApiTypeDef = Integer!{ + description => "The (unique) ID of the VM.", + minimum => Some(1) +}; + + +static PARAMETERS1: StaticPropertyMap = phf_map! { + "force" => Boolean!{ + description => "Test for boolean options." + }, + "text1" => ApiString!{ + description => "A simple text string.", + min_length => Some(10), + max_length => Some(30) + }, + "count" => Integer!{ + description => "A counter for everything.", + minimum => Some(0), + maximum => Some(10) + }, + "myarray1" => Array!{ + description => "Test Array of simple integers.", + items => &PVE_VMID + }, + "myarray2" => ApiTypeDef::Array(ApiTypeDefArray { + description: "Test Array of simple integers.", + optional: Some(false), + items: &Object!{description => "Empty Object."}, + }), + "myobject" => Object!{ + description => "TEST Object.", + properties => &phf_map!{ + "vmid" => ApiTypeDef::Reference { reference: &PVE_VMID}, + "loop" => Integer!{ + description => "Totally useless thing.", + optional => Some(false) + } + } + }, + "emptyobject" => Object!{description => "Empty Object."}, +}; + + +fn main() { + println!("Fast Static Type Definitions 1"); + + for (k, v) in PARAMETERS1.entries() { + println!("Parameter: {} Value: {:?}", k, v); + } + +}