Skip to content

Commit

Permalink
chore: Move PrimitiveType into dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
macisamuele authored and Stranger6667 committed May 22, 2020
1 parent 9a4f79a commit 97b45d8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 67 deletions.
28 changes: 1 addition & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::primitive_type::PrimitiveType;
use serde_json::Value;
use std::{
borrow::Cow,
Expand Down Expand Up @@ -127,33 +128,6 @@ pub enum ValidationErrorKind {
UnknownReferenceScheme { scheme: String },
}

/// For faster error handling in "type" keyword validator we have this enum, to match
/// with it instead of a string.
#[derive(Debug, Clone)]
pub enum PrimitiveType {
Integer,
Null,
Boolean,
String,
Array,
Object,
Number,
}

impl fmt::Display for PrimitiveType {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
PrimitiveType::Integer => write!(f, "integer"),
PrimitiveType::Null => write!(f, "null"),
PrimitiveType::Boolean => write!(f, "boolean"),
PrimitiveType::String => write!(f, "string"),
PrimitiveType::Array => write!(f, "array"),
PrimitiveType::Object => write!(f, "object"),
PrimitiveType::Number => write!(f, "number"),
}
}
}

#[derive(Debug)]
pub enum TypeKind {
Single(PrimitiveType),
Expand Down
39 changes: 19 additions & 20 deletions src/keywords/legacy/type_draft_4.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::super::{type_, CompilationResult, Validate};
use crate::{
compilation::{CompilationContext, JSONSchema},
error::{error, no_error, CompilationError, ErrorIterator, PrimitiveType, ValidationError},
error::{error, no_error, CompilationError, ErrorIterator, ValidationError},
primitive_type::PrimitiveType,
};
use serde_json::{Map, Number, Value};
use std::convert::TryFrom;

pub struct MultipleTypesValidator {
types: Vec<PrimitiveType>,
Expand All @@ -15,16 +17,13 @@ impl MultipleTypesValidator {
let mut types = Vec::with_capacity(items.len());
for item in items {
match item {
Value::String(string) => match string.as_str() {
"integer" => types.push(PrimitiveType::Integer),
"null" => types.push(PrimitiveType::Null),
"boolean" => types.push(PrimitiveType::Boolean),
"string" => types.push(PrimitiveType::String),
"array" => types.push(PrimitiveType::Array),
"object" => types.push(PrimitiveType::Object),
"number" => types.push(PrimitiveType::Number),
_ => return Err(CompilationError::SchemaError),
},
Value::String(string) => {
if let Ok(primitive_type) = PrimitiveType::try_from(string.as_str()) {
types.push(primitive_type)
} else {
return Err(CompilationError::SchemaError);
}
}
_ => return Err(CompilationError::SchemaError),
}
}
Expand Down Expand Up @@ -132,14 +131,14 @@ pub fn compile(
}

fn compile_single_type(item: &str) -> Option<CompilationResult> {
match item {
"integer" => Some(IntegerTypeValidator::compile()),
"null" => Some(type_::NullTypeValidator::compile()),
"boolean" => Some(type_::BooleanTypeValidator::compile()),
"string" => Some(type_::StringTypeValidator::compile()),
"array" => Some(type_::ArrayTypeValidator::compile()),
"object" => Some(type_::ObjectTypeValidator::compile()),
"number" => Some(type_::NumberTypeValidator::compile()),
_ => Some(Err(CompilationError::SchemaError)),
match PrimitiveType::try_from(item) {
Ok(PrimitiveType::Array) => Some(type_::ArrayTypeValidator::compile()),
Ok(PrimitiveType::Boolean) => Some(type_::BooleanTypeValidator::compile()),
Ok(PrimitiveType::Integer) => Some(IntegerTypeValidator::compile()),
Ok(PrimitiveType::Null) => Some(type_::NullTypeValidator::compile()),
Ok(PrimitiveType::Number) => Some(type_::NumberTypeValidator::compile()),
Ok(PrimitiveType::Object) => Some(type_::ObjectTypeValidator::compile()),
Ok(PrimitiveType::String) => Some(type_::StringTypeValidator::compile()),
Err(()) => Some(Err(CompilationError::SchemaError)),
}
}
39 changes: 19 additions & 20 deletions src/keywords/type_.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::{CompilationResult, Validate};
use crate::{
compilation::{CompilationContext, JSONSchema},
error::{error, no_error, CompilationError, ErrorIterator, PrimitiveType, ValidationError},
error::{error, no_error, CompilationError, ErrorIterator, ValidationError},
primitive_type::PrimitiveType,
};
use serde_json::{Map, Number, Value};
use std::convert::TryFrom;

pub struct MultipleTypesValidator {
types: Vec<PrimitiveType>,
Expand All @@ -15,16 +17,13 @@ impl MultipleTypesValidator {
let mut types = Vec::with_capacity(items.len());
for item in items {
match item {
Value::String(string) => match string.as_str() {
"integer" => types.push(PrimitiveType::Integer),
"null" => types.push(PrimitiveType::Null),
"boolean" => types.push(PrimitiveType::Boolean),
"string" => types.push(PrimitiveType::String),
"array" => types.push(PrimitiveType::Array),
"object" => types.push(PrimitiveType::Object),
"number" => types.push(PrimitiveType::Number),
_ => return Err(CompilationError::SchemaError),
},
Value::String(string) => {
if let Ok(primitive_type) = PrimitiveType::try_from(string.as_str()) {
types.push(primitive_type)
} else {
return Err(CompilationError::SchemaError);
}
}
_ => return Err(CompilationError::SchemaError),
}
}
Expand Down Expand Up @@ -308,14 +307,14 @@ pub fn compile(
}

fn compile_single_type(item: &str) -> Option<CompilationResult> {
match item {
"integer" => Some(IntegerTypeValidator::compile()),
"null" => Some(NullTypeValidator::compile()),
"boolean" => Some(BooleanTypeValidator::compile()),
"string" => Some(StringTypeValidator::compile()),
"array" => Some(ArrayTypeValidator::compile()),
"object" => Some(ObjectTypeValidator::compile()),
"number" => Some(NumberTypeValidator::compile()),
_ => Some(Err(CompilationError::SchemaError)),
match PrimitiveType::try_from(item) {
Ok(PrimitiveType::Array) => Some(ArrayTypeValidator::compile()),
Ok(PrimitiveType::Boolean) => Some(BooleanTypeValidator::compile()),
Ok(PrimitiveType::Integer) => Some(IntegerTypeValidator::compile()),
Ok(PrimitiveType::Null) => Some(NullTypeValidator::compile()),
Ok(PrimitiveType::Number) => Some(NumberTypeValidator::compile()),
Ok(PrimitiveType::Object) => Some(ObjectTypeValidator::compile()),
Ok(PrimitiveType::String) => Some(StringTypeValidator::compile()),
Err(()) => Some(Err(CompilationError::SchemaError)),
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
mod compilation;
mod error;
mod keywords;
mod primitive_type;
mod resolver;
mod schemas;
pub use compilation::JSONSchema;
Expand Down
45 changes: 45 additions & 0 deletions src/primitive_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{convert::TryFrom, fmt};

/// For faster error handling in "type" keyword validator we have this enum, to match
/// with it instead of a string.
#[derive(Debug, Clone)]
pub enum PrimitiveType {
Array,
Boolean,
Integer,
Null,
Number,
Object,
String,
}

impl fmt::Display for PrimitiveType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PrimitiveType::Array => write!(f, "array"),
PrimitiveType::Boolean => write!(f, "boolean"),
PrimitiveType::Integer => write!(f, "integer"),
PrimitiveType::Null => write!(f, "null"),
PrimitiveType::Number => write!(f, "number"),
PrimitiveType::Object => write!(f, "object"),
PrimitiveType::String => write!(f, "string"),
}
}
}

impl TryFrom<&str> for PrimitiveType {
type Error = ();

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"array" => Ok(PrimitiveType::Array),
"boolean" => Ok(PrimitiveType::Boolean),
"integer" => Ok(PrimitiveType::Integer),
"null" => Ok(PrimitiveType::Null),
"number" => Ok(PrimitiveType::Number),
"object" => Ok(PrimitiveType::Object),
"string" => Ok(PrimitiveType::String),
_ => Err(()),
}
}
}

0 comments on commit 97b45d8

Please sign in to comment.