define a separate enum ApiStringFormat

This commit is contained in:
Dietmar Maurer 2018-11-07 12:55:33 +01:00
parent d46287e083
commit abbf40fc2e
1 changed files with 12 additions and 5 deletions

View File

@ -29,7 +29,7 @@ pub struct JssString {
pub default: Option<&'static str>, pub default: Option<&'static str>,
pub min_length: Option<usize>, pub min_length: Option<usize>,
pub max_length: Option<usize>, pub max_length: Option<usize>,
pub pattern: Option<Regex>, pub format: ApiStringFormat,
} }
#[derive(Debug)] #[derive(Debug)]
@ -91,9 +91,16 @@ pub const DEFAULTSTRING: JssString = JssString {
default: None, default: None,
min_length: None, min_length: None,
max_length: None, max_length: None,
pattern: None, format: ApiStringFormat::None,
}; };
#[derive(Debug)]
pub enum ApiStringFormat {
None,
Pattern(Regex),
Complex(Box<Jss>),
}
#[macro_export] #[macro_export]
macro_rules! ApiString { macro_rules! ApiString {
($($name:ident => $e:expr),*) => {{ ($($name:ident => $e:expr),*) => {{
@ -168,7 +175,7 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> {
} }
} }
if let Some(ref regex) = jss_string.pattern { if let ApiStringFormat::Pattern(ref regex) = jss_string.format {
if !regex.is_match(&res) { if !regex.is_match(&res) {
bail!("value does not match the regex pattern"); bail!("value does not match the regex pattern");
} }
@ -332,7 +339,7 @@ fn test_query_string() {
let schema = parameter!{name => ApiString!{ let schema = parameter!{name => ApiString!{
optional => false, optional => false,
pattern => Some(Regex::new("test").unwrap()) format => ApiStringFormat::Pattern(Regex::new("test").unwrap())
}}; }};
let res = parse_query_string("name=abcd", &schema, true); let res = parse_query_string("name=abcd", &schema, true);
@ -343,7 +350,7 @@ fn test_query_string() {
let schema = parameter!{name => ApiString!{ let schema = parameter!{name => ApiString!{
optional => false, optional => false,
pattern => Some(Regex::new("^test$").unwrap()) format => ApiStringFormat::Pattern(Regex::new("^test$").unwrap())
}}; }};
let res = parse_query_string("name=ateststring", &schema, true); let res = parse_query_string("name=ateststring", &schema, true);