pub struct CodegenBuilder { /* private fields */ }
Expand description
Generate a type safe interface to use with subxt
.
The options exposed here are similar to those exposed via
the #[subxt]
macro or via the subxt codegen
CLI command.
Both use this under the hood.
§Example
Generating an interface using all of the defaults:
use codec::Decode;
use subxt_codegen::{ Metadata, CodegenBuilder };
// Get hold of and decode some metadata:
let encoded = std::fs::read("../artifacts/polkadot_metadata_full.scale").unwrap();
let metadata = Metadata::decode(&mut &*encoded).unwrap();
// Generate a TokenStream representing the code for the interface.
// This can be converted to a string, displayed as-is or output from a macro.
let token_stream = CodegenBuilder::new().generate(metadata);
Implementations§
Source§impl CodegenBuilder
impl CodegenBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a builder to configure and generate a type-safe interface for Subxt.
Sourcepub fn disable_default_derives(&mut self)
pub fn disable_default_derives(&mut self)
Disable the default derives that are applied to all types.
§Warning
This is not recommended, and is highly likely to break some part of the generated interface. Expect compile errors.
Sourcepub fn disable_default_substitutes(&mut self)
pub fn disable_default_substitutes(&mut self)
Disable the default type substitutions that are applied to the generated code.
§Warning
This is not recommended, and is highly likely to break some part of the generated interface. Expect compile errors.
Sourcepub fn no_docs(&mut self)
pub fn no_docs(&mut self)
Disable the output of doc comments associated with the generated types and methods. This can reduce the generated code size at the expense of losing documentation.
Sourcepub fn runtime_types_only(&mut self)
pub fn runtime_types_only(&mut self)
Only generate the types, and don’t generate the rest of the Subxt specific interface.
Sourcepub fn set_additional_global_derives(&mut self, derives: Vec<Path>)
pub fn set_additional_global_derives(&mut self, derives: Vec<Path>)
Set the additional derives that will be applied to all types. By default, a set of derives required for Subxt are automatically added for all types.
§Warning
Invalid derives, or derives that cannot be applied to all of the generated types (taking into account that some types are substituted for hand written ones that we cannot add extra derives for) will lead to compile errors in the generated code.
Sourcepub fn set_additional_global_attributes(&mut self, attributes: Vec<Attribute>)
pub fn set_additional_global_attributes(&mut self, attributes: Vec<Attribute>)
Set the additional attributes that will be applied to all types. By default, a set of attributes required for Subxt are automatically added for all types.
§Warning
Invalid attributes can very easily lead to compile errors in the generated code.
Sourcepub fn add_derives_for_type(
&mut self,
ty: TypePath,
derives: impl IntoIterator<Item = Path>,
recursive: bool,
)
pub fn add_derives_for_type( &mut self, ty: TypePath, derives: impl IntoIterator<Item = Path>, recursive: bool, )
Set additional derives for a specific type at the path given.
If you want to set the additional derives on all contained types recursively as well,
you can set the recursive
argument to true
. If you don’t do that,
there might be compile errors in the generated code, if the derived trait
relies on the fact that contained types also implement that trait.
Sourcepub fn add_attributes_for_type(
&mut self,
ty: TypePath,
attributes: impl IntoIterator<Item = Attribute>,
recursive: bool,
)
pub fn add_attributes_for_type( &mut self, ty: TypePath, attributes: impl IntoIterator<Item = Attribute>, recursive: bool, )
Set additional attributes for a specific type at the path given.
Setting the recursive
argument to true
will additionally add the specified
attributes to all contained types recursively.
Sourcepub fn set_type_substitute(&mut self, ty: Path, with: Path)
pub fn set_type_substitute(&mut self, ty: Path, with: Path)
Substitute a type at the given path with some type at the second path. During codegen, we will avoid generating the type at the first path given, and instead point any references to that type to the second path given.
The substituted type will need to implement the relevant traits to be compatible with the original, and it will need to SCALE encode and SCALE decode in a compatible way.
Sourcepub fn set_target_module(&mut self, item_mod: ItemMod)
pub fn set_target_module(&mut self, item_mod: ItemMod)
By default, all of the code is generated inside a module pub mod api {}
. We decorate
this module with a few attributes to reduce compile warnings and things. You can provide a
target module here, allowing you to add additional attributes or inner code items (with the
warning that duplicate identifiers will lead to compile errors).
Sourcepub fn set_subxt_crate_path(&mut self, crate_path: Path)
pub fn set_subxt_crate_path(&mut self, crate_path: Path)
Set the path to the subxt
crate. By default, we expect it to be at ::subxt::ext::subxt_core
.
§Panics
Panics if the path provided is not an absolute path.
Sourcepub fn generate(self, metadata: Metadata) -> Result<TokenStream2, CodegenError>
pub fn generate(self, metadata: Metadata) -> Result<TokenStream2, CodegenError>
Generate an interface, assuming that the default path to the subxt
crate is ::subxt::ext::subxt_core
.
If the subxt
crate is not available as a top level dependency, use generate
and provide
a valid path to the `subxt¦ crate.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CodegenBuilder
impl RefUnwindSafe for CodegenBuilder
impl !Send for CodegenBuilder
impl !Sync for CodegenBuilder
impl Unpin for CodegenBuilder
impl UnwindSafe for CodegenBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.