Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

docs: update docs and examples on derive macros #2375

Merged
merged 3 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions ethers-contract/ethers-contract-derive/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ use crate::utils;
use ethers_core::{abi::ParamType, macros::ethers_core_crate};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse::Error, spanned::Spanned, Data, DeriveInput, Fields, Index};
use syn::{parse::Error, spanned::Spanned, Data, DeriveInput, Index};

/// Derive `fmt::Display` for the given type
pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result<TokenStream, Error> {
let fields = match input.data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields) => fields.named.iter().collect(),
Fields::Unnamed(ref fields) => fields.unnamed.iter().collect(),
Fields::Unit => vec![],
},
Data::Enum(_) => {
return Err(Error::new(input.span(), "Enum types are not supported by EthDisplay"))
Data::Struct(data) => data.fields.into_iter(),
Data::Enum(e) => {
return Err(Error::new(e.enum_token.span(), "Enums are not supported by EthDisplay"))
}
Data::Union(_) => {
return Err(Error::new(input.span(), "Union types are not supported by EthDisplay"))
Data::Union(u) => {
return Err(Error::new(u.union_token.span(), "Unions are not supported by EthDisplay"))
}
};

let mut expressions = TokenStream::new();
for (i, field) in fields.iter().enumerate() {
for (i, field) in fields.enumerate() {
// comma separator
if i > 0 {
let tokens = quote! {
::core::fmt::Write::write_str(f, ", ")?;
};
expressions.extend(tokens);
}

let ident = field.ident.as_ref().map(|id| quote!(#id)).unwrap_or_else(|| {
let idx = Index::from(i);
quote!(#idx)
Expand All @@ -36,14 +40,6 @@ pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result<TokenStream,
// could not detect the parameter type and rely on using debug fmt
fmt_debug_tokens(&ident, &mut expressions);
}

// comma separator
if i < fields.len() - 1 {
let tokens = quote! {
::core::fmt::Write::write_str(f, ", ")?;
};
expressions.extend(tokens);
}
}

let name = &input.ident;
Expand Down
Loading