implement Into Arc<Schema>

This commit is contained in:
Dietmar Maurer 2018-11-23 13:18:41 +01:00
parent 8a41cfdd55
commit 768e01091a
4 changed files with 36 additions and 43 deletions

View File

@ -57,10 +57,6 @@ impl BooleanSchema {
self.default = Some(default);
self
}
pub fn arc(self) -> Arc<Schema> {
Arc::new(self.into())
}
}
#[derive(Debug)]
@ -96,10 +92,6 @@ impl IntegerSchema {
self.maximum = Some(maximium);
self
}
pub fn arc(self) -> Arc<Schema> {
Arc::new(self.into())
}
}
@ -143,10 +135,6 @@ impl StringSchema {
self.max_length = Some(max_length);
self
}
pub fn arc(self) -> Arc<Schema> {
Arc::new(self.into())
}
}
#[derive(Debug)]
@ -163,10 +151,6 @@ impl ArraySchema {
items: item_schema,
}
}
pub fn arc(self) -> Arc<Schema> {
Arc::new(self.into())
}
}
#[derive(Debug)]
@ -192,19 +176,15 @@ impl ObjectSchema {
self
}
pub fn required(mut self, name: &'static str, schema: Arc<Schema>) -> Self {
self.properties.insert(name, (false, schema));
pub fn required<S: Into<Arc<Schema>>>(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<Schema>) -> Self {
self.properties.insert(name, (true, schema));
pub fn optional<S: Into<Arc<Schema>>>(mut self, name: &'static str, schema: S) -> Self {
self.properties.insert(name, (true, schema.into()));
self
}
pub fn arc(self) -> Arc<Schema> {
Arc::new(self.into())
}
}
#[derive(Debug)]
@ -223,18 +203,36 @@ impl From<StringSchema> for Schema {
}
}
impl From<StringSchema> for Arc<Schema> {
fn from(string_schema: StringSchema) -> Self {
Arc::new(Schema::String(string_schema))
}
}
impl From<BooleanSchema> for Schema {
fn from(boolean_schema: BooleanSchema) -> Self {
Schema::Boolean(boolean_schema)
}
}
impl From<BooleanSchema> for Arc<Schema> {
fn from(boolean_schema: BooleanSchema) -> Self {
Arc::new(Schema::Boolean(boolean_schema))
}
}
impl From<IntegerSchema> for Schema {
fn from(integer_schema: IntegerSchema) -> Self {
Schema::Integer(integer_schema)
}
}
impl From<IntegerSchema> for Arc<Schema> {
fn from(integer_schema: IntegerSchema) -> Self {
Arc::new(Schema::Integer(integer_schema))
}
}
impl From<ObjectSchema> for Schema {
fn from(object_schema: ObjectSchema) -> Self {
Schema::Object(object_schema)
@ -247,6 +245,12 @@ impl From<ArraySchema> for Schema {
}
}
impl From<ArraySchema> for Arc<Schema> {
fn from(array_schema: ArraySchema) -> Self {
Arc::new(Schema::Array(array_schema))
}
}
pub enum ApiStringFormat {
Enum(Vec<String>),
Pattern(Box<Regex>),
@ -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);

View File

@ -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({

View File

@ -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();

View File

@ -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<Schema> = StringSchema::new("This is a test").into();
//let prop = Arc::new(ApiString!{ optional => true });
let schema = ObjectSchema::new("Parameters.")