use derives::DerivesRegistry;
use proc_macro2::Ident;
use quote::{quote, ToTokens};
use substitutes::TypeSubstitutes;
use syn::parse_quote;
use self::substitutes::absolute_path;
pub mod derives;
pub mod substitutes;
#[derive(Debug, Clone)]
pub struct TypeGeneratorSettings {
pub types_mod_ident: Ident,
pub should_gen_docs: bool,
pub derives: DerivesRegistry,
pub substitutes: TypeSubstitutes,
pub decoded_bits_type_path: Option<syn::Path>,
pub compact_as_type_path: Option<syn::Path>,
pub compact_type_path: Option<syn::Path>,
pub insert_codec_attributes: bool,
pub alloc_crate_path: AllocCratePath,
}
#[derive(Debug, Clone, Default)]
pub enum AllocCratePath {
#[default]
Std,
Custom(syn::Path),
}
impl ToTokens for AllocCratePath {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
AllocCratePath::Std => quote!(::std).to_tokens(tokens),
AllocCratePath::Custom(alloc_path) => alloc_path.to_tokens(tokens),
}
}
}
impl Default for TypeGeneratorSettings {
fn default() -> Self {
Self {
types_mod_ident: parse_quote!(types),
should_gen_docs: true,
substitutes: TypeSubstitutes::new(),
derives: DerivesRegistry::new(),
decoded_bits_type_path: None,
compact_as_type_path: None,
compact_type_path: None,
insert_codec_attributes: false,
alloc_crate_path: Default::default(),
}
}
}
impl TypeGeneratorSettings {
pub fn new() -> Self {
Self::default()
}
pub fn type_mod_name(mut self, type_mod_name: &str) -> Self {
self.types_mod_ident =
syn::parse_str(type_mod_name).expect("The provided type_mod_name is not a valid ident");
self
}
pub fn substitute(mut self, from: syn::Path, to: syn::Path) -> Self {
self.substitutes
.insert(from, absolute_path(to).unwrap())
.unwrap();
self
}
pub fn compact_as_type_path(mut self, path: syn::Path) -> Self {
self.compact_as_type_path = Some(path);
self
}
pub fn compact_type_path(mut self, path: syn::Path) -> Self {
self.compact_type_path = Some(path);
self
}
pub fn decoded_bits_type_path(mut self, path: syn::Path) -> Self {
self.decoded_bits_type_path = Some(path);
self
}
pub fn should_gen_docs(mut self, should_gen_docs: bool) -> Self {
self.should_gen_docs = should_gen_docs;
self
}
pub fn insert_codec_attributes(mut self) -> Self {
self.insert_codec_attributes = true;
self
}
pub fn add_derives_for_all(
mut self,
derive_paths: impl IntoIterator<Item = syn::Path>,
) -> Self {
self.derives.add_derives_for_all(derive_paths);
self
}
}