Skip to content

Commit

Permalink
chore(clippy): make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and vkgnosis committed Oct 31, 2022
1 parent ef9a62b commit c598d03
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 65 deletions.
18 changes: 7 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn encode_input(path: &str, name_or_signature: &str, values: &[String], lenient:
let tokens = parse_tokens(&params, lenient)?;
let result = function.encode_input(&tokens)?;

Ok(hex::encode(&result))
Ok(hex::encode(result))
}

fn encode_params(params: &[String], lenient: bool) -> anyhow::Result<String> {
Expand All @@ -193,38 +193,34 @@ fn encode_params(params: &[String], lenient: bool) -> anyhow::Result<String> {
let tokens = parse_tokens(params.as_slice(), lenient)?;
let result = encode(&tokens);

Ok(hex::encode(&result))
Ok(hex::encode(result))
}

fn decode_call_output(path: &str, name_or_signature: &str, data: &str) -> anyhow::Result<String> {
let function = load_function(path, name_or_signature)?;
let data: Vec<u8> = hex::decode(&data)?;
let data: Vec<u8> = hex::decode(data)?;
let tokens = function.decode_output(&data)?;
let types = function.outputs;

assert_eq!(types.len(), tokens.len());

let result = types
.iter()
.zip(tokens.iter())
.map(|(ty, to)| format!("{} {}", ty.kind, to))
.collect::<Vec<String>>()
.join("\n");
let result =
types.iter().zip(tokens.iter()).map(|(ty, to)| format!("{} {to}", ty.kind)).collect::<Vec<String>>().join("\n");

Ok(result)
}

fn decode_params(types: &[String], data: &str) -> anyhow::Result<String> {
let types: Vec<ParamType> = types.iter().map(|s| Reader::read(s)).collect::<Result<_, _>>()?;

let data: Vec<u8> = hex::decode(&data)?;
let data: Vec<u8> = hex::decode(data)?;

let tokens = decode(&types, &data)?;

assert_eq!(types.len(), tokens.len());

let result =
types.iter().zip(tokens.iter()).map(|(ty, to)| format!("{} {}", ty, to)).collect::<Vec<String>>().join("\n");
types.iter().zip(tokens.iter()).map(|(ty, to)| format!("{ty} {to}")).collect::<Vec<String>>().join("\n");

Ok(result)
}
Expand Down
16 changes: 8 additions & 8 deletions derive/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl<'a> From<&'a ethabi::Event> for Event {
.map(|(index, param)| {
if param.name.is_empty() {
if param.indexed {
syn::Ident::new(&format!("topic{}", index), Span::call_site())
syn::Ident::new(&format!("topic{index}"), Span::call_site())
} else {
syn::Ident::new(&format!("param{}", index), Span::call_site())
syn::Ident::new(&format!("param{index}"), Span::call_site())
}
} else {
syn::Ident::new(&param.name.to_snake_case(), Span::call_site())
Expand All @@ -63,7 +63,7 @@ impl<'a> From<&'a ethabi::Event> for Event {
.filter(|&(_, param)| param.indexed)
.map(|(index, param)| {
if param.name.is_empty() {
syn::Ident::new(&format!("topic{}", index), Span::call_site())
syn::Ident::new(&format!("topic{index}"), Span::call_site())
} else {
syn::Ident::new(&param.name.to_snake_case(), Span::call_site())
}
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<'a> From<&'a ethabi::Event> for Event {
.enumerate()
.take(3)
.map(|(index, (param_name, param))| {
let topic = syn::Ident::new(&format!("topic{}", index), Span::call_site());
let topic = syn::Ident::new(&format!("topic{index}"), Span::call_site());
let i = quote! { i };
let to_token = to_token(&i, &param.kind);
quote! { #topic: #param_name.into().map(|#i| #to_token), }
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Event {
let log_fields = &self.log_fields;

quote! {
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct #name {
#(#log_fields),*
}
Expand Down Expand Up @@ -213,7 +213,7 @@ mod tests {
let e = Event::from(&ethabi_event);

let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hello {}
};

Expand Down Expand Up @@ -330,7 +330,7 @@ mod tests {
let e = Event::from(&ethabi_event);

let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct One {
pub foo: ethabi::Address
}
Expand Down Expand Up @@ -358,7 +358,7 @@ mod tests {
let e = Event::from(&ethabi_event);

let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Many {
pub foo: ethabi::Address,
pub bar: Vec<String>,
Expand Down
20 changes: 10 additions & 10 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn get_option(options: &[syn::NestedMeta], name: &str) -> Result<String> {
_ => None,
})
.find(|meta| meta.path().is_ident(name))
.ok_or_else(|| Error::Other(Cow::Owned(format!("Expected to find option {}", name))))?;
.ok_or_else(|| Error::Other(Cow::Owned(format!("Expected to find option {name}"))))?;

str_value_of_meta_item(item, name)
}
Expand All @@ -71,7 +71,7 @@ fn str_value_of_meta_item(item: &syn::Meta, name: &str) -> Result<String> {
}
}

Err(Error::Other(Cow::Owned(format!(r#"`{}` must be in the form `#[{}="something"]`"#, name, name))))
Err(Error::Other(Cow::Owned(format!(r#"`{name}` must be in the form `#[{name}="something"]`"#))))
}

fn normalize_path(relative_path: &str) -> Result<PathBuf> {
Expand Down Expand Up @@ -139,11 +139,11 @@ fn rust_type(input: &ParamType) -> proc_macro2::TokenStream {
ParamType::Bool => quote! { bool },
ParamType::String => quote! { String },
ParamType::Array(ref kind) => {
let t = rust_type(&*kind);
let t = rust_type(kind);
quote! { Vec<#t> }
}
ParamType::FixedArray(ref kind, size) => {
let t = rust_type(&*kind);
let t = rust_type(kind);
quote! { [#t, #size] }
}
ParamType::Tuple(_) => {
Expand All @@ -153,8 +153,8 @@ fn rust_type(input: &ParamType) -> proc_macro2::TokenStream {
}

fn template_param_type(input: &ParamType, index: usize) -> proc_macro2::TokenStream {
let t_ident = syn::Ident::new(&format!("T{}", index), Span::call_site());
let u_ident = syn::Ident::new(&format!("U{}", index), Span::call_site());
let t_ident = syn::Ident::new(&format!("T{index}"), Span::call_site());
let u_ident = syn::Ident::new(&format!("U{index}"), Span::call_site());
match *input {
ParamType::Address => quote! { #t_ident: Into<ethabi::Address> },
ParamType::Bytes => quote! { #t_ident: Into<ethabi::Bytes> },
Expand All @@ -165,13 +165,13 @@ fn template_param_type(input: &ParamType, index: usize) -> proc_macro2::TokenStr
ParamType::Bool => quote! { #t_ident: Into<bool> },
ParamType::String => quote! { #t_ident: Into<String> },
ParamType::Array(ref kind) => {
let t = rust_type(&*kind);
let t = rust_type(kind);
quote! {
#t_ident: IntoIterator<Item = #u_ident>, #u_ident: Into<#t>
}
}
ParamType::FixedArray(ref kind, size) => {
let t = rust_type(&*kind);
let t = rust_type(kind);
quote! {
#t_ident: Into<[#u_ident; #size]>, #u_ident: Into<#t>
}
Expand Down Expand Up @@ -289,7 +289,7 @@ fn input_names(inputs: &[Param]) -> Vec<syn::Ident> {
.enumerate()
.map(|(index, param)| {
if param.name.is_empty() {
syn::Ident::new(&format!("param{}", index), Span::call_site())
syn::Ident::new(&format!("param{index}"), Span::call_site())
} else {
syn::Ident::new(&rust_variable(&param.name), Span::call_site())
}
Expand All @@ -298,7 +298,7 @@ fn input_names(inputs: &[Param]) -> Vec<syn::Ident> {
}

fn get_template_names(kinds: &[proc_macro2::TokenStream]) -> Vec<syn::Ident> {
kinds.iter().enumerate().map(|(index, _)| syn::Ident::new(&format!("T{}", index), Span::call_site())).collect()
kinds.iter().enumerate().map(|(index, _)| syn::Ident::new(&format!("T{index}"), Span::call_site())).collect()
}

fn get_output_kinds(outputs: &[Param]) -> proc_macro2::TokenStream {
Expand Down
4 changes: 2 additions & 2 deletions ethabi/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn take_bytes(data: &[u8], offset: usize, len: usize) -> Result<Vec<u8>, Error>
if offset + len > data.len() {
Err(Error::InvalidData)
} else {
Ok((&data[offset..(offset + len)]).to_vec())
Ok(data[offset..(offset + len)].to_vec())
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ fn decode_param(param: &ParamType, data: &[u8], offset: usize) -> Result<DecodeR
// prevent invalid strings written into contracts by either users or
// Solidity bugs from causing graph-node to fail decoding event
// data.
token: Token::String(String::from_utf8_lossy(&*bytes).into()),
token: Token::String(String::from_utf8_lossy(&bytes).into()),
new_offset: offset + 32,
};
Ok(result)
Expand Down
8 changes: 4 additions & 4 deletions ethabi/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct RawTopicFilter {
}

/// Topic filter.
#[derive(Debug, PartialEq, Default)]
#[derive(Debug, PartialEq, Eq, Default)]
pub struct TopicFilter {
/// Usually (for not-anonymous transactions) the first topic is event signature.
pub topic0: Topic<Hash>,
Expand All @@ -52,7 +52,7 @@ impl Serialize for TopicFilter {
}

/// Acceptable topic possibilities.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Topic<T> {
/// Match any.
Any,
Expand Down Expand Up @@ -130,10 +130,10 @@ impl Serialize for Topic<Hash> {
let value = match *self {
Topic::Any => Value::Null,
Topic::OneOf(ref vec) => {
let v = vec.iter().map(|h| format!("0x{:x}", h)).map(Value::String).collect();
let v = vec.iter().map(|h| format!("0x{h:x}")).map(Value::String).collect();
Value::Array(v)
}
Topic::This(ref hash) => Value::String(format!("0x{:x}", hash)),
Topic::This(ref hash) => Value::String(format!("0x{hash:x}")),
};
value.serialize(serializer)
}
Expand Down
4 changes: 2 additions & 2 deletions ethabi/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ impl Function {
let outputs = self.outputs.iter().map(|p| p.kind.to_string()).collect::<Vec<_>>().join(",");

match (inputs.len(), outputs.len()) {
(_, 0) => format!("{}({})", self.name, inputs),
(_, _) => format!("{}({}):({})", self.name, inputs, outputs),
(_, 0) => format!("{}({inputs})", self.name),
(_, _) => format!("{}({inputs}):({outputs})", self.name),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ethabi/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait ParseLog {
}

/// Ethereum log.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RawLog {
/// Indexed event params are represented as log topics.
pub topics: Vec<Hash>,
Expand Down
2 changes: 1 addition & 1 deletion ethabi/src/param_type/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a> Visitor<'a> for ParamTypeVisitor {
where
E: SerdeError,
{
Reader::read(value).map_err(|e| SerdeError::custom(format!("{:?}", e).as_str()))
Reader::read(value).map_err(|e| SerdeError::custom(format!("{e:?}").as_str()))
}

fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
Expand Down
10 changes: 5 additions & 5 deletions ethabi/src/param_type/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ impl Writer {
match *param {
ParamType::Address => "address".to_owned(),
ParamType::Bytes => "bytes".to_owned(),
ParamType::FixedBytes(len) => format!("bytes{}", len),
ParamType::Int(len) => format!("int{}", len),
ParamType::Uint(len) => format!("uint{}", len),
ParamType::FixedBytes(len) => format!("bytes{len}"),
ParamType::Int(len) => format!("int{len}"),
ParamType::Uint(len) => format!("uint{len}"),
ParamType::Bool => "bool".to_owned(),
ParamType::String => "string".to_owned(),
ParamType::FixedArray(ref param, len) => {
format!("{}[{}]", Writer::write_for_abi(param, serialize_tuple_contents), len)
format!("{}[{len}]", Writer::write_for_abi(param, serialize_tuple_contents))
}
ParamType::Array(ref param) => {
format!("{}[]", Writer::write_for_abi(param, serialize_tuple_contents))
Expand All @@ -44,7 +44,7 @@ impl Writer {
.map(|t| Writer::write_for_abi(t, serialize_tuple_contents))
.collect::<Vec<String>>()
.join(",");
format!("({})", formatted)
format!("({formatted})")
} else {
"tuple".to_owned()
}
Expand Down
4 changes: 2 additions & 2 deletions ethabi/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub fn long_signature(name: &str, params: &[ParamType]) -> Hash {
fn fill_signature(name: &str, params: &[ParamType], result: &mut [u8]) {
let types = params.iter().map(Writer::write).collect::<Vec<String>>().join(",");

let data: Vec<u8> = From::from(format!("{}({})", name, types).as_str());
let data: Vec<u8> = From::from(format!("{name}({types})").as_str());

result.copy_from_slice(&Keccak256::digest(&data)[..result.len()])
result.copy_from_slice(&Keccak256::digest(data)[..result.len()])
}

#[cfg(test)]
Expand Down
18 changes: 9 additions & 9 deletions ethabi/src/token/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ pub enum Token {
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Token::Bool(b) => write!(f, "{}", b),
Token::String(ref s) => write!(f, "{}", s),
Token::Address(ref a) => write!(f, "{:x}", a),
Token::Bytes(ref bytes) | Token::FixedBytes(ref bytes) => write!(f, "{}", hex::encode(&bytes)),
Token::Uint(ref i) | Token::Int(ref i) => write!(f, "{:x}", i),
Token::Bool(b) => write!(f, "{b}"),
Token::String(ref s) => write!(f, "{s}"),
Token::Address(ref a) => write!(f, "{a:x}"),
Token::Bytes(ref bytes) | Token::FixedBytes(ref bytes) => write!(f, "{}", hex::encode(bytes)),
Token::Uint(ref i) | Token::Int(ref i) => write!(f, "{i:x}"),
Token::Array(ref arr) | Token::FixedArray(ref arr) => {
let s = arr.iter().map(|ref t| format!("{}", t)).collect::<Vec<String>>().join(",");
let s = arr.iter().map(|ref t| format!("{t}")).collect::<Vec<String>>().join(",");

write!(f, "[{}]", s)
write!(f, "[{s}]")
}
Token::Tuple(ref s) => {
let s = s.iter().map(|ref t| format!("{}", t)).collect::<Vec<String>>().join(",");
let s = s.iter().map(|ref t| format!("{t}")).collect::<Vec<String>>().join(",");

write!(f, "({})", s)
write!(f, "({s})")
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ mod tests {
let encoded_from_vec_wrapped = functions::set_validators::encode_input(vec![Wrapper(first), Wrapper(second)]);

let expected = "9300c9260000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000011111111111111111111111111111111111111110000000000000000000000002222222222222222222222222222222222222222".to_owned();
assert_eq!(expected, hex::encode(&encoded_from_vec));
assert_eq!(expected, hex::encode(&encoded_from_vec_iter));
assert_eq!(expected, hex::encode(&encoded_from_vec_wrapped));
assert_eq!(expected, hex::encode(encoded_from_vec));
assert_eq!(expected, hex::encode(encoded_from_vec_iter));
assert_eq!(expected, hex::encode(encoded_from_vec_wrapped));
}

#[test]
Expand Down Expand Up @@ -74,9 +74,9 @@ mod tests {
let encoded_from_vec_wrapped = constructor(code, vec![Wrapper(first), Wrapper(second)]);

let expected = "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000011111111111111111111111111111111111111110000000000000000000000002222222222222222222222222222222222222222".to_owned();
assert_eq!(expected, hex::encode(&encoded_from_vec));
assert_eq!(expected, hex::encode(&encoded_from_vec_iter));
assert_eq!(expected, hex::encode(&encoded_from_vec_wrapped));
assert_eq!(expected, hex::encode(encoded_from_vec));
assert_eq!(expected, hex::encode(encoded_from_vec_iter));
assert_eq!(expected, hex::encode(encoded_from_vec_wrapped));
}

#[test]
Expand All @@ -92,9 +92,9 @@ mod tests {

let expected_array = "7de33d2000000000000000000000000011111111111111111111111111111111111111110000000000000000000000002222222222222222222222222222222222222222".to_owned();
let expected_string = "72910be000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000".to_owned();
assert_eq!(expected_array, hex::encode(&encoded_from_array));
assert_eq!(expected_array, hex::encode(&encoded_from_array_wrapped));
assert_eq!(expected_string, hex::encode(&encoded_from_string))
assert_eq!(expected_array, hex::encode(encoded_from_array));
assert_eq!(expected_array, hex::encode(encoded_from_array_wrapped));
assert_eq!(expected_string, hex::encode(encoded_from_string))
}

#[test]
Expand All @@ -104,7 +104,7 @@ mod tests {
let spender = [1u8; 20];
let encoded = eip20::functions::allowance::encode_input(owner, spender);
// 4 bytes signature + 2 * 32 bytes for params
assert_eq!(hex::encode(&encoded), expected);
assert_eq!(hex::encode(encoded), expected);

let from: Address = [2u8; 20].into();
let to: Address = [3u8; 20].into();
Expand Down

0 comments on commit c598d03

Please sign in to comment.