use crate::Resources;
use super::{AsyncInitialize, Initialize, Initializes, Makers, UnboundCorpus};
use crate::{
Async, AsyncResource, AsyncTypeVisitor, Block, Describe, Habitat, ResourceKind, Shared,
SlotBinding, SlotColumn, SlotName, SlotSchema, TypeVisitor,
};
struct Corpora;
struct Remote;
impl AsyncResource for Corpora {
type Options = ();
type Error = core::convert::Infallible;
async fn build(_ctx: &Resources, _options: ()) -> Result<Self, Self::Error> {
Ok(Self)
}
}
impl AsyncResource for Remote {
type Options = ();
type Error = core::convert::Infallible;
async fn build(_ctx: &Resources, _options: ()) -> Result<Self, Self::Error> {
Ok(Self)
}
}
impl Habitat for Corpora {
type Blueprint = &'static [SlotSchema];
}
impl Habitat for Remote {
type Blueprint = &'static [SlotSchema];
}
#[derive(Default)]
struct Maker {
made: Vec<String>,
}
impl TypeVisitor for Maker {
type Db = Corpora;
type Error = core::convert::Infallible;
fn next_type<E: Describe<Corpora>>(&mut self) -> Result<(), Self::Error> {
for corpus in E::BLUEPRINT {
self
.made
.push(format!("blocking:{}", corpus.slot().as_ref()));
}
Ok(())
}
}
impl AsyncTypeVisitor for Maker {
type Db = Remote;
type Error = core::convert::Infallible;
async fn next_type<E: Describe<Remote>>(&mut self) -> Result<(), Self::Error> {
for corpus in E::BLUEPRINT {
self
.made
.push(format!("awaited:{}", corpus.slot().as_ref()));
}
Ok(())
}
}
impl Initialize for Corpora {
type Visitor = Maker;
fn initializer(_mount: Shared<Self>) -> Maker {
Maker::default()
}
}
impl AsyncInitialize for Remote {
type Visitor = Maker;
fn initializer(_mount: Shared<Self>) -> Maker {
Maker::default()
}
}
struct Media;
impl Describe<Corpora> for Media {
const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
SlotName::new("media__title__lexical"),
&[SlotColumn::text("title")],
)];
}
impl Describe<Remote> for Media {
const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
SlotName::new("media__embedding__remote"),
&[SlotColumn::text("embedding")],
)];
}
struct Caption;
impl Describe<Corpora> for Caption {
const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
SlotName::new("caption__text__lexical"),
&[SlotColumn::text("text")],
)];
}
#[tokio::test]
async fn the_mode_picks_the_face_and_both_are_driven_by_one_call_shape() {
let mut blocking = <Corpora as Initializes<Block>>::initializer(Shared::new(Corpora));
<Corpora as Initializes<Block>>::show::<Media>(&mut blocking)
.await
.expect("the blocking face refuses nothing");
assert_eq!(blocking.made, ["blocking:media__title__lexical"]);
let mut awaited = <Remote as Initializes<Async>>::initializer(Shared::new(Remote));
<Remote as Initializes<Async>>::show::<Media>(&mut awaited)
.await
.expect("the awaited face refuses nothing");
assert_eq!(awaited.made, ["awaited:media__embedding__remote"]);
}
#[tokio::test]
async fn a_mixed_declaration_is_visited_on_both_faces() {
let mut blocking = <Corpora as Initializes<Block>>::initializer(Shared::new(Corpora));
let mut awaited = <Remote as Initializes<Async>>::initializer(Shared::new(Remote));
<Corpora as Initializes<Block>>::show::<Media>(&mut blocking)
.await
.expect("made");
<Remote as Initializes<Async>>::show::<Media>(&mut awaited)
.await
.expect("made");
assert_eq!(
[blocking.made.as_slice(), awaited.made.as_slice()].concat(),
[
"blocking:media__title__lexical",
"awaited:media__embedding__remote"
],
"a mixed node's two sides are two steps, and both ran",
);
}
#[test]
fn the_bag_builds_one_machine_and_hands_it_back() {
let mut makers = Makers::default();
TypeVisitor::next_type::<Media>(makers.entry::<Maker>(Maker::default))
.expect("the first declaration is made");
TypeVisitor::next_type::<Caption>(makers.entry::<Maker>(|| panic!("a second machine was built")))
.expect("the second declaration is made");
assert_eq!(
makers.entry::<Maker>(Maker::default).made,
[
"blocking:media__title__lexical",
"blocking:caption__text__lexical"
],
"one machine per target, shown every declaration that lives there, in member order",
);
}
#[test]
fn the_bag_reports_its_size_and_not_its_machines() {
let mut makers = Makers::default();
let _ = makers.entry::<Maker>(Maker::default);
assert_eq!(format!("{makers:?}"), "Makers { built: 1 }");
}
#[test]
fn an_unbound_corpus_becomes_the_launch_floors_own_row() {
const UNBOUND: &[UnboundCorpus] = &[UnboundCorpus::new(
SlotName::new("bare__body__body"),
ResourceKind::Documents,
)];
let rows: Vec<SlotBinding> = UNBOUND.iter().map(SlotBinding::from).collect();
assert_eq!(
rows,
[SlotBinding::new(
"bare__body__body",
ResourceKind::Documents
)],
);
}
#[tokio::test]
async fn a_declaration_that_elects_nothing_takes_both_defaults() {
struct Bare;
impl super::Habitats for Bare {}
assert!(<Bare as super::Habitats>::UNBOUND.is_empty());
}