use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use crate::{
catalog::{Catalog, ResourceField, ResourceSource, default_resources_name},
emit::{framework, leading, parameter},
graph::{FusedMember, OriginMount},
options::default_options_name,
};
#[cfg(test)]
mod goodbye_tests;
#[cfg(test)]
mod origin_tests;
#[cfg(test)]
mod tests;
#[must_use]
pub fn default_nodes_name(root: &syn::Ident) -> syn::Ident {
format_ident!("{root}Nodes")
}
#[derive(Debug, Clone, Copy)]
pub struct LaunchEntry<'a> {
pub krate: &'a syn::Path,
pub vis: &'a syn::Visibility,
pub root: &'a syn::Ident,
pub nodes: &'a [syn::Type],
pub mounts: &'a [syn::Ident],
pub resources: &'a [ResourceField],
pub fused: &'a [FusedMember],
pub origins: &'a [OriginMount],
}
impl LaunchEntry<'_> {
fn built_members(&self) -> impl Iterator<Item = (&syn::Ident, &syn::Type)> {
self.mounts.iter().zip(self.nodes).chain(
self
.origins
.iter()
.map(|origin| (&origin.mount, &origin.ty)),
)
}
fn built(&self, creation: &TokenStream, section: &TokenStream) -> Vec<TokenStream> {
let krate = self.krate;
let bare = framework(krate, &["drive"], "bare_options");
let bare_origin = framework(krate, &["drive"], "bare_origin_options");
let nodes = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let key = mount.to_string();
quote! {
__ingraph_nodes.#mount = #creation::node::<#node>(
&mut __ingraph_creation,
#key,
::core::option::Option::map(options.#mount, #section::unshelved),
#bare!(#node),
).await?;
}
});
let origins = self.origins.iter().map(|origin| {
let (mount, ty) = (&origin.mount, &origin.ty);
let key = mount.to_string();
quote! {
__ingraph_nodes.#mount = #creation::origin::<#ty>(
&mut __ingraph_creation,
#key,
::core::option::Option::map(options.#mount, #section::unshelved),
#bare_origin!(#ty),
).await?;
}
});
nodes.chain(origins).collect()
}
#[must_use]
pub fn emit(&self) -> TokenStream {
let bag = self.bag();
let takers = self.takers();
let debug = self.debug();
let entry = self.entry();
let converged = self.converged();
quote! {
#bag
#takers
#debug
#entry
#converged
}
}
fn bag(&self) -> TokenStream {
let vis = self.vis;
let root = self.root;
let name = default_nodes_name(root);
let fields = self.built_members().map(|(mount, node)| {
let doc = leading(&format!(
"The member this deployment built at `{mount}` — `None` where it does not launch one."
));
quote! {
#[doc = #doc]
pub(crate) #mount: ::core::option::Option<#node>,
}
});
let doc = leading(&format!(
"What one launch of [`{root}`] BUILT — one slot per declared node and one per declared \
ORIGIN, filled for the ones this deployment runs.\n\nIt is the create walk's product and the \
wiring stage's input: a member comes out of it once, by the mount name its section was \
written under, and goes into the resident that will run it — or, for an origin, onto the \
engine its own life runs on. A slot left `None` is presence-is-enablement arriving at the \
far end — nobody named that member, so nothing was built for it, and there is nothing to \
wire."
));
quote! {
#[doc = #doc]
#vis struct #name {
#(#fields)*
}
}
}
fn takers(&self) -> TokenStream {
let vis = self.vis;
let name = default_nodes_name(self.root);
let takers = self.built_members().map(|(mount, node)| {
let take = format_ident!("take_{mount}");
let doc = leading(&format!(
"Take the member built at `{mount}`, leaving the slot empty — `None` where this deployment \
does not launch it, and `None` again on a second ask."
));
quote! {
#[doc = #doc]
#[inline]
#vis fn #take(&mut self) -> ::core::option::Option<#node> {
::core::option::Option::take(&mut self.#mount)
}
}
});
quote! {
impl #name {
#(#takers)*
}
}
}
fn debug(&self) -> TokenStream {
let name = default_nodes_name(self.root);
let fields = self.built_members().map(|(mount, _ty)| {
let key = mount.to_string();
quote!(.field(#key, &::core::option::Option::is_some(&self.#mount)))
});
quote! {
impl ::core::fmt::Debug for #name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct(::core::stringify!(#name))
#(#fields)*
.finish()
}
}
}
}
fn catalog(&self) -> Catalog<'_> {
Catalog {
krate: self.krate,
vis: self.vis,
root: self.root,
resources: self.resources,
}
}
fn resource_pass(&self) -> TokenStream {
if self.resources.is_empty() {
return TokenStream::new();
}
let krate = self.krate;
let creation = framework(krate, &["drive"], "Creation");
let bare = framework(krate, &["drive"], "bare_resource_options");
let binding = |mount: &syn::Ident| format_ident!("__ingraph_resource_{mount}");
let catalog = self.catalog();
let taken = catalog.has_block().then(|| {
let block = default_resources_name(self.root);
let fields = catalog.sections().map(|field| {
let (mount, held) = (&field.mount, binding(&field.mount));
quote!(#mount: #held,)
});
quote! {
let #block { #(#fields)* } = options.resources;
}
});
let built = self
.resources
.iter()
.filter(|field| field.source.is_walked())
.map(|field| {
let ty = &field.ty;
let sourced = match &field.source {
ResourceSource::Section => {
let held = binding(&field.mount);
quote!(move || ::core::option::Option::or_else(#held, || #bare!(#ty)))
}
ResourceSource::Initializer(path) => quote!(|| ::core::option::Option::Some(#path())),
ResourceSource::Prepare => TokenStream::new(),
};
let door = if field.teardown {
quote!(declared_resource_saying_goodbye)
} else {
quote!(declared_resource)
};
quote! {
#creation::#door::<#ty, _>(&mut __ingraph_creation, #sourced).await?;
}
});
quote! {
#taken
#(#built)*
}
}
fn habitat_pass(&self) -> TokenStream {
if self.fused.is_empty() {
return TokenStream::new();
}
let krate = self.krate;
let habitats = framework(krate, &["drive"], "Habitats");
let steps = self.fused.iter().map(|member| {
let ty = &member.ty;
quote! {
<#ty as #habitats>::make(&mut __ingraph_creation).await?;
}
});
quote!(#(#steps)*)
}
fn binding_pass(&self, plan: &TokenStream) -> TokenStream {
if self.fused.is_empty() {
return TokenStream::new();
}
let krate = self.krate;
let habitats = framework(krate, &["drive"], "Habitats");
let rows = self.fused.iter().map(|member| {
let ty = &member.ty;
quote! {
for __ingraph_corpus in <#ty as #habitats>::UNBOUND {
__ingraph_plan = #plan::binding(
__ingraph_plan,
::core::convert::Into::into(__ingraph_corpus),
);
}
}
});
quote!(#(#rows)*)
}
fn mount_pass(&self, plan: &TokenStream) -> TokenStream {
if self.fused.is_empty() {
return TokenStream::new();
}
let krate = self.krate;
let habitats = framework(krate, &["drive"], "Habitats");
let rows = self.fused.iter().map(|member| {
let ty = &member.ty;
quote! {
for __ingraph_mount in <#ty as #habitats>::TARGETS {
__ingraph_plan = #plan::mounting(__ingraph_plan, __ingraph_mount());
}
}
});
quote!(#(#rows)*)
}
fn mutable_plan(&self) -> TokenStream {
if self.fused.is_empty() {
TokenStream::new()
} else {
quote!(mut)
}
}
fn empty_bag(&self) -> TokenStream {
let bag = default_nodes_name(self.root);
let slots = self
.built_members()
.map(|(mount, _ty)| quote!(#mount: ::core::option::Option::None,));
quote!(#bag { #(#slots)* })
}
fn mutable(&self) -> TokenStream {
if self.mounts.is_empty() && self.origins.is_empty() {
TokenStream::new()
} else {
quote!(mut)
}
}
fn mutable_staged(&self) -> TokenStream {
if self.mounts.is_empty() {
TokenStream::new()
} else {
quote!(mut)
}
}
fn staged_from_bag(&self) -> TokenStream {
let krate = self.krate;
let bag = default_nodes_name(self.root);
let staging = framework(krate, &["drive"], "Staging");
let taken = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let take = format_ident!("take_{mount}");
let key = mount.to_string();
quote! {
if let ::core::option::Option::Some(__ingraph_value) =
#bag::#take(&mut __ingraph_nodes)
{
#staging::declared::<#node>(&mut __ingraph_staged, #key, __ingraph_value);
}
}
});
let mutable = self.mutable_staged();
quote! {
{
let #mutable __ingraph_staged = #staging::new();
#(#taken)*
__ingraph_staged
}
}
}
fn staged_from_bodies(&self) -> TokenStream {
let krate = self.krate;
let bag = default_nodes_name(self.root);
let staging = framework(krate, &["drive"], "Staging");
let taken = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let mounted = self.mounted(node);
let body = format_ident!("__ingraph_body_{mount}");
let take = format_ident!("take_{mount}");
let key = mount.to_string();
quote! {
match ::core::option::Option::take(&mut #body) {
::core::option::Option::Some(__ingraph_body) => {
#mounted::stage(&mut __ingraph_staged, #key, __ingraph_body);
}
::core::option::Option::None => {
if let ::core::option::Option::Some(__ingraph_value) =
#bag::#take(&mut __ingraph_nodes)
{
#staging::declared::<#node>(&mut __ingraph_staged, #key, __ingraph_value);
}
}
}
}
});
let mutable = self.mutable_staged();
quote! {
{
let #mutable __ingraph_staged = #staging::new();
#(#taken)*
__ingraph_staged
}
}
}
fn entry(&self) -> TokenStream {
let krate = self.krate;
let vis = self.vis;
let root = self.root;
let options = default_options_name(root);
let bag = default_nodes_name(root);
let app = framework(krate, &["drive"], "App");
let constructor = framework(krate, &["drive"], "AsyncConstructor");
let creation = framework(krate, &["drive"], "Creation");
let ignition = framework(krate, &["drive"], "Ignition");
let launch_error = framework(krate, &["drive"], "LaunchError");
let plan = framework(krate, &["drive"], "Plan");
let wired = framework(krate, &["drive"], "Wired");
let wiring = framework(krate, &["drive"], "Wiring");
let registries = framework(krate, &["drive"], "Registries");
let prepare = parameter("PREPARE");
let wire = parameter("WIRE");
let errors = self.nodes.iter().map(|node| {
quote! {
<#node as #constructor>::Error:
::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
});
let lived = framework(krate, &["drive"], "Lived");
let declared_origin = framework(krate, &["source"], "DeclaredOrigin");
let died = framework(krate, &["drive"], "Died");
let origin_errors = self.origins.iter().map(|origin| {
let ty = &origin.ty;
quote! {
#ty: #declared_origin + #lived<<#ty as #declared_origin>::Mode>,
#died<#ty>:
::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
});
let resource = framework(krate, &["resource"], "AsyncResource");
let resource_errors = self.resources.iter().map(|field| {
let ty = &field.ty;
quote! {
<#ty as #resource>::Error:
::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
});
let resources = self.resource_pass();
let habitat = self.habitat_pass();
let bindings = self.binding_pass(&plan);
let mounts = self.mount_pass(&plan);
let (held, hold) = if self.fused.is_empty() {
(quote!(plan), TokenStream::new())
} else {
(
quote!(__ingraph_plan),
quote!(let mut __ingraph_plan = plan;),
)
};
let section = framework(krate, &["drive"], "NodeSection");
let built = self.built(&creation, §ion);
let empty = self.empty_bag();
let mutable = self.mutable();
let staged = self.staged_from_bag();
let origins = if self.origins.is_empty() {
String::new()
} else {
"\n\nAn `#[ingraph(origin)]` field is BUILT by this door and mounted by nobody: the \
arrangement is the caller's `plan` here, so the origin comes out in the node bag and the \
`wire` closure — which already names the operators' types — is what opens its edge and puts \
it on an engine. `launch` is the door that does the whole of it."
.to_owned()
};
let doc = leading(&format!(
"Launch this deployment of [`{root}`] — validate, create, wire, ignite.\n\n`options` is what \
this deployment RUNS and `plan` is what the declarations SAY: the entry replaces the plan's \
enablement with the options' own, so presence-is-enablement enters here and nowhere \
else.\n\n`prepare` drives the create walk's first two steps — the resources an enabled node \
demands, and the slots they open — because naming a resource type or a slot's declaration is \
something only the party that has them can do. It runs BEFORE any node is built, which is \
the ruled order: a construction reaching `ctx.shared::<R>()` finds what this put \
there.\n\n`wire` is handed the built nodes and the wiring walk, and gives back the ignition \
it filled. It spans the wiring and the engines because both name the operators' types, which \
an emission cannot. What it does NOT hold is the moment the sources start: this entry spends \
that, so SOURCES LAST stays a fact of the door rather than a rule a caller keeps.{origins}\n\n# \
Errors\n\nEvery refusal of the sequence, under one type: the validate walk's whole \
collected list, or the first failure of any walk after it — a resource that would not load, \
a node that would not build, an edge that would not connect, an engine that would not start."
));
let syncs = self.nodes.iter().map(|node| {
let mounted = self.mounted(node);
quote!(#mounted::SYNC)
});
let all_sync = leading(
"Whether EVERY node this root mounts is on the blocking face — the deployment's own \
syncness, folded from one row per mount, read through the MOUNT CELL that node's own two \
words selected.\n\nIt is a proof and not a switch: nothing in the launch reads it, and \
nothing can, because stable Rust cannot pick an impl by a const. What it is for is an \
ASSERTION — `const _: () = assert!(Root::ALL_SYNC);` in a deployment that means to have no \
awaited body in it, and a diagnostic anywhere the question is worth printing.\n\nIt reads \
BOTH PAIRS, which is why it is spelled through the cell: `Driven::SYNC` is the buffered \
pair's row and a `stream` declaration has not got one, so a fold spelled through that trait \
alone did not compile for a root with a streaming node in it. `Streams::SYNC` is the same \
fact one pair over.\n\nNo row of it can lie, and it takes two facts to say why. The const \
is set by the blanket rows and by nothing an author can write, so a node cannot state a \
syncness its face does not have; and the face itself is pinned against the words the \
declaration wrote by the `Mount` bound the launch spends beside it, so a mount whose body \
awaits and whose declaration stayed bare does not compile at all.",
);
quote! {
impl #root {
#[doc = #all_sync]
#vis const ALL_SYNC: bool = true #(&& #syncs)*;
#[doc = #doc]
#vis async fn launch_wired<#prepare, #wire>(
options: #options,
plan: #plan,
prepare: #prepare,
wire: #wire,
) -> ::core::result::Result<#app<Self>, #launch_error>
where
#prepare: ::core::ops::AsyncFnOnce(
&mut #creation<'_>,
) -> ::core::result::Result<(), #launch_error>,
#wire: ::core::ops::FnOnce(
#bag,
#wiring<'_>,
) -> ::core::result::Result<#ignition, #launch_error>,
#(#errors)*
#(#origin_errors)*
#(#resource_errors)*
{
let __ingraph_enablement = #options::enablement(&options);
let __ingraph_registries = ::std::sync::Arc::new(
#registries::new(::core::clone::Clone::clone(#options::registry(&options))),
);
#hold
#bindings
#mounts
let __ingraph_wired = #plan::validate(
#plan::with_resources(
#plan::with_enablement(#held, __ingraph_enablement),
Self::resources(),
),
)?;
let mut __ingraph_creation = #wired::create(&__ingraph_wired);
let #mutable __ingraph_nodes = #empty;
let __ingraph_created = async {
#resources
prepare(&mut __ingraph_creation).await?;
#habitat
#(#built)*
::core::result::Result::<(), #launch_error>::Ok(())
}.await;
if let ::core::result::Result::Err(__ingraph_error) = __ingraph_created {
return ::core::result::Result::Err(
#creation::unwind(__ingraph_creation, #staged, __ingraph_error),
);
}
let __ingraph_ignition = wire(
__ingraph_nodes,
{
let mut __ingraph_wiring = #creation::wire(__ingraph_creation);
#wiring::set_registries(&mut __ingraph_wiring, __ingraph_registries);
__ingraph_wiring
},
)?;
::core::result::Result::Ok(#ignition::into_app::<Self>(__ingraph_ignition))
}
}
}
}
}
impl LaunchEntry<'_> {
fn converged(&self) -> TokenStream {
let krate = self.krate;
let vis = self.vis;
let root = self.root;
let options = default_options_name(root);
let bag = default_nodes_name(root);
let app = framework(krate, &["drive"], "App");
let blueprint = framework(krate, &["drive"], "Blueprint");
let creation = framework(krate, &["drive"], "Creation");
let declared = framework(krate, &["drive"], "Declared");
let ignition = framework(krate, &["drive"], "Ignition");
let launch_error = framework(krate, &["drive"], "LaunchError");
let plan = framework(krate, &["drive"], "Plan");
let wired = framework(krate, &["drive"], "Wired");
let wiring = framework(krate, &["drive"], "Wiring");
let registries = framework(krate, &["drive"], "Registries");
let produced_of = framework(krate, &["drive"], "Produced");
let prepare = parameter("PREPARE");
let predicates = self.predicates();
let resources = self.resource_pass();
let habitat = self.habitat_pass();
let bindings = self.binding_pass(&plan);
let mounts = self.mount_pass(&plan);
let mutable_plan = self.mutable_plan();
let handles = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let mounted = self.mounted(node);
let handle = format_ident!("__ingraph_at_{mount}");
let key = mount.to_string();
quote! {
let #handle = #mounted::declare(&mut __ingraph_blueprint, #key);
}
});
let origin_handles = self.origins.iter().map(|origin| {
let (mount, ty) = (&origin.mount, &origin.ty);
let handle = format_ident!("__ingraph_at_{mount}");
let key = mount.to_string();
quote! {
let #handle = #blueprint::origin::<#ty>(&mut __ingraph_blueprint, #key);
}
});
let section = framework(krate, &["drive"], "NodeSection");
let seats = self.mounts.iter().map(|mount| {
let seat = format_ident!("__ingraph_seats_{mount}");
quote! {
let #seat = ::core::option::Option::map(
::core::option::Option::and_then(
::core::option::Option::as_ref(&options.#mount),
#section::stream,
),
::core::clone::Clone::clone,
);
}
});
let channels = framework(krate, &["drive"], "Channels");
let tuned = self.mounts.iter().map(|mount| {
let key = mount.to_string();
quote!(.mounted(#key, ::core::option::Option::as_ref(&options.#mount)))
});
let built = self.built(&creation, §ion);
let empty = self.empty_bag();
let mutable = self.mutable();
let staged_bag = self.staged_from_bag();
let staged_bodies = self.staged_from_bodies();
let holders = self.mounts.iter().map(|mount| {
let body = format_ident!("__ingraph_body_{mount}");
let feed = format_ident!("__ingraph_feed_{mount}");
let intake = format_ident!("__ingraph_intake_{mount}");
quote! {
let mut #body = ::core::option::Option::None;
let mut #feed = ::core::option::Option::None;
let mut #intake = ::core::option::Option::None;
}
});
let origin_holders = self.origins.iter().map(|origin| {
let mount = &origin.mount;
let bridge = format_ident!("__ingraph_bridge_{mount}");
quote!(let mut #bridge = ::core::option::Option::None;)
});
let intakes = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let mounted = self.mounted(node);
let handle = format_ident!("__ingraph_at_{mount}");
let intake = format_ident!("__ingraph_intake_{mount}");
quote! {
#intake = #mounted::intake(&mut __ingraph_wiring, #handle)?;
}
});
let wire = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let mounted = self.mounted(node);
let handle = format_ident!("__ingraph_at_{mount}");
let body = format_ident!("__ingraph_body_{mount}");
let feed = format_ident!("__ingraph_feed_{mount}");
let intake = format_ident!("__ingraph_intake_{mount}");
let seat = format_ident!("__ingraph_seats_{mount}");
let take = format_ident!("take_{mount}");
quote! {
if let ::core::option::Option::Some(__ingraph_operator) = #bag::#take(&mut __ingraph_nodes) {
if let ::core::option::Option::Some(__ingraph_intake) =
::core::option::Option::take(&mut #intake)
{
if <#node as #declared>::UPSTREAMS.is_empty() {
#feed = #wiring::outbox::<#mounted::In>(
&mut __ingraph_wiring,
#handle,
)?;
}
#body = #mounted::wire(
&mut __ingraph_wiring,
#handle,
__ingraph_operator,
__ingraph_intake,
#seat,
)?;
}
}
}
});
let bridges = self.origins.iter().map(|origin| {
let (mount, ty) = (&origin.mount, &origin.ty);
let handle = format_ident!("__ingraph_at_{mount}");
let bridge = format_ident!("__ingraph_bridge_{mount}");
let take = format_ident!("take_{mount}");
let key = mount.to_string();
let produced = quote!(#produced_of<#ty>);
quote! {
if let ::core::option::Option::Some(__ingraph_consumer) =
#wiring::bridge::<#produced>(&mut __ingraph_wiring, #handle)?
{
if let ::core::option::Option::Some(__ingraph_origin) =
#bag::#take(&mut __ingraph_nodes)
{
#bridge = ::core::option::Option::Some((
__ingraph_origin,
__ingraph_consumer,
#wiring::enrol(&mut __ingraph_wiring, #key),
));
}
}
}
});
let ignite = self.mounts.iter().zip(self.nodes).map(|(mount, node)| {
let mounted = self.mounted(node);
let body = format_ident!("__ingraph_body_{mount}");
let feed = format_ident!("__ingraph_feed_{mount}");
quote! {
if let ::core::option::Option::Some(__ingraph_feeder) = #feed {
#mounted::feeder(&mut __ingraph_ignition, __ingraph_feeder);
}
if let ::core::option::Option::Some(__ingraph_body) = #body {
#mounted::ignite(&mut __ingraph_ignition, __ingraph_body)?;
}
}
});
let origin_ignite = self.origins.iter().map(|origin| {
let (mount, ty) = (&origin.mount, &origin.ty);
let bridge = format_ident!("__ingraph_bridge_{mount}");
let key = mount.to_string();
quote! {
if let ::core::option::Option::Some(
(__ingraph_origin, __ingraph_consumer, __ingraph_party)
) = #bridge {
#ignition::origin_kept::<#ty>(
&mut __ingraph_ignition,
#key,
__ingraph_origin,
__ingraph_consumer,
#wiring::DOOR_CAPACITY,
__ingraph_party,
)?;
}
}
});
let origins = if self.origins.is_empty() {
String::new()
} else {
"\n\nAn `#[ingraph(origin)]` field is mounted here too, and is mounted LAST: a source is a \
HEAD the graph does not feed, so what it gets is an engine of its own and its one out-edge \
as its feed. See `App::origin` for the two ends this world then keeps — and note that it \
KEEPS them, because letting go of that handle is telling the origin to wind down."
.to_owned()
};
let plain_doc = leading(&format!(
"**Launch this deployment of [`{root}`] — and wire NOTHING by hand.**\n\nThe whole ruled \
sequence from one value: the arrangement is joined from what each declared node states about \
itself, the deployment's own sections decide who runs, the resources an enabled node demands \
are built before the first construction, the edges are opened and the engines \
started.{origins}\n\nWhat \
comes back is the running world, with an intake door onto every head — see [`App::feeder`]. \
Use [`launch_with`](Self::launch_with) where a `factory` resource's options are the \
application's own to hand over.\n\n# Errors\n\nEvery refusal of the sequence, under one type: \
an upstream this root does not mount, the validate walk's whole collected list, or the first \
failure of any walk after it."
));
let hooked_doc = leading(&format!(
"Launch this deployment of [`{root}`], giving the create walk the application's own \
hand.\n\n[`launch`](Self::launch)'s walk exactly, with `prepare` run after the declared \
resources are built and BEFORE the first node — which is the ruled order, and what makes a \
construction reaching `ctx.shared::<R>()` find what this put there. It is where a bare \
`#[ingraph(resource(factory))]` gets its options, because those close over a value the \
application holds and no declaration can state one.\n\n# Errors\n\nSee \
[`launch`](Self::launch)."
));
quote! {
impl #root {
#[doc = #plain_doc]
#vis async fn launch(
options: #options,
) -> ::core::result::Result<#app<Self>, #launch_error>
where
#predicates
{
Self::launch_with(options, async |_| ::core::result::Result::Ok(())).await
}
#[doc = #hooked_doc]
#vis async fn launch_with<#prepare>(
options: #options,
prepare: #prepare,
) -> ::core::result::Result<#app<Self>, #launch_error>
where
#prepare: ::core::ops::AsyncFnOnce(
&mut #creation<'_>,
) -> ::core::result::Result<(), #launch_error>,
#predicates
{
let mut __ingraph_blueprint = #blueprint::new();
#(#handles)*
#(#origin_handles)*
let #mutable_plan __ingraph_plan = #blueprint::into_plan(__ingraph_blueprint)?;
#bindings
#mounts
let __ingraph_enablement = #options::enablement(&options);
let __ingraph_registries = ::std::sync::Arc::new(
#registries::new(::core::clone::Clone::clone(#options::registry(&options))),
);
let __ingraph_channels = #channels::new() #(#tuned)*;
#(#seats)*
let __ingraph_wired = #plan::validate(
#plan::with_channels(
#plan::with_resources(
#plan::with_enablement(__ingraph_plan, __ingraph_enablement),
Self::resources(),
),
__ingraph_channels,
),
)?;
let mut __ingraph_creation = #wired::create(&__ingraph_wired);
let #mutable __ingraph_nodes = #empty;
let __ingraph_created = async {
#resources
prepare(&mut __ingraph_creation).await?;
#habitat
#(#built)*
::core::result::Result::<(), #launch_error>::Ok(())
}.await;
if let ::core::result::Result::Err(__ingraph_error) = __ingraph_created {
return ::core::result::Result::Err(
#creation::unwind(__ingraph_creation, #staged_bag, __ingraph_error),
);
}
let mut __ingraph_wiring = #creation::wire(__ingraph_creation);
#wiring::set_registries(&mut __ingraph_wiring, __ingraph_registries);
#(#holders)*
#(#origin_holders)*
let __ingraph_connected = (|| -> ::core::result::Result<(), #launch_error> {
#(#intakes)*
#(#wire)*
#(#bridges)*
::core::result::Result::Ok(())
})();
if let ::core::result::Result::Err(__ingraph_error) = __ingraph_connected {
return ::core::result::Result::Err(
#wiring::unwind(__ingraph_wiring, #staged_bodies, __ingraph_error),
);
}
let mut __ingraph_ignition = #wiring::into_assembly(__ingraph_wiring).ignite();
#(#ignite)*
#(#origin_ignite)*
::core::result::Result::Ok(#ignition::into_app::<Self>(__ingraph_ignition))
}
}
}
}
fn cell(&self, node: &syn::Type) -> TokenStream {
let krate = self.krate;
let declared = framework(krate, &["drive"], "Declared");
let worn = framework(krate, &["drive"], "Worn");
quote!(#worn<<#node as #declared>::Pair, <#node as #declared>::Mode>)
}
fn mounted(&self, node: &syn::Type) -> TokenStream {
let krate = self.krate;
let mount = framework(krate, &["drive"], "Mount");
let cell = self.cell(node);
quote!(<#cell as #mount<#node>>)
}
fn predicates(&self) -> TokenStream {
let krate = self.krate;
let constructor = framework(krate, &["drive"], "AsyncConstructor");
let declared = framework(krate, &["drive"], "Declared");
let mount = framework(krate, &["drive"], "Mount");
let resource = framework(krate, &["resource"], "AsyncResource");
let nodes = self.nodes.iter().map(|node| {
let cell = self.cell(node);
quote! {
#node: #declared
+ #constructor<Options = <#node as #declared>::Options>
+ ::core::marker::Send
+ 'static,
#cell: #mount<#node>,
<#node as #constructor>::Error:
::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
});
let resources = self.resources.iter().map(|field| {
let ty = &field.ty;
quote! {
<#ty as #resource>::Error:
::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
});
let origins = self
.origins
.iter()
.map(|origin| self.origin_predicate(origin));
quote!(#(#nodes)* #(#resources)* #(#origins)*)
}
fn origin_predicate(&self, origin: &OriginMount) -> TokenStream {
let krate = self.krate;
let ty = &origin.ty;
let declared_origin = framework(krate, &["source"], "DeclaredOrigin");
let lived = framework(krate, &["drive"], "Lived");
let produced = framework(krate, &["drive"], "Produced");
let ordered = framework(krate, &["drive"], "Ordered");
let died = framework(krate, &["drive"], "Died");
quote! {
#ty: #declared_origin
+ #lived<<#ty as #declared_origin>::Mode>
+ ::core::marker::Send
+ 'static,
#produced<#ty>: ::core::marker::Send,
#ordered<#ty>: ::core::marker::Send,
#died<#ty>: ::core::error::Error + ::core::marker::Send + ::core::marker::Sync + 'static,
}
}
}