Skip to content

Commit

Permalink
Add helper for Default
Browse files Browse the repository at this point in the history
Co-authored-by: dpetrick <[email protected]>
  • Loading branch information
JoviDeCroock and dpetrick committed May 22, 2024
1 parent f31dbb8 commit cf66425
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/ast/ast_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ pub trait DefaultIn<'a> {
fn default_in(arena: &'a bumpalo::Bump) -> Self;
}

impl<'a, T> DefaultIn<'a> for T
where
T: Default,
{
fn default_in(_ctx: &'a bumpalo::Bump) -> Self {
Self::default()
}
}

impl<'a> DefaultIn<'a> for Document<'a> {
fn default_in(arena: &'a bumpalo::Bump) -> Self {
Document {
Expand Down
2 changes: 1 addition & 1 deletion src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! As such, the [`DefaultRules`](rules::DefaultRules) rule is a [`ValidationRule`] itself that's
//! composed using the [`ComposedVisitor`](crate::visit::ComposedVisitor) utility.
//!
//! All rules must implement the [`DefaultIn`](crate::ast::DefaultIn) trait, which makes it easier to quickly run a validation
//! All rules must implement the [`DefaultIn`](crate::ast::DefaultIn) or `Default` trait, which makes it easier to quickly run a validation
//! rule and isolates them from external state, since no validation requires any external state.
//!
//! For example, this is one way to run a validation rule, in this case `DefaultRules`:
Expand Down
10 changes: 1 addition & 9 deletions src/validate/rules/lone_anonymous_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,12 @@ use crate::{ast::*, visit::*};
///
/// See [`ValidationRule`]
/// [Reference](https://spec.graphql.org/October2021/#sec-Lone-Anonymous-Operation)
#[derive(Default)]
pub struct LoneAnonymousOperation {
operations: usize,
has_anonymous: bool,
}

impl<'a> DefaultIn<'a> for LoneAnonymousOperation {
fn default_in(_arena: &'a bumpalo::Bump) -> Self {
Self {
operations: 0,
has_anonymous: false
}
}
}

impl<'a> ValidationRule<'a> for LoneAnonymousOperation {}

impl<'a> Visitor<'a, ValidationContext<'a>> for LoneAnonymousOperation {
Expand Down

0 comments on commit cf66425

Please sign in to comment.