use core::{convert::Infallible, future::Future, num::NonZeroUsize};
use super::{FusedCitizen, FusedEngine, WireRoot};
use crate::{AsyncFusedRead, Fusable, Rooted};
struct Shots;
struct Toy;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Key(u32);
impl Fusable for Shots {
type Options = ();
type Question = String;
}
impl AsyncFusedRead<Toy> for Shots {
type Root = Key;
type Evidence = f32;
type Error = Infallible;
type Ctx<'a>
= &'a str
where
Toy: 'a;
fn keys<'a>(
ctx: Self::Ctx<'a>,
_options: Option<&Self::Options>,
_question: &Self::Question,
_limit: NonZeroUsize,
) -> impl Future<Output = Result<Vec<Rooted<Self::Root, Self::Evidence>>, Self::Error>> {
let ranked = vec![Rooted::new(Key(ctx.len() as u32), 1, 0.5)];
core::future::ready(Ok(ranked))
}
}
impl FusedEngine for Shots {
type Engine = Toy;
}
impl FusedCitizen<Toy> for Shots {
fn ranked<'a>(
_ctx: &'a async_graphql::Context<'_>,
options: Option<&'a Self::Options>,
question: &'a Self::Question,
limit: NonZeroUsize,
) -> core::pin::Pin<
Box<dyn Future<Output = async_graphql::Result<Vec<Rooted<Key, f32>>>> + Send + 'a>,
> {
Box::pin(async move {
let mut held = String::from("mounted");
let ranked =
<Shots as AsyncFusedRead<Toy>>::keys(held.as_mut_str(), options, question, limit)
.await
.map_err(|refusal| async_graphql::Error::new(refusal.to_string()))?;
Ok(ranked)
})
}
}
impl WireRoot for Key {
type Address = u32;
type Entity = u32;
fn address(self) -> Self::Address {
self.0
}
fn hydrate<'a>(
_ctx: &'a async_graphql::Context<'_>,
keys: Vec<Self>,
) -> core::pin::Pin<
Box<dyn Future<Output = async_graphql::Result<Vec<Option<Self::Entity>>>> + Send + 'a>,
> {
let rows: Vec<Option<u32>> = keys.into_iter().map(|key| Some(key.0)).collect();
Box::pin(core::future::ready(Ok(rows)))
}
}
#[test]
fn a_members_wire_engine_is_reached_by_projection() {
fn asked_through<M: FusedEngine>() -> &'static str {
core::any::type_name::<<M as FusedEngine>::Engine>()
}
assert!(
asked_through::<Shots>().ends_with("Toy"),
"{}",
asked_through::<Shots>()
);
}
const _: () = {
#[expect(
clippy::type_complexity,
reason = "the SIGNATURE is the assertion — this fn is never called, and a shape written through \
an alias would stop being the seam's own line"
)]
fn rows<'a, M>(
ctx: &'a async_graphql::Context<'_>,
options: Option<&'a <M as Fusable>::Options>,
question: &'a <M as Fusable>::Question,
limit: NonZeroUsize,
) -> core::pin::Pin<
Box<
dyn Future<
Output = async_graphql::Result<
Vec<Rooted<<M as AsyncFusedRead<Toy>>::Root, <M as AsyncFusedRead<Toy>>::Evidence>>,
>,
> + Send
+ 'a,
>,
>
where
M: FusedCitizen<Toy>,
{
<M as FusedCitizen<Toy>>::ranked(ctx, options, question, limit)
}
let _ = rows::<Shots>;
};
#[test]
fn both_tiers_of_a_root_are_reached_by_projection() {
fn published<K: WireRoot>() -> (
::std::borrow::Cow<'static, str>,
::std::borrow::Cow<'static, str>,
) {
(
<<K as WireRoot>::Address as async_graphql::OutputType>::type_name(),
<<K as WireRoot>::Entity as async_graphql::OutputType>::type_name(),
)
}
assert_eq!(published::<Key>(), ("Int".into(), "Int".into()));
assert_eq!(Key(7).address(), 7);
}