Replaced panicking From with TryFrom

This commit is contained in:
Oliver Cooper 2021-07-15 16:03:33 +12:00
parent 39dc8399ec
commit 5b6f06ced1

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::convert::TryFrom;
use crate::parser::types::{Field, FragmentDefinition, Selection, SelectionSet};
use crate::{Name, Positioned, SelectionField};
@ -54,19 +55,23 @@ impl<'a> From<SelectionField<'a>> for Lookahead<'a> {
}
/// Convert a slice of `SelectionField`s to a `Lookahead`.
/// Assumes all `SelectionField`s are from the same query and thus have the same fragments.
///
/// This assumes that the `SelectionField`s are all from the same query.
///
/// # Panics
/// Panics if 0 `SelectionField`s are provided.
impl<'a> From<&[SelectionField<'a>]> for Lookahead<'a> {
fn from(selection_fields: &[SelectionField<'a>]) -> Self {
Lookahead {
fragments: selection_fields[0].fragments,
fields: selection_fields
.iter()
.map(|selection_field| selection_field.field)
.collect(),
/// Fails if either no `SelectionField`s were provided.
impl<'a> TryFrom<&[SelectionField<'a>]> for Lookahead<'a> {
type Error = ();
fn try_from(selection_fields: &[SelectionField<'a>]) -> Result<Self, Self::Error> {
if selection_fields.is_empty() {
Err(())
} else {
Ok(Lookahead {
fragments: selection_fields[0].fragments,
fields: selection_fields
.iter()
.map(|selection_field| selection_field.field)
.collect(),
})
}
}
}