#[macro_export]
macro_rules! define_selector {
(
$( #[$($struct_meta:tt)*] )*
$vis:vis struct $name:ident {
$(
$( #[$($field_meta:tt)*] )*
$field_fn:ident $( ($nested_ty:ident) )? : $gql_ident:ident
),* $(,)?
}
) => {
$crate::define_selector! {
@parse_struct_attrs
struct = ($vis struct $name),
raw_attrs = ( $( #[$($struct_meta)*] )* ),
clean_docs = [],
default_str = None,
default_cost = None,
fields = [
$(
( ( $( #[$($field_meta)*] )* ), $field_fn, ( $($nested_ty)? ), $gql_ident )
)*
]
}
};
(@unwrap_cost None, $default:expr) => { $default };
(@unwrap_cost (Some($cost:expr)), $default:expr) => { $cost };
(@field_cost () , () ) => { 1 };
(@field_cost () , ($nested_ty:path) ) => {
<$nested_ty as $crate::traits::BuildableSelector>::DEFAULT_COMPLEXITY
};
(@field_cost ( #[complexity = $c:expr] $( #[$($rest:tt)*] )* ) , $nested:tt ) => { $c };
(@field_cost ( #[$($other:tt)*] $( #[$($rest:tt)*] )* ) , $nested:tt ) => {
$crate::define_selector!(@field_cost ( $( #[$($rest)*] )* ) , $nested )
};
(
@parse_struct_attrs
struct = $struct_def:tt,
raw_attrs = ( #[default_complexity = $cost:expr] $( #[$($rest:tt)*] )* ),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = $default_str:tt,
default_cost = $old_cost:tt,
fields = $fields:tt
) => {
$crate::define_selector! {
@parse_struct_attrs
struct = $struct_def,
raw_attrs = ( $( #[$($rest)*] )* ),
clean_docs = [ $( #[$($docs)*] )* ],
default_str = $default_str,
default_cost = (Some($cost)),
fields = $fields
}
};
(
@parse_struct_attrs
struct = $struct_def:tt,
raw_attrs = ( #[default = $default:expr] $( #[$($rest:tt)*] )* ),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = $default_str:tt,
default_cost = $default_cost:tt,
fields = $fields:tt
) => {
$crate::define_selector! {
@parse_struct_attrs
struct = $struct_def,
raw_attrs = ( $( #[$($rest)*] )* ),
clean_docs = [ $( #[$($docs)*] )* ],
default_str = (Some($default)),
default_cost = $default_cost,
fields = $fields
}
};
(
@parse_struct_attrs
struct = $struct_def:tt,
raw_attrs = ( #[$($other:tt)*] $( #[$($rest:tt)*] )* ),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = $default_str:tt,
default_cost = $default_cost:tt,
fields = $fields:tt
) => {
$crate::define_selector! {
@parse_struct_attrs
struct = $struct_def,
raw_attrs = ( $( #[$($rest)*] )* ),
clean_docs = [ $( #[$($docs)*] )* #[$($other)*] ],
default_str = $default_str,
default_cost = $default_cost,
fields = $fields
}
};
(
@parse_struct_attrs
struct = ($vis:vis struct $name:ident),
raw_attrs = (),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = (Some($default_str:expr)),
default_cost = (Some($default_cost:expr)),
fields = $fields:tt
) => {
$crate::define_selector! {
@build_struct
default = $default_str,
default_cost = $default_cost,
docs = [ $( #[$($docs)*] )* ],
struct = ($vis struct $name),
fields = $fields
}
};
(
@parse_struct_attrs
struct = ($vis:vis struct $name:ident),
raw_attrs = (),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = (Some($default_str:expr)),
default_cost = None,
fields = $fields:tt
) => {
$crate::define_selector! {
@build_struct
default = $default_str,
default_cost = 1,
docs = [ $( #[$($docs)*] )* ],
struct = ($vis struct $name),
fields = $fields
}
};
(
@parse_struct_attrs
struct = ($vis:vis struct $name:ident),
raw_attrs = (),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = None,
default_cost = (Some($default_cost:expr)),
fields = $fields:tt
) => {
$crate::define_selector! {
@build_struct
default = "",
default_cost = $default_cost,
docs = [ $( #[$($docs)*] )* ],
struct = ($vis struct $name),
fields = $fields
}
};
(
@parse_struct_attrs
struct = ($vis:vis struct $name:ident),
raw_attrs = (),
clean_docs = [ $( #[$($docs:tt)*] )* ],
default_str = None,
default_cost = None,
fields = $fields:tt
) => {
$crate::define_selector! {
@build_struct
default = "",
default_cost = 1,
docs = [ $( #[$($docs)*] )* ],
struct = ($vis struct $name),
fields = $fields
}
};
(
@build_struct
default = $default_str:expr,
default_cost = $default_cost:expr,
docs = [ $( #[$($doc_meta:tt)*] )* ],
struct = ($vis:vis struct $name:ident),
fields = [
$(
( $f_meta:tt, $field_fn:ident, $f_nested:tt, $gql_ident:ident )
)*
]
) => {
$( #[$($doc_meta)*] )*
#[doc = ""]
#[doc = concat!("**Complexity**: `", stringify!($default_cost), "` (base root weight).")]
#[allow(non_upper_case_globals)]
#[derive(Debug, Clone)]
$vis struct $name<
$( const $field_fn: bool = false ),*
> {
pub(crate) fields: std::collections::BTreeSet<String>,
pub(crate) total_complexity: u32,
pub(crate) total_depth: u32,
}
impl $name {
pub fn new() -> Self {
Self {
fields: std::collections::BTreeSet::new(),
total_complexity: 0,
total_depth: 1,
}
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
#[allow(non_upper_case_globals)]
impl<
$( const $field_fn: bool ),*
> $name< $( { $field_fn } ),* > {
pub fn field_default_complexity(gql_name: &str) -> u32 {
match gql_name {
$(
stringify!($gql_ident) => {
$crate::define_selector!(@field_cost $f_meta, $f_nested)
}
)*
_ => 1,
}
}
}
#[allow(non_upper_case_globals)]
impl<
$( const $field_fn: bool ),*
> $crate::traits::BuildableSelector for $name< $( { $field_fn } ),* > {
const DEFAULT_COMPLEXITY: u32 = $default_cost;
fn build(&self) -> String {
if self.fields.is_empty() {
return $default_str.to_string();
}
self.fields.iter().cloned().collect::<Vec<_>>().join(" ")
}
fn root_complexity(&self) -> u32 {
Self::DEFAULT_COMPLEXITY
}
fn fields_complexity(&self) -> u32 {
if self.fields.is_empty() {
let mut total = 0;
for field_name in $default_str.split_whitespace() {
total += Self::field_default_complexity(field_name);
}
total
} else {
self.total_complexity
}
}
fn depth(&self) -> u32 {
if self.fields.is_empty() {
1 } else {
self.total_depth
}
}
}
$crate::define_selector! {
@munch_all
struct = $name,
before = [],
after = [
$(
( $f_meta, $field_fn, $f_nested, $gql_ident )
)*
]
}
};
(@munch_all struct = $name:ident, before = [ $( $b:tt )* ], after = []) => {};
(
@munch_all
struct = $name:ident,
before = [ $( ( $b_meta:tt, $b_fn:ident, $b_nested:tt, $b_gql:ident ) )* ],
after = [
( $curr_meta:tt, $curr_fn:ident, $curr_nested:tt, $curr_gql:ident )
$( ( $a_meta:tt, $a_fn:ident, $a_nested:tt, $a_gql:ident ) )*
]
) => {
$crate::define_selector! {
@parse_attrs
struct = $name,
before = [ $( $b_fn )* ],
after = [ $( $a_fn )* ],
field = $curr_fn,
nested = $curr_nested,
gql = $curr_gql,
attrs = $curr_meta,
clean_attrs = [],
current_cost = None
}
$crate::define_selector! {
@munch_all
struct = $name,
before = [
$( ( $b_meta, $b_fn, $b_nested, $b_gql ) )*
( $curr_meta, $curr_fn, $curr_nested, $curr_gql )
],
after = [ $( ( $a_meta, $a_fn, $a_nested, $a_gql ) )* ]
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = $nested:tt,
gql = $curr_gql:ident,
attrs = ( #[complexity = $cost:expr] $( #[$($rest:tt)*] )* ),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = $old_cost:tt
) => {
$crate::define_selector! {
@parse_attrs
struct = $name,
before = [ $( $b )* ],
after = [ $( $a )* ],
field = $curr_fn,
nested = $nested,
gql = $curr_gql,
attrs = ( $( #[$($rest)*] )* ),
clean_attrs = [ $( #[$($clean)*] )* ],
current_cost = (Some($cost))
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = $nested:tt,
gql = $curr_gql:ident,
attrs = ( #[$($other:tt)*] $( #[$($rest:tt)*] )* ),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = $cost_opt:tt
) => {
$crate::define_selector! {
@parse_attrs
struct = $name,
before = [ $( $b )* ],
after = [ $( $a )* ],
field = $curr_fn,
nested = $nested,
gql = $curr_gql,
attrs = ( $( #[$($rest)*] )* ),
clean_attrs = [ $( #[$($clean)*] )* #[$($other)*] ],
current_cost = $cost_opt
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = (),
gql = $curr_gql:ident,
attrs = (),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = (Some($cost:expr))
) => {
#[allow(non_upper_case_globals)]
impl<
$( const $b: bool, )*
$( const $a: bool, )*
> $name< $( { $b }, )* false, $( { $a }, )* > {
$( #[$($clean)*] )*
#[doc = ""]
#[doc = concat!("**Complexity**: `", stringify!($cost), "`")]
pub fn $curr_fn(mut self) -> $name< $( { $b }, )* true, $( { $a }, )* > {
self.fields.insert(stringify!($curr_gql).to_string());
self.total_complexity += $cost;
self.total_depth = self.total_depth.max(1);
$name {
fields: self.fields,
total_complexity: self.total_complexity,
total_depth: self.total_depth,
}
}
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = (),
gql = $curr_gql:ident,
attrs = (),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = None
) => {
#[allow(non_upper_case_globals)]
impl<
$( const $b: bool, )*
$( const $a: bool, )*
> $name< $( { $b }, )* false, $( { $a }, )* > {
$( #[$($clean)*] )*
#[doc = ""]
#[doc = "**Complexity**: `1`"]
pub fn $curr_fn(mut self) -> $name< $( { $b }, )* true, $( { $a }, )* > {
self.fields.insert(stringify!($curr_gql).to_string());
self.total_complexity += 1;
self.total_depth = self.total_depth.max(1);
$name {
fields: self.fields,
total_complexity: self.total_complexity,
total_depth: self.total_depth,
}
}
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = ($nested_ty:ident),
gql = $curr_gql:ident,
attrs = (),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = (Some($cost:expr))
) => {
#[allow(non_upper_case_globals)]
impl<
$( const $b: bool, )*
$( const $a: bool, )*
> $name< $( { $b }, )* false, $( { $a }, )* > {
$( #[$($clean)*] )*
#[doc = ""]
#[doc = concat!("**Complexity**: `", stringify!($cost), "`")]
pub fn $curr_fn<F, S>(mut self, selector_fn: F) -> $name< $( { $b }, )* true, $( { $a }, )* >
where
F: FnOnce($nested_ty) -> S,
S: $crate::traits::BuildableSelector,
{
let initial = <$nested_ty>::new();
let built = selector_fn(initial);
let child_cost = built.fields_complexity();
let child_depth = built.depth();
self.fields.insert(format!("{} {{ {} }}", stringify!($curr_gql), built.build()));
self.total_complexity += $cost + child_cost;
self.total_depth = self.total_depth.max(1 + child_depth);
$name {
fields: self.fields,
total_complexity: self.total_complexity,
total_depth: self.total_depth,
}
}
}
};
(
@parse_attrs
struct = $name:ident,
before = [ $( $b:ident )* ],
after = [ $( $a:ident )* ],
field = $curr_fn:ident,
nested = ($nested_ty:ident),
gql = $curr_gql:ident,
attrs = (),
clean_attrs = [ $( #[$($clean:tt)*] )* ],
current_cost = None
) => {
#[allow(non_upper_case_globals)]
impl<
$( const $b: bool, )*
$( const $a: bool, )*
> $name< $( { $b }, )* false, $( { $a }, )* > {
$( #[$($clean)*] )*
#[doc = ""]
#[doc = concat!("**Complexity**: Base weight of [`", stringify!($nested_ty), "`].")]
pub fn $curr_fn<F, S>(mut self, selector_fn: F) -> $name< $( { $b }, )* true, $( { $a }, )* >
where
F: FnOnce($nested_ty) -> S,
S: $crate::traits::BuildableSelector,
{
let field_cost = <$nested_ty as $crate::traits::BuildableSelector>::DEFAULT_COMPLEXITY;
let initial = <$nested_ty>::new();
let built = selector_fn(initial);
let child_cost = built.fields_complexity();
let child_depth = built.depth();
self.fields.insert(format!("{} {{ {} }}", stringify!($curr_gql), built.build()));
self.total_complexity += field_cost + child_cost;
self.total_depth = self.total_depth.max(1 + child_depth);
$name {
fields: self.fields,
total_complexity: self.total_complexity,
total_depth: self.total_depth,
}
}
}
};
}