use quote::quote;
use crate::{
emit::{framework, leading, vendored},
model::{ChannelOptions, RecordModel, Upstream, default_channels_name},
};
impl RecordModel {
pub(super) fn channel_face(&self) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
let Some(subscription) = self.subscriptions.as_ref() else {
return Default::default();
};
let upstreams = subscription.upstreams.as_slice();
let shelf = default_channels_name(&self.record);
let types = self.declaration(&shelf, upstreams);
(types, self.shelf_doors(&shelf, upstreams))
}
fn declaration(&self, shelf: &syn::Ident, upstreams: &[Upstream]) -> proc_macro2::TokenStream {
let krate = &self.krate;
let deserialized = self.shelf_struct(shelf, upstreams, true);
let plain = self.shelf_struct(shelf, upstreams, false);
let if_serde = quote!(#krate::__private::gate::if_serde);
let if_no_serde = quote!(#krate::__private::gate::if_no_serde);
quote! {
#if_serde ! { #deserialized }
#if_no_serde ! { #plain }
}
}
fn shelf_struct(
&self,
shelf: &syn::Ident,
upstreams: &[Upstream],
deserialized: bool,
) -> proc_macro2::TokenStream {
let (record, vis) = (&self.record, &self.vis);
let options = self.framework(&["drive"], "ChannelOptions");
let fields = upstreams.iter().map(|upstream| {
let name = upstream.shelf();
let key = name.external().to_owned();
let doc = leading(&format!(
"What this deployment opens the `{}` edge as — `None` being the edge at whatever its \
declaration and the framework say.",
upstream.variant
));
let serde = deserialized.then(|| quote!(#[serde(default, rename = #key)]));
quote! {
#[doc = #doc]
#serde
#vis #name: ::core::option::Option<#options>,
}
});
let derives = if deserialized {
let deserialize = framework(&self.krate, &["serde"], "Deserialize");
let serde = vendored(&self.krate, "serde");
quote! {
#[derive(
::core::fmt::Debug,
::core::clone::Clone,
::core::marker::Copy,
::core::default::Default,
::core::cmp::PartialEq,
::core::cmp::Eq,
#deserialize
)]
#[serde(crate = #serde, deny_unknown_fields)]
}
} else {
quote! {
#[derive(
::core::fmt::Debug,
::core::clone::Clone,
::core::marker::Copy,
::core::default::Default,
::core::cmp::PartialEq,
::core::cmp::Eq
)]
}
};
let doc = leading(&format!(
"How ONE DEPLOYMENT opens [`{record}`]'s inbound edges — one field per declared upstream, \
absent for the ones it says nothing about.\n\nIt is the `channels` block of the node's own \
section, and it OVERRIDES: what it names wins over the author's declared default and over \
the framework's shipped constant, for the kind as readily as for the depth. A upstream it \
leaves unnamed is left to those layers.\n\nThe field names are read off the declaration — the \
upstream's own word, or the product's where `out = \"…\"` selected one — so a rename moves \
the document's key with the type it names, and a key naming no upstream of this node is \
refused where the file is open."
));
quote! {
#[doc = #doc]
#derives
#vis struct #shelf {
#(#fields)*
}
}
}
fn shelf_doors(&self, shelf: &syn::Ident, upstreams: &[Upstream]) -> proc_macro2::TokenStream {
let (record, vis) = (&self.record, &self.vis);
let arity = upstreams.len();
let options = self.framework(&["drive"], "ChannelOptions");
let shelf_trait = self.framework(&["drive"], "ChannelShelf");
let declared = upstreams.iter().map(|upstream| match upstream.channel {
Some(ChannelOptions::Bounded(capacity)) => {
quote!(::core::option::Option::Some(#options::Bounded(#capacity)))
}
Some(ChannelOptions::Unbounded) => {
quote!(::core::option::Option::Some(#options::Unbounded))
}
None => quote!(::core::option::Option::None),
});
let tuning = upstreams.iter().map(|upstream| {
let name = upstream.shelf();
quote!(self.#name)
});
let count_doc = leading(&format!("How many upstreams a [`{record}`] draws through."));
let declared_doc = leading(&format!(
"What [`{record}`]'s AUTHOR ships each of its edges as, in declaration order — `None` where \
the declaration wrote no `channel` word.\n\nThe middle layer of the precedence, and the one \
a deployment's document overrides. It is a constant rather than a default of the shelf \
because the two are different statements: this is what the DECLARATION says, and the shelf \
is what a deployment says."
));
quote! {
impl #shelf {
#[doc = #count_doc]
#vis const COUNT: usize = #arity;
#[doc = #declared_doc]
#vis const DECLARED: [::core::option::Option<#options>; #arity] = [#(#declared,)*];
}
#[automatically_derived]
impl #shelf_trait for #shelf {
#[inline]
fn tuning(&self) -> impl ::core::iter::Iterator<
Item = ::core::option::Option<#options>
> {
::core::iter::IntoIterator::into_iter([#(#tuning,)*])
}
}
}
}
}
#[cfg(test)]
mod tests;