rename Jss to Schema
This commit is contained in:
		| @ -38,7 +38,7 @@ pub fn router() -> Router { | |||||||
|             handler: test_subdir_api_handler, |             handler: test_subdir_api_handler, | ||||||
|             description: "Another Endpoint.", |             description: "Another Endpoint.", | ||||||
|             parameters: parameter!{}, |             parameters: parameter!{}, | ||||||
|             returns: Jss::Null, |             returns: Schema::Null, | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|     let route2 = Router::new() |     let route2 = Router::new() | ||||||
| @ -51,7 +51,7 @@ pub fn router() -> Router { | |||||||
|                     description => "Test for boolean options." |                     description => "Test for boolean options." | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
|             returns: Jss::Null, |             returns: Schema::Null, | ||||||
|         }) |         }) | ||||||
|         .subdirs({ |         .subdirs({ | ||||||
|             let mut map = HashMap::new(); |             let mut map = HashMap::new(); | ||||||
|  | |||||||
| @ -6,8 +6,8 @@ use std::collections::HashMap; | |||||||
|  |  | ||||||
| pub struct ApiMethod { | pub struct ApiMethod { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub parameters: Jss, |     pub parameters: Schema, | ||||||
|     pub returns: Jss, |     pub returns: Schema, | ||||||
|     pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>, |     pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>, | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ use url::form_urlencoded; | |||||||
| use regex::Regex; | use regex::Regex; | ||||||
| use std::fmt; | use std::fmt; | ||||||
|  |  | ||||||
| pub type PropertyMap = HashMap<&'static str, Jss>; | pub type PropertyMap = HashMap<&'static str, Schema>; | ||||||
|  |  | ||||||
| #[derive(Debug, Fail)] | #[derive(Debug, Fail)] | ||||||
| pub struct ParameterError { | pub struct ParameterError { | ||||||
| @ -38,14 +38,14 @@ impl fmt::Display for ParameterError { | |||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct JssBoolean { | pub struct BooleanSchema { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub optional: bool, |     pub optional: bool, | ||||||
|     pub default: Option<bool>, |     pub default: Option<bool>, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct JssInteger { | pub struct IntegerSchema { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub optional: bool, |     pub optional: bool, | ||||||
|     pub minimum: Option<isize>, |     pub minimum: Option<isize>, | ||||||
| @ -54,7 +54,7 @@ pub struct JssInteger { | |||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct JssString { | pub struct StringSchema { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub optional: bool, |     pub optional: bool, | ||||||
|     pub default: Option<&'static str>, |     pub default: Option<&'static str>, | ||||||
| @ -64,31 +64,31 @@ pub struct JssString { | |||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct JssArray { | pub struct ArraySchema { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub optional: bool, |     pub optional: bool, | ||||||
|     pub items: Box<Jss>, |     pub items: Box<Schema>, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct JssObject { | pub struct ObjectSchema { | ||||||
|     pub description: &'static str, |     pub description: &'static str, | ||||||
|     pub optional: bool, |     pub optional: bool, | ||||||
|     pub additional_properties: bool, |     pub additional_properties: bool, | ||||||
|     pub properties: HashMap<&'static str, Jss>, |     pub properties: HashMap<&'static str, Schema>, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub enum Jss { | pub enum Schema { | ||||||
|     Null, |     Null, | ||||||
|     Boolean(JssBoolean), |     Boolean(BooleanSchema), | ||||||
|     Integer(JssInteger), |     Integer(IntegerSchema), | ||||||
|     String(JssString), |     String(StringSchema), | ||||||
|     Object(JssObject), |     Object(ObjectSchema), | ||||||
|     Array(JssArray), |     Array(ArraySchema), | ||||||
| } | } | ||||||
|  |  | ||||||
| pub const DEFAULTBOOL: JssBoolean = JssBoolean { | pub const DEFAULTBOOL: BooleanSchema = BooleanSchema { | ||||||
|     description: "", |     description: "", | ||||||
|     optional: false, |     optional: false, | ||||||
|     default: None, |     default: None, | ||||||
| @ -97,11 +97,11 @@ pub const DEFAULTBOOL: JssBoolean = JssBoolean { | |||||||
| #[macro_export] | #[macro_export] | ||||||
| macro_rules! Boolean { | macro_rules! Boolean { | ||||||
|     ($($name:ident => $e:expr),*) => {{ |     ($($name:ident => $e:expr),*) => {{ | ||||||
|         Jss::Boolean(JssBoolean { $($name: $e, )* ..DEFAULTBOOL}) |         Schema::Boolean(BooleanSchema { $($name: $e, )* ..DEFAULTBOOL}) | ||||||
|     }} |     }} | ||||||
| } | } | ||||||
|  |  | ||||||
| pub const DEFAULTINTEGER: JssInteger = JssInteger { | pub const DEFAULTINTEGER: IntegerSchema = IntegerSchema { | ||||||
|     description: "", |     description: "", | ||||||
|     optional: false, |     optional: false, | ||||||
|     default: None, |     default: None, | ||||||
| @ -112,11 +112,11 @@ pub const DEFAULTINTEGER: JssInteger = JssInteger { | |||||||
| #[macro_export] | #[macro_export] | ||||||
| macro_rules! Integer { | macro_rules! Integer { | ||||||
|     ($($name:ident => $e:expr),*) => {{ |     ($($name:ident => $e:expr),*) => {{ | ||||||
|         Jss::Integer(JssInteger { $($name: $e, )* ..DEFAULTINTEGER}) |         Schema::Integer(IntegerSchema { $($name: $e, )* ..DEFAULTINTEGER}) | ||||||
|     }} |     }} | ||||||
| } | } | ||||||
|  |  | ||||||
| pub const DEFAULTSTRING: JssString = JssString { | pub const DEFAULTSTRING: StringSchema = StringSchema { | ||||||
|     description: "", |     description: "", | ||||||
|     optional: false, |     optional: false, | ||||||
|     default: None, |     default: None, | ||||||
| @ -130,13 +130,13 @@ pub enum ApiStringFormat { | |||||||
|     None, |     None, | ||||||
|     Enum(Vec<String>), |     Enum(Vec<String>), | ||||||
|     Pattern(Box<Regex>), |     Pattern(Box<Regex>), | ||||||
|     Complex(Box<Jss>), |     Complex(Box<Schema>), | ||||||
| } | } | ||||||
|  |  | ||||||
| #[macro_export] | #[macro_export] | ||||||
| macro_rules! ApiString { | macro_rules! ApiString { | ||||||
|     ($($name:ident => $e:expr),*) => {{ |     ($($name:ident => $e:expr),*) => {{ | ||||||
|         Jss::String(JssString { $($name: $e, )* ..DEFAULTSTRING}) |         Schema::String(StringSchema { $($name: $e, )* ..DEFAULTSTRING}) | ||||||
|     }} |     }} | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -144,22 +144,22 @@ macro_rules! ApiString { | |||||||
| #[macro_export] | #[macro_export] | ||||||
| macro_rules! parameter { | macro_rules! parameter { | ||||||
|     () => {{ |     () => {{ | ||||||
|         let inner = JssObject { |         let inner = ObjectSchema { | ||||||
|             description: "", |             description: "", | ||||||
|             optional: false, |             optional: false, | ||||||
|             additional_properties: false, |             additional_properties: false, | ||||||
|             properties: HashMap::<&'static str, Jss>::new(), |             properties: HashMap::<&'static str, Schema>::new(), | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         Jss::Object(inner) |         Schema::Object(inner) | ||||||
|     }}; |     }}; | ||||||
|     ($($name:ident => $e:expr),*) => {{ |     ($($name:ident => $e:expr),*) => {{ | ||||||
|         let inner = JssObject { |         let inner = ObjectSchema { | ||||||
|             description: "", |             description: "", | ||||||
|             optional: false, |             optional: false, | ||||||
|             additional_properties: false, |             additional_properties: false, | ||||||
|             properties: { |             properties: { | ||||||
|                 let mut map = HashMap::<&'static str, Jss>::new(); |                 let mut map = HashMap::<&'static str, Schema>::new(); | ||||||
|                 $( |                 $( | ||||||
|                     map.insert(stringify!($name), $e); |                     map.insert(stringify!($name), $e); | ||||||
|                 )* |                 )* | ||||||
| @ -167,17 +167,17 @@ macro_rules! parameter { | |||||||
|             } |             } | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         Jss::Object(inner) |         Schema::Object(inner) | ||||||
|     }}; |     }}; | ||||||
| } | } | ||||||
|  |  | ||||||
| fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> { | fn parse_simple_value(value_str: &str, schema: &Schema) -> Result<Value, Error> { | ||||||
|  |  | ||||||
|     let value = match schema { |     let value = match schema { | ||||||
|         Jss::Null => { |         Schema::Null => { | ||||||
|             bail!("internal error - found Null schema."); |             bail!("internal error - found Null schema."); | ||||||
|         } |         } | ||||||
|         Jss::Boolean(_jss_boolean) => { |         Schema::Boolean(_jss_boolean) => { | ||||||
|             let res = match value_str.to_lowercase().as_str() { |             let res = match value_str.to_lowercase().as_str() { | ||||||
|                 "1" | "on" | "yes" | "true" => true, |                 "1" | "on" | "yes" | "true" => true, | ||||||
|                 "0" | "off" | "no" | "false" => false, |                 "0" | "off" | "no" | "false" => false, | ||||||
| @ -185,7 +185,7 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> { | |||||||
|             }; |             }; | ||||||
|             Value::Bool(res) |             Value::Bool(res) | ||||||
|         } |         } | ||||||
|         Jss::Integer(jss_integer) => { |         Schema::Integer(jss_integer) => { | ||||||
|             let res: isize = value_str.parse()?; |             let res: isize = value_str.parse()?; | ||||||
|  |  | ||||||
|             if let Some(minimum) = jss_integer.minimum { |             if let Some(minimum) = jss_integer.minimum { | ||||||
| @ -202,7 +202,7 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> { | |||||||
|  |  | ||||||
|             Value::Number(res.into()) |             Value::Number(res.into()) | ||||||
|         } |         } | ||||||
|         Jss::String(jss_string) => { |         Schema::String(jss_string) => { | ||||||
|             let res: String = value_str.into(); |             let res: String = value_str.into(); | ||||||
|             let char_count = res.chars().count(); |             let char_count = res.chars().count(); | ||||||
|  |  | ||||||
| @ -242,7 +242,7 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> { | |||||||
|     Ok(value) |     Ok(value) | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_required: bool) -> Result<Value, ParameterError> { | pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, test_required: bool) -> Result<Value, ParameterError> { | ||||||
|  |  | ||||||
|     println!("QUERY Strings {:?}", data); |     println!("QUERY Strings {:?}", data); | ||||||
|  |  | ||||||
| @ -251,11 +251,11 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_ | |||||||
|     let mut errors = ParameterError::new(); |     let mut errors = ParameterError::new(); | ||||||
|  |  | ||||||
|     match schema { |     match schema { | ||||||
|         Jss::Object(JssObject { properties, additional_properties, .. })   => { |         Schema::Object(ObjectSchema { properties, additional_properties, .. })   => { | ||||||
|             for (key, value) in data { |             for (key, value) in data { | ||||||
|                 if let Some(prop_schema) = properties.get::<str>(key) { |                 if let Some(prop_schema) = properties.get::<str>(key) { | ||||||
|                     match prop_schema { |                     match prop_schema { | ||||||
|                         Jss::Array(jss_array) => { |                         Schema::Array(jss_array) => { | ||||||
|                             if params[key] == Value::Null { |                             if params[key] == Value::Null { | ||||||
|                                 params[key] = json!([]); |                                 params[key] = json!([]); | ||||||
|                             } |                             } | ||||||
| @ -307,12 +307,12 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_ | |||||||
|             if test_required && errors.len() == 0 { |             if test_required && errors.len() == 0 { | ||||||
|                 for (name, prop_schema) in properties { |                 for (name, prop_schema) in properties { | ||||||
|                     let optional = match prop_schema { |                     let optional = match prop_schema { | ||||||
|                         Jss::Boolean(jss_boolean) => jss_boolean.optional, |                         Schema::Boolean(jss_boolean) => jss_boolean.optional, | ||||||
|                         Jss::Integer(jss_integer) => jss_integer.optional, |                         Schema::Integer(jss_integer) => jss_integer.optional, | ||||||
|                         Jss::String(jss_string) => jss_string.optional, |                         Schema::String(jss_string) => jss_string.optional, | ||||||
|                         Jss::Array(jss_array) => jss_array.optional, |                         Schema::Array(jss_array) => jss_array.optional, | ||||||
|                         Jss::Object(jss_object) => jss_object.optional, |                         Schema::Object(jss_object) => jss_object.optional, | ||||||
|                         Jss::Null => true, |                         Schema::Null => true, | ||||||
|                     }; |                     }; | ||||||
|                     if optional == false && params[name] == Value::Null { |                     if optional == false && params[name] == Value::Null { | ||||||
|                         errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name)); |                         errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name)); | ||||||
| @ -331,7 +331,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_ | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn parse_query_string(query: &str, schema: &Jss, test_required: bool) -> Result<Value,  ParameterError> { | pub fn parse_query_string(query: &str, schema: &Schema, test_required: bool) -> Result<Value,  ParameterError> { | ||||||
|  |  | ||||||
|     let param_list: Vec<(String, String)> = |     let param_list: Vec<(String, String)> = | ||||||
|         form_urlencoded::parse(query.as_bytes()).into_owned().collect(); |         form_urlencoded::parse(query.as_bytes()).into_owned().collect(); | ||||||
| @ -341,7 +341,7 @@ pub fn parse_query_string(query: &str, schema: &Jss, test_required: bool) -> Res | |||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| fn test_shema1() { | fn test_shema1() { | ||||||
|     let schema = Jss::Object(JssObject { |     let schema = Schema::Object(ObjectSchema { | ||||||
|         description: "TEST", |         description: "TEST", | ||||||
|         optional: false, |         optional: false, | ||||||
|         additional_properties: false, |         additional_properties: false, | ||||||
| @ -536,7 +536,7 @@ fn test_shema1() { | |||||||
|             description => "Test Array of simple integers.", |             description => "Test Array of simple integers.", | ||||||
|             items => &PVE_VMID |             items => &PVE_VMID | ||||||
|         }, |         }, | ||||||
|         myarray2 => &Jss::Array(JssArray { |         myarray2 => &Schema::Array(ArraySchema { | ||||||
|             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."}, | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user