scale_typegen/typegen/settings/
mod.rs

1use derives::DerivesRegistry;
2use proc_macro2::Ident;
3use quote::{quote, ToTokens};
4use substitutes::TypeSubstitutes;
5use syn::parse_quote;
6
7use self::substitutes::absolute_path;
8
9/// Settings for which derives should be applied on types
10pub mod derives;
11/// Settings for which types should be substituted by other types.
12pub mod substitutes;
13
14/// A struct containing all the settings for generating rust types from a type registry.
15#[derive(Debug, Clone)]
16pub struct TypeGeneratorSettings {
17    /// The name of the module which will contain the generated types.
18    pub types_mod_ident: Ident,
19    /// If false, no docs are generated for the types.
20    pub should_gen_docs: bool,
21    /// Derive traits on generated types.
22    pub derives: DerivesRegistry,
23    /// User defined overrides for generated types.
24    pub substitutes: TypeSubstitutes,
25    /// Two generic parameters are expected on this type:
26    /// - Store (e.g. `u8`/`u16`/`u32`/`u64`)
27    /// - Order (e.g. LSB, MSB)
28    ///
29    /// subxt provides a `subxt::utils::DecodedBits` that can be used here.
30    pub decoded_bits_type_path: Option<syn::Path>,
31    /// TypePath to the CompactAs trait/derive macro.
32    /// E.g. `subxt::ext::codec::CompactAs`
33    pub compact_as_type_path: Option<syn::Path>,
34    /// TypePath to the Compact<T> struct.
35    /// E.g. `subxt::ext::codec::Compact`
36    pub compact_type_path: Option<syn::Path>,
37    /// If false, no codec attributes like `codec(index=0)` and `codec(compact)` are inserted.
38    /// This is a useful option if we do not want to derive Decode and Encode on our types.
39    pub insert_codec_attributes: bool,
40    /// Configure a custom type path for the `alloc` crate, which is the base for generating type paths like
41    /// `alloc::string::String`, `alloc::vec::Vec` and `alloc::boxed::Box`. The default is `AllocCratePath::Std` which
42    /// uses the types from the `std` library instead.
43    pub alloc_crate_path: AllocCratePath,
44}
45
46/// Information about how to construct the type paths for types that need allocation, e.g.
47#[derive(Debug, Clone, Default)]
48pub enum AllocCratePath {
49    /// Equivalent to `AllocCratePath::Custom(quote!(::std))`. This is the default.
50    #[default]
51    Std,
52    /// Custom path to the alloc crate, e.g. `::alloc` if `extern crate alloc;` is used
53    /// or e.g. `::subxt_core::alloc` if the alloc crate is exported from another crate.
54    ///
55    /// We'd expect the Custom path to export `vec::Vec`, `string::String`, `boxed::Box`
56    /// and `collections::BTreeMap`.
57    Custom(syn::Path),
58}
59
60impl ToTokens for AllocCratePath {
61    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
62        match self {
63            AllocCratePath::Std => quote!(::std).to_tokens(tokens),
64            AllocCratePath::Custom(alloc_path) => alloc_path.to_tokens(tokens),
65        }
66    }
67}
68
69impl Default for TypeGeneratorSettings {
70    fn default() -> Self {
71        Self {
72            types_mod_ident: parse_quote!(types),
73            should_gen_docs: true,
74            substitutes: TypeSubstitutes::new(),
75            derives: DerivesRegistry::new(),
76            decoded_bits_type_path: None,
77            compact_as_type_path: None,
78            compact_type_path: None,
79            insert_codec_attributes: false,
80            alloc_crate_path: Default::default(),
81        }
82    }
83}
84
85impl TypeGeneratorSettings {
86    /// Creates a new `TypeGeneratorSettings`.
87    pub fn new() -> Self {
88        Self::default()
89    }
90
91    /// Sets the `type_mod_name` field.
92    pub fn type_mod_name(mut self, type_mod_name: &str) -> Self {
93        self.types_mod_ident =
94            syn::parse_str(type_mod_name).expect("The provided type_mod_name is not a valid ident");
95        self
96    }
97
98    /// Adds a rule, that a type with path `from` should be replaced with the path `to`.
99    pub fn substitute(mut self, from: syn::Path, to: syn::Path) -> Self {
100        self.substitutes
101            .insert(from, absolute_path(to).unwrap())
102            .unwrap();
103        self
104    }
105
106    /// Sets the `compact_as_type_path` field.
107    pub fn compact_as_type_path(mut self, path: syn::Path) -> Self {
108        self.compact_as_type_path = Some(path);
109        self
110    }
111
112    /// Sets the `compact_type_path` field.
113    pub fn compact_type_path(mut self, path: syn::Path) -> Self {
114        self.compact_type_path = Some(path);
115        self
116    }
117
118    /// Sets the `decoded_bits_type_path` field.
119    pub fn decoded_bits_type_path(mut self, path: syn::Path) -> Self {
120        self.decoded_bits_type_path = Some(path);
121        self
122    }
123
124    /// Sets the `should_gen_docs` field.
125    pub fn should_gen_docs(mut self, should_gen_docs: bool) -> Self {
126        self.should_gen_docs = should_gen_docs;
127        self
128    }
129
130    /// Sets the `insert_codec_attributes` field.
131    pub fn insert_codec_attributes(mut self) -> Self {
132        self.insert_codec_attributes = true;
133        self
134    }
135
136    /// Adds some derives for all types.
137    pub fn add_derives_for_all(
138        mut self,
139        derive_paths: impl IntoIterator<Item = syn::Path>,
140    ) -> Self {
141        self.derives.add_derives_for_all(derive_paths);
142        self
143    }
144}