use quote::quote;
use crate::{
emit::leading,
model::{
ChangesFeed, ChannelOptions, RecordModel, Upstream, Upstreams, default_channels_name,
default_downstream_name,
},
};
#[cfg(test)]
mod tests;
impl RecordModel {
pub(super) fn declared(&self) -> proc_macro2::TokenStream {
let record = &self.record;
let declared = self.framework(&["drive"], "Declared");
let effect = self.framework(&["drive"], "Effect");
let upstream = self.framework(&["drive"], "Upstream");
let grant = self.framework(&["drive"], "Grant");
let (arm, lane) = if self.kind.stores() {
(quote!(Lands), quote!(true))
} else {
(quote!(Pure), quote!(false))
};
let upstreams = self.declared_sources();
let options = self
.options
.as_ref()
.map_or_else(|| quote!(()), |path| quote!(#path));
let channels = self.subscriptions.as_ref().map_or_else(
|| {
let unchanneled = self.framework(&["drive"], "Unchanneled");
quote!(#unchanneled)
},
|_| {
let shelf = default_channels_name(&self.record);
quote!(#shelf)
},
);
let effect_doc = leading(&format!(
"What a [`{record}`]'s outputs become — read off the kind it was declared under."
));
let lane_doc = leading(&format!(
"Whether a [`{record}`] holds a write lane of its own — read off the same kind."
));
let sources_doc = leading(&format!(
"Every upstream a [`{record}`] draws from, in declaration order."
));
let grants_doc = leading(&format!(
"Every store a [`{record}`] granted itself the right to write, in declaration order."
));
let grants = self.declared_grants();
let goodbye = self.declared_goodbye();
let mode = self.declared_mode();
let pair = self.declared_pair();
let wires = self.declared_wires();
let attribution = self.declared_attribution();
let tracing = self.declared_tracing();
quote! {
#[automatically_derived]
impl #declared for #record {
#[doc = #effect_doc]
const EFFECT: #effect = #effect::#arm;
#[doc = #lane_doc]
const HOLDS_LANE: bool = #lane;
#[doc = #sources_doc]
const UPSTREAMS: &'static [#upstream] = &[#(#upstreams,)*];
#[doc = #grants_doc]
const GRANTS: &'static [#grant] = &[#(#grants,)*];
type Channels = #channels;
type Options = #options;
#pair
#wires
#attribution
#mode
#goodbye
#tracing
}
}
}
pub(super) fn receipts(&self) -> Option<proc_macro2::TokenStream> {
let receipt = self.receipt.as_ref()?;
let record = &self.record;
let receipts = self.framework(&["operator"], "Receipts");
let doc = leading(&format!(
"A [`{record}`] wrote `receipt = \"…\"`, so an acked give OF ONE is answered with this type — \
what a consumer states through `Emitter::receipt_of::<{record}>` and what the producer's \
`Acked::receipt` reads back, neither of them naming it twice."
));
Some(quote! {
#[automatically_derived]
impl #receipts for #record {
#[doc = #doc]
type Receipt = #receipt;
}
})
}
fn declared_mode(&self) -> proc_macro2::TokenStream {
let record = &self.record;
let (verb, blocking, awaited, row) = if self.streaming.is_some() {
(
"run",
"a `StreamOperator`",
"an `AsyncStreamOperator`",
"Streams",
)
} else if self.autonomous.is_some() {
(
"run",
"an `AutonomousOperator`",
"an `AsyncAutonomousOperator`",
"Runs",
)
} else {
("accept", "an `Operator`", "an `AsyncOperator`", "Driven")
};
if self.asynchronous.is_none() {
let doc = leading(&format!(
"A [`{record}`] wrote no mode word, so its `{verb}` is the blocking face's — {blocking} \
body, called by either engine and answering without a future."
));
let block = self.framework(&["operator"], "Block");
return quote! {
#[doc = #doc]
type Mode = #block;
};
}
let mode_doc = leading(&format!(
"A [`{record}`] wrote `async`, so its `{verb}` is the awaited face's — {awaited} body, \
driven by `block_on` on the thread engine and awaited on the async one. A root folding its \
mounts' syncness folds the `false` this mode's `{row}` row carries."
));
let asynchronous = self.framework(&["operator"], "Async");
quote! {
#[doc = #mode_doc]
type Mode = #asynchronous;
}
}
fn declared_pair(&self) -> proc_macro2::TokenStream {
let record = &self.record;
if self.autonomous.is_some() {
let doc = leading(&format!(
"A [`{record}`] wrote `autonomous`, so its body wears the AUTONOMOUS kind — the node owns \
its own loop over its inbound edges, its downstream set and its stop, and what mounts it \
is `Blueprint::autonomous` rather than `Blueprint::node`."
));
let autonomous = self.framework(&["drive"], "Autonomous");
return quote! {
#[doc = #doc]
type Pair = #autonomous;
};
}
if self.streaming.is_none() {
let doc = leading(&format!(
"A [`{record}`] wrote no `stream`, so its body wears the BUFFERED pair — an `accept` per \
delivery, mounted at `Blueprint::node`."
));
let buffered = self.framework(&["drive"], "Buffered");
return quote! {
#[doc = #doc]
type Pair = #buffered;
};
}
let doc = leading(&format!(
"A [`{record}`] wrote `stream`, so its body wears the STREAMING pair — one delivery is a \
whole life, streamed out through a bounded `Feed`, and what mounts it is `Blueprint::stream` \
rather than `Blueprint::node`."
));
let streaming = self.framework(&["drive"], "Streaming");
quote! {
#[doc = #doc]
type Pair = #streaming;
}
}
fn declared_wires(&self) -> proc_macro2::TokenStream {
let record = &self.record;
if self.products.as_ref().is_some_and(|p| p.is_sum()) {
let set = default_downstream_name(record);
let doc = leading(&format!(
"A [`{record}`] wrote `emits = […]`, so it publishes through its own [`{set}`] — one edge \
list per declared product, each consumer on the edge it opened, and as many per product as \
subscribed. The product parameter is the ONE-product road's and is unread here: this set's \
slots are typed at the products the word named."
));
return quote! {
#[doc = #doc]
type Wires<__INGRAPH_OUT__> = #set;
};
}
let downstream = self.framework(&["drive"], "Downstream");
let doc = leading(&format!(
"A [`{record}`] emits ONE thing, so it publishes through the framework's own fan-out set — N \
edges of that one product, the copy taken at the set rather than asked of any driver."
));
quote! {
#[doc = #doc]
type Wires<__INGRAPH_OUT__> = #downstream<__INGRAPH_OUT__>;
}
}
fn declared_attribution(&self) -> proc_macro2::TokenStream {
let record = &self.record;
if !self.attributed {
let doc = leading(&format!(
"A [`{record}`] wrote no `attributed`, so it attributes its outputs to nothing — the unit \
projection, whose `Attributes` impl is the empty program and whose value is zero-sized. \
Its observations stay counter-grade, which is the shipped posture for every node that \
emits a payload with no declared root. The product parameter is the attributed road's and \
is unread here."
));
return quote! {
#[doc = #doc]
type Attribution<__INGRAPH_OUT__> = ();
};
}
let projection = self.framework(&["drive"], "RootProjection");
let doc = leading(&format!(
"A [`{record}`] wrote `attributed`, so the launch installs the framework's root projection \
over whatever this node emits — the key's FIRST column, read off the `Attributes` impl that \
output's own declaration emitted. An output no record declared has no such impl, and the \
mount is where that is refused."
));
quote! {
#[doc = #doc]
type Attribution<__INGRAPH_OUT__> = #projection<__INGRAPH_OUT__>;
}
}
fn declared_tracing(&self) -> Option<proc_macro2::TokenStream> {
let level = self.tracing?;
let record = &self.record;
let gate = self.framework(&["gate"], "if_tracing");
let ty = self.framework(&["drive"], "TracingLevel");
let variant = level.variant();
let doc = leading(&format!(
"A [`{record}`] wrote `tracing(level = {word})`, so its framework spans open at that level.",
word = level.as_str(),
));
Some(quote! {
#gate! {
#[doc = #doc]
const TRACING: #ty = #ty::#variant;
}
})
}
fn declared_goodbye(&self) -> Option<proc_macro2::TokenStream> {
if !self.teardown {
return None;
}
let record = &self.record;
let goodbye = self.framework(&["drive"], "Goodbye");
let teardown = self.framework(&["drive"], "AsyncTeardown");
let fact_doc = leading(&format!(
"A [`{record}`] wrote `teardown`, so its value is enrolled in the shutdown's goodbye chain."
));
let goodbye_doc = leading(&format!(
"A [`{record}`]'s own `AsyncTeardown` impl, boxed for the chain to drive."
));
Some(quote! {
#[doc = #fact_doc]
const TEARDOWN: bool = true;
#[doc = #goodbye_doc]
#[inline]
fn goodbye(self) -> #goodbye {
::std::boxed::Box::pin(<#record as #teardown>::teardown(self))
}
})
}
fn declared_sources(&self) -> Vec<proc_macro2::TokenStream> {
if let Some(subscription) = self.subscriptions.as_ref() {
return subscription
.upstreams
.iter()
.map(|upstream| self.declared_source(upstream))
.collect();
}
self
.subscribe
.iter()
.chain(self.upstreams.iter().flat_map(Upstreams::upstreams))
.map(|node| self.bare_source(node))
.collect()
}
fn declared_source(&self, upstream: &Upstream) -> proc_macro2::TokenStream {
let listen = upstream.changes.as_ref().map(|feed| self.changes(feed));
let channel = upstream.channel.map(|spec| self.channelled(spec));
let bare = self.bare_source(&upstream.node);
quote!(#bare #listen #channel)
}
fn bare_source(&self, node: &syn::Path) -> proc_macro2::TokenStream {
let upstream = self.framework(&["drive"], "Upstream");
let named = quote!(#node).to_string();
quote!(#upstream::of::<#node>(#named))
}
fn changes(&self, feed: &ChangesFeed) -> proc_macro2::TokenStream {
let listen = self.framework(&["drive"], "Listen");
let feeder = self.framework(&["drive"], "Feeder");
let arm = if feed.external {
quote!(External)
} else {
quote!(InGraph)
};
quote!(.listening(#listen::Changes(#feeder::#arm)))
}
fn channelled(&self, spec: ChannelOptions) -> proc_macro2::TokenStream {
let channel = self.framework(&["drive"], "ChannelOptions");
let arm = match spec {
ChannelOptions::Bounded(capacity) => quote!(#channel::Bounded(#capacity)),
ChannelOptions::Unbounded => quote!(#channel::Unbounded),
};
quote!(.channelled(#arm))
}
}