diff --git a/src/api/schema.rs b/src/api/schema.rs index cd55eb01..0142a2b8 100644 --- a/src/api/schema.rs +++ b/src/api/schema.rs @@ -57,10 +57,6 @@ impl BooleanSchema { self.default = Some(default); self } - - pub fn arc(self) -> Arc { - Arc::new(self.into()) - } } #[derive(Debug)] @@ -96,10 +92,6 @@ impl IntegerSchema { self.maximum = Some(maximium); self } - - pub fn arc(self) -> Arc { - Arc::new(self.into()) - } } @@ -143,10 +135,6 @@ impl StringSchema { self.max_length = Some(max_length); self } - - pub fn arc(self) -> Arc { - Arc::new(self.into()) - } } #[derive(Debug)] @@ -163,10 +151,6 @@ impl ArraySchema { items: item_schema, } } - - pub fn arc(self) -> Arc { - Arc::new(self.into()) - } } #[derive(Debug)] @@ -192,19 +176,15 @@ impl ObjectSchema { self } - pub fn required(mut self, name: &'static str, schema: Arc) -> Self { - self.properties.insert(name, (false, schema)); + pub fn required>>(mut self, name: &'static str, schema: S) -> Self { + self.properties.insert(name, (false, schema.into())); self } - pub fn optional(mut self, name: &'static str, schema: Arc) -> Self { - self.properties.insert(name, (true, schema)); + pub fn optional>>(mut self, name: &'static str, schema: S) -> Self { + self.properties.insert(name, (true, schema.into())); self } - - pub fn arc(self) -> Arc { - Arc::new(self.into()) - } } #[derive(Debug)] @@ -223,18 +203,36 @@ impl From for Schema { } } +impl From for Arc { + fn from(string_schema: StringSchema) -> Self { + Arc::new(Schema::String(string_schema)) + } +} + impl From for Schema { fn from(boolean_schema: BooleanSchema) -> Self { Schema::Boolean(boolean_schema) } } +impl From for Arc { + fn from(boolean_schema: BooleanSchema) -> Self { + Arc::new(Schema::Boolean(boolean_schema)) + } +} + impl From for Schema { fn from(integer_schema: IntegerSchema) -> Self { Schema::Integer(integer_schema) } } +impl From for Arc { + fn from(integer_schema: IntegerSchema) -> Self { + Arc::new(Schema::Integer(integer_schema)) + } +} + impl From for Schema { fn from(object_schema: ObjectSchema) -> Self { Schema::Object(object_schema) @@ -247,6 +245,12 @@ impl From for Schema { } } +impl From for Arc { + fn from(array_schema: ArraySchema) -> Self { + Arc::new(Schema::Array(array_schema)) + } +} + pub enum ApiStringFormat { Enum(Vec), Pattern(Box), @@ -456,13 +460,13 @@ fn test_schema1() { fn test_query_string() { let schema = ObjectSchema::new("Parameters.") - .required("name", StringSchema::new("Name.").arc()); + .required("name", StringSchema::new("Name.")); let res = parse_query_string("", &schema, true); assert!(res.is_err()); let schema = ObjectSchema::new("Parameters.") - .optional("name", StringSchema::new("Name.").arc()); + .optional("name", StringSchema::new("Name.")); let res = parse_query_string("", &schema, true); assert!(res.is_ok()); @@ -474,7 +478,6 @@ fn test_query_string() { "name", StringSchema::new("Name.") .min_length(5) .max_length(10) - .arc() ); let res = parse_query_string("name=abcd", &schema, true); @@ -495,7 +498,6 @@ fn test_query_string() { .required( "name", StringSchema::new("Name.") .format(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap())))) - .arc() ); let res = parse_query_string("name=abcd", &schema, true); @@ -508,7 +510,6 @@ fn test_query_string() { .required( "name", StringSchema::new("Name.") .format(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap())))) - .arc() ); let res = parse_query_string("name=ateststring", &schema, true); @@ -523,7 +524,6 @@ fn test_query_string() { .required( "name", StringSchema::new("Name.") .format(Arc::new(ApiStringFormat::Enum(vec!["ev1".into(), "ev2".into()]))) - .arc() ); let res = parse_query_string("name=noenum", &schema, true); @@ -546,7 +546,6 @@ fn test_query_integer() { let schema = ObjectSchema::new("Parameters.") .required( "count" , IntegerSchema::new("Count.") - .arc() ); let res = parse_query_string("", &schema, true); @@ -557,7 +556,6 @@ fn test_query_integer() { "count", IntegerSchema::new("Count.") .minimum(-3) .maximum(50) - .arc() ); let res = parse_query_string("", &schema, true); @@ -591,7 +589,6 @@ fn test_query_boolean() { let schema = ObjectSchema::new("Parameters.") .required( "force", BooleanSchema::new("Force.") - .arc() ); let res = parse_query_string("", &schema, true); @@ -600,7 +597,6 @@ fn test_query_boolean() { let schema = ObjectSchema::new("Parameters.") .optional( "force", BooleanSchema::new("Force.") - .arc() ); let res = parse_query_string("", &schema, true); diff --git a/src/api3.rs b/src/api3.rs index 62e89868..4f09b82d 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -45,11 +45,7 @@ pub fn router() -> Router { handler: test_sync_api_handler, description: "This is a simple test.", parameters: ObjectSchema::new("This is a simple test.") - .optional( - "force", - BooleanSchema::new("Test for boolean options") - .arc() - ), + .optional("force", BooleanSchema::new("Test for boolean options")), returns: Schema::Null, }) .subdirs({ diff --git a/src/getopts.rs b/src/getopts.rs index 32a0dad5..e0fdcad5 100644 --- a/src/getopts.rs +++ b/src/getopts.rs @@ -159,7 +159,6 @@ fn test_boolean_arg() { let schema = ObjectSchema::new("Parameters:") .required( "enable", BooleanSchema::new("Enable") - .arc() ); let mut variants: Vec<(Vec<&str>, bool)> = vec![]; @@ -191,8 +190,8 @@ fn test_boolean_arg() { fn test_argument_paramenter() { let schema = ObjectSchema::new("Parameters:") - .required("enable", BooleanSchema::new("Enable.").arc()) - .required("storage", StringSchema::new("Storage.").arc()); + .required("enable", BooleanSchema::new("Enable.")) + .required("storage", StringSchema::new("Storage.")); let args = vec!["-enable", "local"]; let string_args = args.iter().map(|s| s.to_string()).collect(); diff --git a/src/main.rs b/src/main.rs index 94398922..cf62baa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ extern crate apitest; +use std::sync::Arc; + use apitest::api::schema::*; use apitest::api::router::*; use apitest::api::config::*; @@ -16,7 +18,7 @@ use hyper; fn main() { println!("Proxmox REST Server example."); - let prop = StringSchema::new("This is a test").arc(); + let prop : Arc = StringSchema::new("This is a test").into(); //let prop = Arc::new(ApiString!{ optional => true }); let schema = ObjectSchema::new("Parameters.")