use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::ext::IdentExt as _;
use crate::model::GraphqlKind;
mod face;
#[cfg(test)]
mod tests;
mod wire;
#[derive(Debug, Clone)]
pub struct MutationModel {
pub krate: syn::Path,
pub name: syn::Ident,
pub vis: syn::Visibility,
pub graphql: Vec<GraphqlKind>,
pub exchanges: Vec<ExchangeModel>,
}
#[derive(Debug, Clone)]
pub struct ExchangeModel {
pub field: syn::Ident,
pub ty: syn::Type,
pub rename: Option<syn::LitStr>,
pub handler: HandlerModel,
}
#[derive(Debug, Clone)]
pub struct HandlerModel {
pub path: syn::Path,
pub awaited: bool,
pub context: Vec<syn::Path>,
pub error: Option<DeclaredError>,
}
#[derive(Debug, Clone)]
pub enum DeclaredError {
Rich {
ty: syn::Path,
code: Option<syn::LitStr>,
},
Canned {
message: syn::LitStr,
code: Option<syn::LitStr>,
},
}
impl MutationModel {
#[must_use]
pub fn command_name(&self) -> syn::Ident {
format_ident!("{}Command", self.name)
}
#[must_use]
pub fn all_sync(&self) -> bool {
!self
.exchanges
.iter()
.any(|exchange| exchange.handler.awaited)
}
#[must_use]
pub fn emit(&self) -> TokenStream {
let command = self.command();
let node = self.node();
let declared = self.declared();
let face = self.assembly();
let wire = self.wire();
quote! {
#command
#node
#declared
#face
#wire
}
}
fn command(&self) -> TokenStream {
let krate = &self.krate;
let vis = &self.vis;
let name = self.command_name();
let arms = self.exchanges.iter().map(|exchange| {
let variant = exchange.variant();
let ty = &exchange.ty;
let doc = format!(
"What an app says to run the `{field}` exchange.",
field = exchange.named(),
);
quote! {
#[doc = #doc]
#variant(<#ty as #krate::__private::source::Mutation>::Request)
}
});
let doc = format!(
"The command vocabulary of [`{node}`] — one arm per exchange it declared.",
node = self.name,
);
quote! {
#[doc = #doc]
#vis enum #name {
#(#arms),*
}
}
}
fn node(&self) -> TokenStream {
let krate = &self.krate;
let name = &self.name;
let command = self.command_name();
quote! {
impl #krate::__private::source::MutationNode for #name {
type Command = #command;
}
}
}
fn declared(&self) -> TokenStream {
let krate = &self.krate;
let name = &self.name;
let all_sync = self.all_sync();
let rows = self.exchanges.iter().map(|exchange| exchange.row(krate));
quote! {
impl #krate::__private::source::DeclaredMutation for #name {
const ALL_SYNC: bool = #all_sync;
const EXCHANGES: &'static [#krate::__private::source::Exchange] = &[
#(#rows),*
];
}
}
}
}
impl ExchangeModel {
#[must_use]
pub fn variant(&self) -> syn::Ident {
use heck::ToUpperCamelCase as _;
format_ident!("{}", self.named().to_upper_camel_case())
}
#[must_use]
pub fn named(&self) -> String {
self.field.unraw().to_string()
}
fn row(&self, krate: &syn::Path) -> TokenStream {
let field = self.named();
let handler = path_word(&self.handler.path);
let mut row = quote!(#krate::__private::source::Exchange::of(#field, #handler));
if let Some(rename) = &self.rename {
row = quote!(#row.renamed(#rename));
}
if self.handler.awaited {
row = quote!(#row.awaited());
}
if !self.handler.context.is_empty() {
let granted = self.handler.context.iter().map(path_word);
row = quote!(#row.granting(&[#(#granted),*]));
}
if let Some(error) = &self.handler.error {
let fallibility = error.emit(krate);
row = quote!(#row.failing(#fallibility));
}
row
}
}
impl DeclaredError {
fn emit(&self, krate: &syn::Path) -> TokenStream {
match self {
Self::Rich { ty, code } => {
let named = path_word(ty);
let code = optional(code.as_ref());
quote!(#krate::__private::source::Fallibility::Rich { named: #named, code: #code })
}
Self::Canned { message, code } => {
let code = optional(code.as_ref());
quote!(#krate::__private::source::Fallibility::Canned { message: #message, code: #code })
}
}
}
}
fn path_word(path: &syn::Path) -> String {
quote!(#path).to_string().replace(' ', "")
}
fn optional(word: Option<&syn::LitStr>) -> TokenStream {
match word {
Some(word) => quote!(::core::option::Option::Some(#word)),
None => quote!(::core::option::Option::None),
}
}