MCPcopy Create free account
hub / github.com/csskit/csskit / discriminator

Method discriminator

crates/csskit_derives/src/parse.rs:388–439  ·  view source on GitHub ↗
(&self, shared_atom_paths: &[String], hoisted_type_var_names: &[&Ident])

Source from the content-addressed store, hash-verified

386 }
387
388 fn discriminator(&self, shared_atom_paths: &[String], hoisted_type_var_names: &[&Ident]) -> TokenStream {
389 let all_optional = self.fields.iter().all(|f| f.ty.is_option());
390 let is_all_optional_must_occur =
391 matches!(self.parse_mode, FieldParseMode::OneMustOccur | FieldParseMode::AllMustOccur) && all_optional;
392 if is_all_optional_must_occur {
393 let peeks: Vec<TokenStream> = self
394 .fields
395 .iter()
396 .filter(|f| {
397 f.atom_path_string().is_none_or(|ap| !shared_atom_paths.contains(&ap))
398 && !hoisted_type_var_names.contains(&&f.var)
399 })
400 .map(|f| f.peek_tokens())
401 .collect();
402 if !peeks.is_empty() {
403 return quote! { #(#peeks)||* };
404 }
405 }
406
407 let discriminating =
408 self.fields.iter().find(|f| f.atom_path_string().is_some_and(|ap| !shared_atom_paths.contains(&ap)));
409 if let Some(field) = discriminating {
410 return field.peek_tokens();
411 }
412
413 if let Some(field) = self.fields.iter().find(|f| f.atom.is_some()) {
414 return field.peek_tokens();
415 }
416
417 let peek_types: Vec<Type> = self
418 .fields
419 .iter()
420 .filter(|f| !hoisted_type_var_names.contains(&&f.var))
421 .scan(true, |still_optional, f| {
422 if !*still_optional {
423 return None;
424 }
425 if !f.ty.is_option() {
426 *still_optional = false;
427 }
428 Some(f.ty.unpack_option())
429 })
430 .collect();
431 let type_checks: Vec<TokenStream> = peek_types.iter().map(|t| quote! { p.peek::<#t>() }).collect();
432 if type_checks.len() == 1 {
433 type_checks.into_iter().next().expect("len checked")
434 } else if type_checks.is_empty() {
435 quote! { false }
436 } else {
437 quote! { #(#type_checks)||* }
438 }
439 }
440
441 fn body_with_hoisted(&self, hoisted: &[&Ident], where_collector: &mut WhereCollector) -> TokenStream {
442 let ident = &self.ident;

Callers 1

renderMethod · 0.80

Calls 11

iterMethod · 0.80
is_optionMethod · 0.80
collectMethod · 0.80
atom_path_stringMethod · 0.80
peek_tokensMethod · 0.80
unpack_optionMethod · 0.80
into_iterMethod · 0.80
containsMethod · 0.45
is_emptyMethod · 0.45
lenMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected