juniper/juniper_codegen/src/util/duplicate.rs
Jonas Meurer 558eae91df
Ensure Specification(June 2018) Compliance (#631)
* Implemented most test cases from the specification

* Unified error handling for all generators

- Removed proc-macro-error -> not required -> use syn::Error
- Everything below lib.rs uses proc_macro2::TokenStream
  instead of proc_macro::TokenStream
- Replaced error handling in attribute parsers

* WIP better error messages for *all* macros

* Refactored GraphQLInputObject and minor tweaks

- removed support for Scalar within a string ("DefaultScalarValue")
- removed unraw function and replaced it with the built-in one
- added error messages and return types for all functions within utils
- added more constraints to fulfill the GraphQL spec

* Fixed test-cases which are not compliant with the specification

* Removed unused function

* Added constrains, updated error messages, added marker

* Added argument rename within impl_graphql and fixed `__` tests

* Formatted and cleanup

* Added GraphQLTypeAsync for input object

* Moved codegen tests to separate module

Nightly and stable produce different outputs, thus only test nightly.

* Added IsInputType/IsOutputType traits for type checking

Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
2020-05-01 16:24:01 -10:00

46 lines
1.1 KiB
Rust

//!
use std::collections::HashMap;
pub struct Duplicate<T> {
pub name: String,
pub spanned: Vec<T>,
}
impl<T> Duplicate<T> {
pub fn find_by_key<'a, F>(items: &'a [T], name: F) -> Option<Vec<Duplicate<&'a T>>>
where
T: 'a,
F: Fn(&'a T) -> &'a str,
{
let mut mapping: HashMap<&str, Vec<&T>> = HashMap::with_capacity(items.len());
for item in items {
if let Some(vals) = mapping.get_mut(name(item)) {
vals.push(item);
} else {
mapping.insert(name(item), vec![item]);
}
}
let duplicates = mapping
.into_iter()
.filter_map(|(k, v)| {
if v.len() != 1 {
Some(Duplicate {
name: k.to_string(),
spanned: v,
})
} else {
None
}
})
.collect::<Vec<_>>();
if !duplicates.is_empty() {
Some(duplicates)
} else {
None
}
}
}