Skip to content

Commit

Permalink
refactor: Use enum dispatch
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Oct 26, 2024
1 parent 6b25e32 commit 5dbca06
Show file tree
Hide file tree
Showing 42 changed files with 575 additions and 304 deletions.
6 changes: 3 additions & 3 deletions crates/jsonschema/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
self,
custom::{CustomKeyword, KeywordFactory},
format::Format,
BoxedValidator, BuiltinKeyword, Keyword,
BuiltinKeyword, KeywordKind, KeywordValue,
},
node::SchemaNode,
options::ValidationOptions,
Expand Down Expand Up @@ -400,8 +400,8 @@ pub(crate) fn compile_with<'a>(
if let Some(factory) = ctx.get_keyword_factory(keyword) {
let path = ctx.location().join(keyword);
let validator = CustomKeyword::new(factory.init(schema, value, path)?);
let validator: BoxedValidator = Box::new(validator);
validators.push((Keyword::custom(keyword), validator));
let validator: KeywordValue = validator.into();
validators.push((KeywordKind::custom(keyword), validator));
} else if let Some((keyword, validator)) = keywords::get_for_draft(ctx, keyword)
.and_then(|(keyword, f)| f(ctx, schema, value).map(|v| (keyword, v)))
{
Expand Down
14 changes: 6 additions & 8 deletions crates/jsonschema/src/keywords/additional_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use serde_json::{Map, Value};

pub(crate) struct AdditionalItemsObjectValidator {
node: SchemaNode,
node: Box<SchemaNode>,
items_count: usize,
}
impl AdditionalItemsObjectValidator {
Expand All @@ -20,11 +20,8 @@ impl AdditionalItemsObjectValidator {
schema: &'a Value,
items_count: usize,
) -> CompilationResult<'a> {
let node = compiler::compile(ctx, ctx.as_resource_ref(schema))?;
Ok(Box::new(AdditionalItemsObjectValidator {
node,
items_count,
}))
let node = Box::new(compiler::compile(ctx, ctx.as_resource_ref(schema))?);
Ok(AdditionalItemsObjectValidator { node, items_count }.into())
}
}
impl Validate for AdditionalItemsObjectValidator {
Expand Down Expand Up @@ -75,10 +72,11 @@ pub(crate) struct AdditionalItemsBooleanValidator {
impl AdditionalItemsBooleanValidator {
#[inline]
pub(crate) fn compile<'a>(items_count: usize, location: Location) -> CompilationResult<'a> {
Ok(Box::new(AdditionalItemsBooleanValidator {
Ok(AdditionalItemsBooleanValidator {
items_count,
location,
}))
}
.into())
}
}
impl Validate for AdditionalItemsBooleanValidator {
Expand Down
99 changes: 52 additions & 47 deletions crates/jsonschema/src/keywords/additional_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ macro_rules! iter_errors {
/// }
/// ```
pub(crate) struct AdditionalPropertiesValidator {
node: SchemaNode,
node: Box<SchemaNode>,
}
impl AdditionalPropertiesValidator {
#[inline]
pub(crate) fn compile<'a>(schema: &'a Value, ctx: &compiler::Context) -> CompilationResult<'a> {
let ctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(AdditionalPropertiesValidator {
node: compiler::compile(&ctx, ctx.as_resource_ref(schema))?,
}))
Ok(AdditionalPropertiesValidator {
node: Box::new(compiler::compile(&ctx, ctx.as_resource_ref(schema))?),
}
.into())
}
}
impl Validate for AdditionalPropertiesValidator {
Expand Down Expand Up @@ -161,7 +162,7 @@ pub(crate) struct AdditionalPropertiesFalseValidator {
impl AdditionalPropertiesFalseValidator {
#[inline]
pub(crate) fn compile<'a>(location: Location) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesFalseValidator { location }))
Ok(AdditionalPropertiesFalseValidator { location }.into())
}
}
impl Validate for AdditionalPropertiesFalseValidator {
Expand Down Expand Up @@ -219,10 +220,11 @@ impl AdditionalPropertiesNotEmptyFalseValidator<SmallValidatorsMap> {
map: &'a Map<String, Value>,
ctx: &compiler::Context,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator {
Ok(AdditionalPropertiesNotEmptyFalseValidator {
properties: compile_small_map(ctx, map)?,
location: ctx.location().join("additionalProperties"),
}))
}
.into())
}
}
impl AdditionalPropertiesNotEmptyFalseValidator<BigValidatorsMap> {
Expand All @@ -231,10 +233,11 @@ impl AdditionalPropertiesNotEmptyFalseValidator<BigValidatorsMap> {
map: &'a Map<String, Value>,
ctx: &compiler::Context,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator {
Ok(AdditionalPropertiesNotEmptyFalseValidator {
properties: compile_big_map(ctx, map)?,
location: ctx.location().join("additionalProperties"),
}))
}
.into())
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyFalseValidator<M> {
Expand Down Expand Up @@ -354,7 +357,7 @@ impl<M: PropertiesValidatorsMap> core::fmt::Display
/// }
/// ```
pub(crate) struct AdditionalPropertiesNotEmptyValidator<M: PropertiesValidatorsMap> {
node: SchemaNode,
node: Box<SchemaNode>,
properties: M,
}
impl AdditionalPropertiesNotEmptyValidator<SmallValidatorsMap> {
Expand All @@ -365,10 +368,11 @@ impl AdditionalPropertiesNotEmptyValidator<SmallValidatorsMap> {
schema: &'a Value,
) -> CompilationResult<'a> {
let kctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(AdditionalPropertiesNotEmptyValidator {
Ok(AdditionalPropertiesNotEmptyValidator {
properties: compile_small_map(ctx, map)?,
node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?,
}))
node: Box::new(compiler::compile(&kctx, kctx.as_resource_ref(schema))?),
}
.into())
}
}
impl AdditionalPropertiesNotEmptyValidator<BigValidatorsMap> {
Expand All @@ -379,10 +383,11 @@ impl AdditionalPropertiesNotEmptyValidator<BigValidatorsMap> {
schema: &'a Value,
) -> CompilationResult<'a> {
let kctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(AdditionalPropertiesNotEmptyValidator {
Ok(AdditionalPropertiesNotEmptyValidator {
properties: compile_big_map(ctx, map)?,
node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?,
}))
node: Box::new(compiler::compile(&kctx, kctx.as_resource_ref(schema))?),
}
.into())
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyValidator<M> {
Expand Down Expand Up @@ -479,7 +484,7 @@ impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyValida
/// }
/// ```
pub(crate) struct AdditionalPropertiesWithPatternsValidator {
node: SchemaNode,
node: Box<SchemaNode>,
patterns: PatternedValidators,
/// We need this because `compiler::compile` uses the additionalProperties keyword to compile
/// this validator. That means that the schema node which contains this validator has
Expand All @@ -496,12 +501,13 @@ impl AdditionalPropertiesWithPatternsValidator {
patterns: PatternedValidators,
) -> CompilationResult<'a> {
let kctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(AdditionalPropertiesWithPatternsValidator {
node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?,
Ok(AdditionalPropertiesWithPatternsValidator {
node: Box::new(compiler::compile(&kctx, kctx.as_resource_ref(schema))?),
patterns,
pattern_keyword_path: ctx.location().join("patternProperties"),
pattern_keyword_absolute_location: ctx.new_at_location("patternProperties").base_uri(),
}))
}
.into())
}
}
impl Validate for AdditionalPropertiesWithPatternsValidator {
Expand Down Expand Up @@ -642,12 +648,13 @@ impl AdditionalPropertiesWithPatternsFalseValidator {
ctx: &compiler::Context,
patterns: PatternedValidators,
) -> CompilationResult<'a> {
Ok(Box::new(AdditionalPropertiesWithPatternsFalseValidator {
Ok(AdditionalPropertiesWithPatternsFalseValidator {
patterns,
location: ctx.location().join("additionalProperties"),
pattern_keyword_path: ctx.location().join("patternProperties"),
pattern_keyword_absolute_location: ctx.new_at_location("patternProperties").base_uri(),
}))
}
.into())
}
}
impl Validate for AdditionalPropertiesWithPatternsFalseValidator {
Expand Down Expand Up @@ -795,7 +802,7 @@ impl Validate for AdditionalPropertiesWithPatternsFalseValidator {
/// }
/// ```
pub(crate) struct AdditionalPropertiesWithPatternsNotEmptyValidator<M: PropertiesValidatorsMap> {
node: SchemaNode,
node: Box<SchemaNode>,
properties: M,
patterns: PatternedValidators,
}
Expand All @@ -808,13 +815,12 @@ impl AdditionalPropertiesWithPatternsNotEmptyValidator<SmallValidatorsMap> {
patterns: PatternedValidators,
) -> CompilationResult<'a> {
let kctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyValidator {
node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?,
properties: compile_small_map(ctx, map)?,
patterns,
},
))
Ok(AdditionalPropertiesWithPatternsNotEmptyValidator {
node: Box::new(compiler::compile(&kctx, kctx.as_resource_ref(schema))?),
properties: compile_small_map(ctx, map)?,
patterns,
}
.into())
}
}
impl AdditionalPropertiesWithPatternsNotEmptyValidator<BigValidatorsMap> {
Expand All @@ -826,13 +832,12 @@ impl AdditionalPropertiesWithPatternsNotEmptyValidator<BigValidatorsMap> {
patterns: PatternedValidators,
) -> CompilationResult<'a> {
let kctx = ctx.new_at_location("additionalProperties");
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyValidator {
node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?,
properties: compile_big_map(ctx, map)?,
patterns,
},
))
Ok(AdditionalPropertiesWithPatternsNotEmptyValidator {
node: Box::new(compiler::compile(&kctx, kctx.as_resource_ref(schema))?),
properties: compile_big_map(ctx, map)?,
patterns,
}
.into())
}
}
impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesWithPatternsNotEmptyValidator<M> {
Expand Down Expand Up @@ -1012,13 +1017,14 @@ impl AdditionalPropertiesWithPatternsNotEmptyFalseValidator<SmallValidatorsMap>
ctx: &compiler::Context,
patterns: PatternedValidators,
) -> CompilationResult<'a> {
Ok(Box::new(
Ok(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator::<SmallValidatorsMap> {
properties: compile_small_map(ctx, map)?,
patterns,
location: ctx.location().join("additionalProperties"),
},
))
}
.into(),
)
}
}
impl AdditionalPropertiesWithPatternsNotEmptyFalseValidator<BigValidatorsMap> {
Expand All @@ -1028,13 +1034,12 @@ impl AdditionalPropertiesWithPatternsNotEmptyFalseValidator<BigValidatorsMap> {
ctx: &compiler::Context,
patterns: PatternedValidators,
) -> CompilationResult<'a> {
Ok(Box::new(
AdditionalPropertiesWithPatternsNotEmptyFalseValidator {
properties: compile_big_map(ctx, map)?,
patterns,
location: ctx.location().join("additionalProperties"),
},
))
Ok(AdditionalPropertiesWithPatternsNotEmptyFalseValidator {
properties: compile_big_map(ctx, map)?,
patterns,
location: ctx.location().join("additionalProperties"),
}
.into())
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/jsonschema/src/keywords/all_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AllOfValidator {
let validators = compiler::compile(&ctx, ctx.as_resource_ref(item))?;
schemas.push(validators)
}
Ok(Box::new(AllOfValidator { schemas }))
Ok(AllOfValidator { schemas }.into())
}
}

Expand Down Expand Up @@ -68,16 +68,16 @@ impl Validate for AllOfValidator {
}

pub(crate) struct SingleValueAllOfValidator {
node: SchemaNode,
node: Box<SchemaNode>,
}

impl SingleValueAllOfValidator {
#[inline]
pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> {
let ctx = ctx.new_at_location("allOf");
let ctx = ctx.new_at_location(0);
let node = compiler::compile(&ctx, ctx.as_resource_ref(schema))?;
Ok(Box::new(SingleValueAllOfValidator { node }))
let node = Box::new(compiler::compile(&ctx, ctx.as_resource_ref(schema))?);
Ok(SingleValueAllOfValidator { node }.into())
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/jsonschema/src/keywords/any_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ impl AnyOfValidator {
let node = compiler::compile(&ctx, ctx.as_resource_ref(item))?;
schemas.push(node)
}
Ok(Box::new(AnyOfValidator {
Ok(AnyOfValidator {
schemas,
location: ctx.location().clone(),
}))
}
.into())
} else {
Err(ValidationError::single_type_error(
Location::new(),
Expand Down
2 changes: 1 addition & 1 deletion crates/jsonschema/src/keywords/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) struct FalseValidator {
impl FalseValidator {
#[inline]
pub(crate) fn compile<'a>(location: Location) -> CompilationResult<'a> {
Ok(Box::new(FalseValidator { location }))
Ok(FalseValidator { location }.into())
}
}
impl Validate for FalseValidator {
Expand Down
Loading

0 comments on commit 5dbca06

Please sign in to comment.