move code into lib.rs

This commit is contained in:
Dietmar Maurer 2018-10-30 10:40:05 +01:00
parent d8d978ebc4
commit d6a4ba7192
3 changed files with 156 additions and 133 deletions

View File

@ -3,6 +3,14 @@ name = "apitest"
version = "0.1.0" version = "0.1.0"
authors = ["Dietmar Maurer <dietmar@proxmox.com>"] authors = ["Dietmar Maurer <dietmar@proxmox.com>"]
[lib]
name = "json_schema"
path = "src/lib.rs"
version = "0.1.0"
authors = ["Dietmar Maurer <dietmar@proxmox.com>"]
[dependencies] [dependencies]
failure = "0.1.3" failure = "0.1.3"
phf = "0.7.23" phf = "0.7.23"

143
src/lib.rs Normal file
View File

@ -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<bool>,
pub default: Option<bool>,
}
#[derive(Debug)]
pub struct JssInteger {
pub description: &'static str,
pub optional: Option<bool>,
pub minimum: Option<usize>,
pub maximum: Option<usize>,
pub default: Option<usize>,
}
#[derive(Debug)]
pub struct JssString {
pub description: &'static str,
pub optional: Option<bool>,
pub default: Option<&'static str>,
pub min_length: Option<usize>,
pub max_length: Option<usize>,
}
#[derive(Debug)]
pub struct JssArray {
pub description: &'static str,
pub optional: Option<bool>,
pub items: &'static Jss,
}
#[derive(Debug)]
pub struct JssObject {
pub description: &'static str,
pub optional: Option<bool>,
pub additional_properties: Option<bool>,
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)
};

View File

@ -1,139 +1,11 @@
#![feature(plugin)] #![feature(plugin)]
#![plugin(phf_macros)] #![plugin(phf_macros)]
extern crate phf;
extern crate failure; extern crate failure;
extern crate phf; extern crate json_schema;
use json_schema::*;
use failure::Error;
type StaticPropertyMap = phf::Map<&'static str, ApiTypeDef>;
#[derive(Debug)]
struct ApiTypeDefBoolean {
description: &'static str,
optional: Option<bool>,
default: Option<bool>,
}
#[derive(Debug)]
struct ApiTypeDefInteger {
description: &'static str,
optional: Option<bool>,
minimum: Option<usize>,
maximum: Option<usize>,
default: Option<usize>,
}
#[derive(Debug)]
struct ApiTypeDefString {
description: &'static str,
optional: Option<bool>,
default: Option<&'static str>,
min_length: Option<usize>,
max_length: Option<usize>,
}
#[derive(Debug)]
struct ApiTypeDefArray {
description: &'static str,
optional: Option<bool>,
items: &'static ApiTypeDef,
}
#[derive(Debug)]
struct ApiTypeDefObject {
description: &'static str,
optional: Option<bool>,
additional_properties: Option<bool>,
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! { static PARAMETERS1: StaticPropertyMap = phf_map! {
@ -154,7 +26,7 @@ static PARAMETERS1: StaticPropertyMap = phf_map! {
description => "Test Array of simple integers.", description => "Test Array of simple integers.",
items => &PVE_VMID items => &PVE_VMID
}, },
"myarray2" => ApiTypeDef::Array(ApiTypeDefArray { "myarray2" => Jss::Array(JssArray {
description: "Test Array of simple integers.", description: "Test Array of simple integers.",
optional: Some(false), optional: Some(false),
items: &Object!{description => "Empty Object."}, items: &Object!{description => "Empty Object."},
@ -162,7 +34,7 @@ static PARAMETERS1: StaticPropertyMap = phf_map! {
"myobject" => Object!{ "myobject" => Object!{
description => "TEST Object.", description => "TEST Object.",
properties => &phf_map!{ properties => &phf_map!{
"vmid" => ApiTypeDef::Reference { reference: &PVE_VMID}, "vmid" => Jss::Reference { reference: &PVE_VMID},
"loop" => Integer!{ "loop" => Integer!{
description => "Totally useless thing.", description => "Totally useless thing.",
optional => Some(false) optional => Some(false)