diff --git a/Cargo.toml b/Cargo.toml index 537e3b25..48e2810f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,14 @@ name = "apitest" version = "0.1.0" authors = ["Dietmar Maurer "] + +[lib] +name = "json_schema" +path = "src/lib.rs" +version = "0.1.0" +authors = ["Dietmar Maurer "] + + [dependencies] failure = "0.1.3" phf = "0.7.23" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..4d4da188 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,143 @@ +#![feature(plugin)] +#![plugin(phf_macros)] + +//extern crate failure; + +extern crate phf; + +// Jss => JavaScript Schema + +//use failure::Error; + +pub type StaticPropertyMap = phf::Map<&'static str, Jss>; + +#[derive(Debug)] +pub struct JssBoolean { + pub description: &'static str, + pub optional: Option, + pub default: Option, +} + +#[derive(Debug)] +pub struct JssInteger { + pub description: &'static str, + pub optional: Option, + pub minimum: Option, + pub maximum: Option, + pub default: Option, +} + +#[derive(Debug)] +pub struct JssString { + pub description: &'static str, + pub optional: Option, + pub default: Option<&'static str>, + pub min_length: Option, + pub max_length: Option, +} + +#[derive(Debug)] +pub struct JssArray { + pub description: &'static str, + pub optional: Option, + pub items: &'static Jss, +} + +#[derive(Debug)] +pub struct JssObject { + pub description: &'static str, + pub optional: Option, + pub additional_properties: Option, + pub properties: &'static StaticPropertyMap, +} + +#[derive(Debug)] +pub enum Jss { + Null, + Boolean(JssBoolean), + Integer(JssInteger), + String(JssString), + Object(JssObject), + Array(JssArray), + Reference { reference: &'static Jss }, +} + +pub static DEFAULTBOOL: JssBoolean = JssBoolean { + description: "", + optional: None, + default: None, +}; + +#[macro_export] +macro_rules! Boolean { + ($($name:ident => $e:expr),*) => {{ + Jss::Boolean(JssBoolean { $($name: $e, )* ..DEFAULTBOOL}) + }} +} + +pub static DEFAULTINTEGER: JssInteger = JssInteger { + description: "", + optional: None, + default: None, + minimum: None, + maximum: None, +}; + +#[macro_export] +macro_rules! Integer { + ($($name:ident => $e:expr),*) => {{ + Jss::Integer(JssInteger { $($name: $e, )* ..DEFAULTINTEGER}) + }} +} + +pub static DEFAULTSTRING: JssString = JssString { + description: "", + optional: None, + default: None, + min_length: None, + max_length: None, +}; + +#[macro_export] +macro_rules! ApiString { + ($($name:ident => $e:expr),*) => {{ + Jss::String(JssString { $($name: $e, )* ..DEFAULTSTRING}) + }} +} + +pub static DEFAULTARRAY: JssArray = JssArray { + description: "", + optional: None, + items: &Jss::Null, // is this a reasonable default?? +}; + +#[macro_export] +macro_rules! Array { + ($($name:ident => $e:expr),*) => {{ + Jss::Array(JssArray { $($name: $e, )* ..DEFAULTARRAY}) + }} +} + +pub static EMPTYOBJECT: StaticPropertyMap = phf_map!{}; + +pub static DEFAULTOBJECT: JssObject = JssObject { + description: "", + optional: None, + additional_properties: None, + properties: &EMPTYOBJECT, // is this a reasonable default?? +}; + +#[macro_export] +macro_rules! Object { + ($($name:ident => $e:expr),*) => {{ + Jss::Object(JssObject { $($name: $e, )* ..DEFAULTOBJECT}) + }} +} + + +// Standard Option Definitions +pub static PVE_VMID: Jss = Integer!{ + description => "The (unique) ID of the VM.", + minimum => Some(1) +}; + diff --git a/src/main.rs b/src/main.rs index a9cc9af8..027f9c12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,139 +1,11 @@ #![feature(plugin)] #![plugin(phf_macros)] +extern crate phf; 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) -}; +extern crate json_schema; +use json_schema::*; static PARAMETERS1: StaticPropertyMap = phf_map! { @@ -154,7 +26,7 @@ static PARAMETERS1: StaticPropertyMap = phf_map! { description => "Test Array of simple integers.", items => &PVE_VMID }, - "myarray2" => ApiTypeDef::Array(ApiTypeDefArray { + "myarray2" => Jss::Array(JssArray { description: "Test Array of simple integers.", optional: Some(false), items: &Object!{description => "Empty Object."}, @@ -162,7 +34,7 @@ static PARAMETERS1: StaticPropertyMap = phf_map! { "myobject" => Object!{ description => "TEST Object.", properties => &phf_map!{ - "vmid" => ApiTypeDef::Reference { reference: &PVE_VMID}, + "vmid" => Jss::Reference { reference: &PVE_VMID}, "loop" => Integer!{ description => "Totally useless thing.", optional => Some(false)