From f1c00214368589bfbcd843949bcf825afc3494a7 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 1 Nov 2018 14:16:41 +0100 Subject: [PATCH] remove hardcoded static lifetime --- src/api_info.rs | 24 ++++++++++++------------ src/json_schema.rs | 44 ++++++++++++++++++++++---------------------- src/main.rs | 8 ++++---- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/api_info.rs b/src/api_info.rs index e1262278..1a4ee4b4 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -3,21 +3,21 @@ use failure::*; use json_schema::*; use serde_json::{Value}; -pub struct ApiMethod { - pub description: &'static str, - pub properties: &'static StaticPropertyMap, - pub returns: &'static Jss, +pub struct ApiMethod<'a> { + pub description: &'a str, + pub properties: &'a PropertyMap<'a>, + pub returns: &'a Jss<'a>, pub handler: fn(Value) -> Result, } -pub type StaticSubdirMap = crate::static_map::StaticMap<'static, &'static str, &'static MethodInfo>; +pub type SubdirMap<'a> = crate::static_map::StaticMap<'a, &'a str, &'a MethodInfo<'a>>; -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 struct MethodInfo<'a> { + pub get: Option<&'a ApiMethod<'a>>, + pub put: Option<&'a ApiMethod<'a>>, + pub post: Option<&'a ApiMethod<'a>>, + pub delete: Option<&'a ApiMethod<'a>>, + pub subdirs: Option<&'a SubdirMap<'a>>, } pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { @@ -28,7 +28,7 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { subdirs: None, }; -pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo> { +pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo<'a>> { if components.len() == 0 { return Some(root); }; diff --git a/src/json_schema.rs b/src/json_schema.rs index 9212b01f..51dbe303 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -1,17 +1,17 @@ use static_map::StaticMap; -pub type StaticPropertyMap = StaticMap<'static, &'static str, Jss>; +pub type PropertyMap<'a> = StaticMap<'a, &'a str, Jss<'a>>; #[derive(Debug)] -pub struct JssBoolean { - pub description: &'static str, +pub struct JssBoolean<'a> { + pub description: &'a str, pub optional: Option, pub default: Option, } #[derive(Debug)] -pub struct JssInteger { - pub description: &'static str, +pub struct JssInteger<'a> { + pub description: &'a str, pub optional: Option, pub minimum: Option, pub maximum: Option, @@ -19,38 +19,38 @@ pub struct JssInteger { } #[derive(Debug)] -pub struct JssString { - pub description: &'static str, +pub struct JssString<'a> { + pub description: &'a str, pub optional: Option, - pub default: Option<&'static str>, + pub default: Option<&'a str>, pub min_length: Option, pub max_length: Option, } #[derive(Debug)] -pub struct JssArray { - pub description: &'static str, +pub struct JssArray<'a> { + pub description: &'a str, pub optional: Option, - pub items: &'static Jss, + pub items: &'a Jss<'a>, } #[derive(Debug)] -pub struct JssObject { - pub description: &'static str, +pub struct JssObject<'a> { + pub description: &'a str, pub optional: Option, pub additional_properties: Option, - pub properties: &'static StaticPropertyMap, + pub properties: &'a PropertyMap<'a>, } #[derive(Debug)] -pub enum Jss { +pub enum Jss<'a> { Null, - Boolean(JssBoolean), - Integer(JssInteger), - String(JssString), - Object(JssObject), - Array(JssArray), - Reference { reference: &'static Jss }, + Boolean(JssBoolean<'a>), + Integer(JssInteger<'a>), + String(JssString<'a>), + Object(JssObject<'a>), + Array(JssArray<'a>), + Reference { reference: &'a Jss<'a> }, } pub static DEFAULTBOOL: JssBoolean = JssBoolean { @@ -109,7 +109,7 @@ macro_rules! Array { }} } -pub static EMPTYOBJECT: StaticPropertyMap = StaticPropertyMap { entries: &[] }; +pub static EMPTYOBJECT: PropertyMap = PropertyMap { entries: &[] }; pub static DEFAULTOBJECT: JssObject = JssObject { description: "", diff --git a/src/main.rs b/src/main.rs index 8a6186d6..704cf1a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ use hyper::{Method, Body, Request, Response, Server, StatusCode}; use hyper::rt::Future; use hyper::service::service_fn_ok; -static PARAMETERS1: StaticPropertyMap = StaticPropertyMap { +static PARAMETERS1: PropertyMap = PropertyMap { entries: &[ ("force", Boolean!{ description => "Test for boolean options." @@ -54,7 +54,7 @@ static PARAMETERS1: StaticPropertyMap = StaticPropertyMap { })), ("myobject", Object!{ description => "TEST Object.", - properties => &StaticPropertyMap { + properties => &PropertyMap { entries: &[ ("vmid", Jss::Reference { reference: &PVE_VMID}), ("loop", Integer!{ @@ -97,7 +97,7 @@ fn test_api_handler(param: Value) -> Result { static TEST_API_METHOD: ApiMethod = ApiMethod { description: "This is a simple test.", - properties: &StaticPropertyMap { + properties: &PropertyMap { entries: &[ ("force", Boolean!{ optional => Some(true), @@ -117,7 +117,7 @@ static API3_NODES: MethodInfo = MethodInfo { static API_ROOT: MethodInfo = MethodInfo { get: Some(&TEST_API_METHOD), - subdirs: Some(&StaticSubdirMap { + subdirs: Some(&SubdirMap { entries: &[ ("nodes", &API3_NODES), ]