Skip to content

Commit d73c9c0

Browse files
committedJan 17, 2025
feat(state-lens-light-client-types): introduce generic client state type
1 parent 0543024 commit d73c9c0

File tree

9 files changed

+547
-62
lines changed

9 files changed

+547
-62
lines changed
 

‎Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/macros/src/lib.rs

+76-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use syn::{
1111
punctuated::Punctuated,
1212
spanned::Spanned,
1313
Attribute, Data, DeriveInput, Expr, ExprPath, Field, Fields, GenericParam, Generics, Ident,
14-
Item, ItemEnum, ItemStruct, LitStr, MacroDelimiter, Meta, MetaList, Path, Token, Type, Variant,
15-
WhereClause, WherePredicate,
14+
Index, Item, ItemEnum, ItemStruct, LitStr, MacroDelimiter, Meta, MetaList, Path, Token, Type,
15+
Variant, WhereClause, WherePredicate,
1616
};
1717

1818
#[proc_macro_attribute]
@@ -1107,3 +1107,77 @@ fn parse_ibc_path(path: LitStr) -> Vec<Segment> {
11071107
})
11081108
.collect()
11091109
}
1110+
1111+
#[proc_macro_derive(AsTuple)]
1112+
pub fn as_tuple(ts: TokenStream) -> TokenStream {
1113+
derive_as_tuple(parse_macro_input!(ts as DeriveInput))
1114+
// .inspect(|x| println!("{x}"))
1115+
.map_err(|e| e.into_compile_error())
1116+
.unwrap_or_else(convert::identity)
1117+
.into()
1118+
}
1119+
1120+
fn derive_as_tuple(
1121+
DeriveInput {
1122+
ident,
1123+
generics,
1124+
data,
1125+
..
1126+
}: DeriveInput,
1127+
) -> Result<proc_macro2::TokenStream, syn::Error> {
1128+
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
1129+
1130+
let Data::Struct(data_struct) = data else {
1131+
return Err(syn::Error::new(
1132+
Span::call_site(),
1133+
"only structs are supported",
1134+
));
1135+
};
1136+
1137+
let (into_tuple_fields, as_tuple_fields, from_tuple_fields) = (
1138+
data_struct.fields.members(),
1139+
data_struct.fields.members(),
1140+
data_struct.fields.members(),
1141+
);
1142+
1143+
let field_types = data_struct.fields.iter().map(|f| &f.ty);
1144+
1145+
let from_tuple_tuple_idxs = data_struct
1146+
.fields
1147+
.members()
1148+
.enumerate()
1149+
.map(|(idx, _)| Index::from(idx));
1150+
1151+
Ok(quote! {
1152+
const _: () = {
1153+
#[automatically_derived]
1154+
impl #impl_generics ::unionlabs::tuple::AsTuple for #ident #ty_generics #where_clause {
1155+
type Tuple = (#(#field_types,)*);
1156+
1157+
fn as_tuple(&self) -> <Self::Tuple as Tuple>::Ref<'_> {
1158+
(
1159+
#(
1160+
&self.#as_tuple_fields,
1161+
)*
1162+
)
1163+
}
1164+
1165+
fn into_tuple(self) -> Self::Tuple {
1166+
(
1167+
#(
1168+
self.#into_tuple_fields,
1169+
)*
1170+
)
1171+
}
1172+
1173+
fn from_tuple(tuple: Self::Tuple) -> Self {
1174+
Self {
1175+
#(
1176+
#from_tuple_fields: tuple.#from_tuple_tuple_idxs,
1177+
)*
1178+
}
1179+
}
1180+
}
1181+
};
1182+
})
1183+
}

‎lib/state-lens-light-client-types/Cargo.toml

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ repository.workspace = true
66
version = "0.1.0"
77

88
[dependencies]
9-
alloy = { workspace = true, features = ["sol-types"], optional = true }
10-
bincode = { workspace = true, features = ["alloc", "derive"], optional = true }
11-
protos = { workspace = true, optional = true, features = ["proto_full", "serde"] }
12-
serde = { workspace = true, optional = true, features = ["derive"] }
13-
thiserror = { workspace = true }
14-
unionlabs = { workspace = true, features = ["ethabi", "proto"] }
9+
alloy = { workspace = true, optional = true, features = ["sol-types", "dyn-abi"] }
10+
bincode = { workspace = true, optional = true, features = ["alloc", "derive"] }
11+
protos = { workspace = true, optional = true, features = ["proto_full", "serde"] }
12+
serde = { workspace = true, optional = true, features = ["derive"] }
13+
thiserror = { workspace = true }
14+
tuple_join = { version = "0.1.0", optional = true }
15+
unionlabs = { workspace = true, features = ["ethabi", "proto"] }
1516

1617
[dev-dependencies]
17-
hex-literal = { workspace = true }
18+
hex-literal = { workspace = true }
19+
serde_json = { workspace = true }
20+
state-lens-light-client-types = { workspace = true, features = ["bincode", "ethabi", "serde"] }
21+
unionlabs = { workspace = true, features = ["test-utils"] }
1822

1923
[features]
20-
bincode = ["dep:bincode", "unionlabs/bincode"]
2124
default = []
22-
ethabi = ["unionlabs/ethabi", "dep:alloy", "dep:protos"]
25+
26+
bincode = ["dep:bincode", "unionlabs/bincode"]
27+
ethabi = ["unionlabs/ethabi", "dep:alloy", "dep:protos", "dep:tuple_join"]
2328
serde = ["dep:serde"]
2429

2530
[lints]

0 commit comments

Comments
 (0)