split code into several files

This commit is contained in:
Dietmar Maurer 2018-10-31 10:42:14 +01:00
parent 22f0adf26a
commit 886e5ce8f7
5 changed files with 212 additions and 157 deletions

View File

@ -5,7 +5,7 @@ authors = ["Dietmar Maurer <dietmar@proxmox.com>"]
[lib] [lib]
name = "json_schema" name = "apitest"
path = "src/lib.rs" path = "src/lib.rs"
version = "0.1.0" version = "0.1.0"
authors = ["Dietmar Maurer <dietmar@proxmox.com>"] authors = ["Dietmar Maurer <dietmar@proxmox.com>"]
@ -16,6 +16,7 @@ failure = "0.1.3"
phf = "0.7.23" phf = "0.7.23"
phf_macros = "0.7.23" phf_macros = "0.7.23"
derive-new = "0.5.5" derive-new = "0.5.5"
serde = "1.0.80" # A generic serialization/deserialization framework serde = "1.0.80"
serde_json = "1.0.32" # A JSON serialization file format serde_json = "1.0.32"
serde_derive = "1.0.80" serde_derive = "1.0.80"
hyper = "0.12.13"

30
src/api_info.rs Normal file
View File

@ -0,0 +1,30 @@
use failure::*;
use json_schema::*;
use serde_json::{json, Value};
pub struct ApiMethod {
pub description: &'static str,
pub properties: StaticPropertyMap,
pub returns: Jss,
pub handler: fn(Value) -> Result<Value, Error>,
}
pub type StaticSubdirMap = phf::Map<&'static str, &'static MethodInfo>;
pub struct MethodInfo {
pub get: Option<&'static ApiMethod>,
pub put: Option<&'static ApiMethod>,
pub post: Option<&'static ApiMethod>,
pub delete: Option<&'static ApiMethod>,
pub subdirs: Option<&'static StaticSubdirMap>,
}
pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
get: None,
put: None,
post: None,
delete: None,
subdirs: None,
};

132
src/json_schema.rs Normal file
View File

@ -0,0 +1,132 @@
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,143 +1,16 @@
#![feature(plugin)] #![feature(plugin)]
#![plugin(phf_macros)] #![plugin(phf_macros)]
//extern crate failure; extern crate failure;
extern crate phf; extern crate phf;
extern crate serde_json;
// Jss => JavaScript Schema // Jss => JavaScript Schema
//use failure::Error; //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)
};
pub mod json_schema;
pub mod api_info;

View File

@ -5,8 +5,12 @@ extern crate phf;
extern crate failure; extern crate failure;
use failure::*; use failure::*;
extern crate json_schema; #[macro_use]
use json_schema::*; extern crate apitest;
use apitest::json_schema::*;
use apitest::api_info::*;
extern crate serde_json; extern crate serde_json;
#[macro_use] #[macro_use]
@ -14,6 +18,11 @@ extern crate serde_derive;
use serde_json::{json, Value}; use serde_json::{json, Value};
extern crate hyper;
use hyper::{Body, Request, Response, Server};
use hyper::rt::Future;
use hyper::service::service_fn_ok;
static PARAMETERS1: StaticPropertyMap = phf_map! { static PARAMETERS1: StaticPropertyMap = phf_map! {
"force" => Boolean!{ "force" => Boolean!{
@ -52,12 +61,6 @@ static PARAMETERS1: StaticPropertyMap = phf_map! {
}; };
struct ApiMethod {
description: &'static str,
properties: StaticPropertyMap,
returns: Jss,
handler: fn(Value) -> Result<Value, Error>,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Myparam { struct Myparam {
@ -95,26 +98,29 @@ static TEST_API_METHOD: ApiMethod = ApiMethod {
handler: test_api_handler, handler: test_api_handler,
}; };
type StaticSubdirMap = phf::Map<&'static str, &'static MethodInfo>;
struct MethodInfo {
path: &'static str,
get: Option<&'static ApiMethod>,
subdirs: Option<&'static StaticSubdirMap>,
}
static API3_NODES: MethodInfo = MethodInfo { static API3_NODES: MethodInfo = MethodInfo {
path: "",
get: Some(&TEST_API_METHOD), get: Some(&TEST_API_METHOD),
subdirs: None, ..METHOD_INFO_DEFAULTS
}; };
static API3: MethodInfo = MethodInfo { static API3: MethodInfo = MethodInfo {
path: "",
get: Some(&TEST_API_METHOD), get: Some(&TEST_API_METHOD),
subdirs: Some(&phf_map!{"nodes" => &API3_NODES}), subdirs: Some(&phf_map!{"nodes" => &API3_NODES}),
..METHOD_INFO_DEFAULTS
}; };
fn hello_world(req: Request<Body>) -> Response<Body> {
let method = req.method();
let path = req.uri().path();
println!("REQUEST {} {}", method, path);
Response::new(Body::from("hello World!\n"))
}
fn main() { fn main() {
println!("Fast Static Type Definitions 1"); println!("Fast Static Type Definitions 1");
@ -122,4 +128,17 @@ fn main() {
println!("Parameter: {} Value: {:?}", k, v); println!("Parameter: {} Value: {:?}", k, v);
} }
let addr = ([127, 0, 0, 1], 8007).into();
let new_svc = || {
// service_fn_ok converts our function into a `Service`
service_fn_ok(hello_world)
};
let server = Server::bind(&addr)
.serve(new_svc)
.map_err(|e| eprintln!("server error: {}", e));
// Run this server for... forever!
hyper::rt::run(server);
} }