use async_graphql::{
InputType, InputValueError, Name, Value,
indexmap::IndexMap,
registry::{MetaInputValue, Registry},
};
use super::NullableFace;
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be flattened into a composed filter",
label = "no field table to splice",
note = "`#[ingraph(flatten)]` puts a COMPONENT FACE's own fields into this filter, so the field \
it sits on holds a whole filter face and is written BARE — `code: SoundEventCodeFilter`, \
not `Option<…>`, because the component's fields carry their own absence and the component \
has none of its own. What a face has to publish is `ingraph::NullableFace`: every filter \
kind in this crate carries it, and so does every `#[ingraph(composed)]` declaration. An \
operand, a list of operands or a text slot is a SLOT rather than a component — drop the \
`flatten` and let it be one."
)]
pub trait FlattenFace: Sized {
fn flat_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue>;
fn flat_read<F>(object: &IndexMap<Name, Value>) -> Result<Self, InputValueError<F>>
where
F: InputType;
fn flat_values(&self) -> IndexMap<Name, Value>;
}
#[diagnostic::do_not_recommend]
impl<C: NullableFace> FlattenFace for C {
#[inline]
fn flat_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
Self::bare_fields(registry)
}
#[inline]
fn flat_read<F>(object: &IndexMap<Name, Value>) -> Result<Self, InputValueError<F>>
where
F: InputType,
{
<Self as InputType>::parse(Some(Value::Object(object.clone())))
.map_err(InputValueError::propagate)
}
#[inline]
fn flat_values(&self) -> IndexMap<Name, Value> {
Self::bare_values(self)
}
}
#[must_use]
pub fn table(
face: &str,
entries: std::vec::Vec<(String, MetaInputValue)>,
) -> IndexMap<String, MetaInputValue> {
let mut fields = IndexMap::with_capacity(entries.len());
for (name, field) in entries {
assert!(
!fields.contains_key(&name),
"`{face}` publishes the field `{name}` twice — a `#[ingraph(flatten)]` component's own \
fields become this filter's, so one of them has landed on a name the filter already \
declares. Rename the declared slot with `#[ingraph(rename = \"…\")]`, or hold the component \
in a nested slot instead of splicing it."
);
fields.insert(name, field);
}
fields
}