git.delta.rocks / unique-network / refs/commits / 12eee8195c76

difftreelog

misk: Generate solidity docs for struct

Trubnikov Sergey2022-11-21parent: #3e03fdf.patch.diff
in: master

4 files changed

modifiedcrates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth
--- a/crates/evm-coder/procedural/src/abi_derive.rs
+++ b/crates/evm-coder/procedural/src/abi_derive.rs
@@ -2,6 +2,29 @@
 
 pub(crate) fn impl_abi_macro(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
 	let name = &ast.ident;
+	let docs = ast
+		.attrs
+		.iter()
+		.filter_map(|attr| {
+			if let Some(ps) = attr.path.segments.first() {
+				if ps.ident == "doc" {
+					let meta = match attr.parse_meta() {
+						Ok(meta) => meta,
+						Err(e) => return Some(Err(e)),
+					};
+					match meta {
+						syn::Meta::NameValue(mnv) => match &mnv.lit {
+							syn::Lit::Str(ls) => return Some(Ok(ls.value())),
+							_ => unreachable!(),
+						},
+						_ => unreachable!(),
+					}
+				}
+			}
+			None
+		})
+		.collect::<syn::Result<Vec<_>>>()?;
+
 	let (is_named_fields, field_names, field_types, params_count) = match &ast.data {
 		syn::Data::Struct(ds) => match ds.fields {
 			syn::Fields::Named(ref fields) => Ok((
@@ -37,7 +60,8 @@
 	let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names.clone());
 	let solidity_type = impl_solidity_type(name, field_types.clone(), params_count);
 	let solidity_type_name = impl_solidity_type_name(name, field_types.clone(), params_count);
-	let solidity_struct_collect = impl_solidity_struct_collect(name, field_names, field_types);
+	let solidity_struct_collect =
+		impl_solidity_struct_collect(name, field_names, field_types, &docs);
 
 	Ok(quote! {
 		#can_be_plcaed_in_vec
@@ -259,6 +283,7 @@
 	name: &syn::Ident,
 	field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,
 	field_types: impl Iterator<Item = &'a syn::Type> + Clone,
+	docs: &Vec<String>,
 ) -> proc_macro2::TokenStream {
 	let string_name = name.to_string();
 	let name_type = field_names
@@ -271,6 +296,13 @@
 				write!(str, "{};", #name).unwrap();
 			)
 		});
+	let docs = docs.iter().enumerate().map(|(i, doc)| {
+		let doc = doc.trim();
+		let dev = if i == 0 { " @dev" } else { "" };
+		quote! {
+			writeln!(str, "///{} {}", #dev, #doc).unwrap();
+		}
+	});
 
 	quote! {
 		#[cfg(feature = "stubgen")]
@@ -283,7 +315,7 @@
 				use std::fmt::Write;
 
 				let mut str = String::new();
-				writeln!(str, "/// @dev Cross account struct").unwrap();
+				#(#docs)*
 				writeln!(str, "struct {} {{", Self::name()).unwrap();
 				#(#name_type)*
 				writeln!(str, "}}").unwrap();
modifiedcrates/evm-coder/src/solidity/impls.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/solidity/impls.rs
+++ b/crates/evm-coder/src/solidity/impls.rs
@@ -1,6 +1,7 @@
 use super::{TypeCollector, SolidityTypeName, SolidityType, StructCollect};
 use crate::{sealed, types::*};
 use core::fmt;
+use primitive_types::{U256, H160};
 
 macro_rules! solidity_type_name {
     ($($ty:ty => $name:literal $simple:literal = $default:literal),* $(,)?) => {
@@ -31,13 +32,13 @@
 }
 
 solidity_type_name! {
-	uint8 => "uint8" true = "0",
-	uint32 => "uint32" true = "0",
-	uint64 => "uint64" true = "0",
-	uint128 => "uint128" true = "0",
-	uint256 => "uint256" true = "0",
+	u8 => "uint8" true = "0",
+	u32 => "uint32" true = "0",
+	u64 => "uint64" true = "0",
+	u128 => "uint128" true = "0",
+	U256 => "uint256" true = "0",
 	bytes4 => "bytes4" true = "bytes4(0)",
-	address => "address" true = "0x0000000000000000000000000000000000000000",
+	H160 => "address" true = "0x0000000000000000000000000000000000000000",
 	string => "string" false = "\"\"",
 	bytes => "bytes" false = "hex\"\"",
 	bool => "bool" true = "false",
modifiedcrates/evm-coder/tests/abi_derive_generation.rsdiffbeforeafterboth
57 _b: TypeStruct2DynamicParam,57 _b: TypeStruct2DynamicParam,
58}58}
5959
60/// Some docs
61/// At multi
62/// line
60#[derive(AbiCoder, PartialEq, Debug)]63#[derive(AbiCoder, PartialEq, Debug)]
61struct TypeStruct3DerivedMixedParam {64struct TypeStruct3DerivedMixedParam {
65 /// Docs for A
62 _a: TypeStruct1SimpleParam,66 _a: TypeStruct1SimpleParam,
67 /// Docs for B
63 _b: TypeStruct2DynamicParam,68 _b: TypeStruct2DynamicParam,
69 /// Docs for C
64 _c: TypeStruct2MixedParam,70 _c: TypeStruct2MixedParam,
65}71}
6672
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -113,6 +113,7 @@
 	}
 }
 
+/// Cross account struct
 #[derive(Debug, Default, AbiCoder)]
 pub struct EthCrossAccount {
 	pub(crate) eth: address,