From 4a0ed13875e3402fb9389fc6adc152c84b0e9b0f Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Thu, 1 Aug 2024 20:29:01 -0300 Subject: [PATCH] formatter and clippy pass --- crates/antelope/src/api/client.rs | 9 +++++--- crates/antelope/src/api/default_provider.rs | 4 ++-- crates/antelope/src/api/system/mod.rs | 2 +- crates/antelope/src/api/v1/chain.rs | 24 +++++++++++++++------ crates/antelope/src/api/v1/structs.rs | 8 +++---- crates/antelope/src/chain/signature.rs | 5 ++++- crates/antelope/src/serializer/packer.rs | 16 ++++++-------- crates/antelope/tests/client.rs | 22 ++++++++++++------- crates/antelope/tests/serializer.rs | 2 +- crates/macros/lib.rs | 2 +- 10 files changed, 57 insertions(+), 37 deletions(-) diff --git a/crates/antelope/src/api/client.rs b/crates/antelope/src/api/client.rs index ce16993..4e1574d 100644 --- a/crates/antelope/src/api/client.rs +++ b/crates/antelope/src/api/client.rs @@ -43,11 +43,14 @@ impl APIClient

{ pub fn default_provider(base_url: String) -> Result, String> { Self::default_provider_debug(base_url, false) } - - pub fn default_provider_debug(base_url: String, debug: bool) -> Result, String> { + + pub fn default_provider_debug( + base_url: String, + debug: bool, + ) -> Result, String> { let mut provider = DefaultProvider::new(base_url).unwrap(); provider.set_debug(debug); - + APIClient::custom_provider(provider) } diff --git a/crates/antelope/src/api/default_provider.rs b/crates/antelope/src/api/default_provider.rs index c292b2c..6cc4a1c 100644 --- a/crates/antelope/src/api/default_provider.rs +++ b/crates/antelope/src/api/default_provider.rs @@ -84,7 +84,7 @@ impl Provider for DefaultProvider { if self.debug { println!("Error: {}", err_str); } - + return Err(err_str); } @@ -92,7 +92,7 @@ impl Provider for DefaultProvider { if self.debug { println!("Response: {}", response); } - + Ok(response) } diff --git a/crates/antelope/src/api/system/mod.rs b/crates/antelope/src/api/system/mod.rs index 72e0482..3eaed89 100644 --- a/crates/antelope/src/api/system/mod.rs +++ b/crates/antelope/src/api/system/mod.rs @@ -1,6 +1,5 @@ pub mod structs; -use std::path::Path; use crate::api::client::{APIClient, Provider}; use crate::api::system::structs::{ CreateAccountParams, DelegateBandwidthAction, NewAccountAction, SetAbiAction, SetCodeAction, @@ -15,6 +14,7 @@ use crate::chain::private_key::PrivateKey; use crate::name; use crate::serializer::Encoder; use sha2::{Digest, Sha256}; +use std::path::Path; #[derive(Debug, Default, Clone)] pub struct SystemAPI { diff --git a/crates/antelope/src/api/v1/chain.rs b/crates/antelope/src/api/v1/chain.rs index bf32752..ec98cf3 100644 --- a/crates/antelope/src/api/v1/chain.rs +++ b/crates/antelope/src/api/v1/chain.rs @@ -2,7 +2,10 @@ use std::fmt::Debug; use serde_json::{self, Value}; -use crate::api::v1::structs::{ABIResponse, EncodingError, GetBlockResponse, GetTransactionStatusResponse, ServerError}; +use crate::api::v1::structs::{ + ABIResponse, EncodingError, GetBlockResponse, GetTransactionStatusResponse, ServerError, +}; +use crate::chain::checksum::{Checksum160, Checksum256}; use crate::{ api::{ client::Provider, @@ -21,7 +24,6 @@ use crate::{ serializer::formatter::{JSONObject, ValueTo}, util::hex_to_bytes, }; -use crate::chain::checksum::{Checksum160, Checksum256}; #[derive(Debug, Default, Clone)] pub struct ChainAPI { @@ -191,14 +193,20 @@ impl ChainAPI { } } - pub async fn get_transaction_status(&self, trx_id: Checksum256) -> Result> { + pub async fn get_transaction_status( + &self, + trx_id: Checksum256, + ) -> Result> { let payload = serde_json::json!({ "id": trx_id, }); let result = self .provider - .post(String::from("/v1/chain/get_transaction_status"), Some(payload.to_string())) + .post( + String::from("/v1/chain/get_transaction_status"), + Some(payload.to_string()), + ) .await; match result { @@ -264,10 +272,14 @@ impl ChainAPI { next_key = Some(TableIndexType::UINT128(next_key_str.parse().unwrap())); } Some(TableIndexType::CHECKSUM160(_)) => { - next_key = Some(TableIndexType::CHECKSUM160(Checksum160::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap())); + next_key = Some(TableIndexType::CHECKSUM160( + Checksum160::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap(), + )); } Some(TableIndexType::CHECKSUM256(_)) => { - next_key = Some(TableIndexType::CHECKSUM256(Checksum256::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap())); + next_key = Some(TableIndexType::CHECKSUM256( + Checksum256::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap(), + )); } Some(TableIndexType::FLOAT64(_)) => { next_key = Some(TableIndexType::FLOAT64(next_key_str.parse().unwrap())); diff --git a/crates/antelope/src/api/v1/structs.rs b/crates/antelope/src/api/v1/structs.rs index 15abc6d..0b44230 100644 --- a/crates/antelope/src/api/v1/structs.rs +++ b/crates/antelope/src/api/v1/structs.rs @@ -5,7 +5,6 @@ use serde::{Deserialize, Deserializer, Serialize}; use serde_json::{json, Value}; use std::fmt; use std::mem::discriminant; -use digest::block_buffer::Block; use crate::chain::abi::ABI; use crate::chain::public_key::PublicKey; @@ -16,8 +15,8 @@ use crate::chain::{ authority::Authority, block_id::{deserialize_block_id, deserialize_optional_block_id, BlockId}, checksum::{deserialize_checksum256, Checksum160, Checksum256}, - signature::deserialize_signature, name::{deserialize_name, deserialize_optional_name, deserialize_vec_name, Name}, + signature::deserialize_signature, time::{deserialize_optional_timepoint, deserialize_timepoint, TimePoint, TimePointSec}, transaction::TransactionHeader, varint::VarUint32, @@ -364,7 +363,7 @@ impl TableIndexType { TableIndexType::CHECKSUM160(value) => json!(value.as_string()), } } - + pub fn get_key_type(&self) -> Value { match self { TableIndexType::NAME(_) => Value::String("name".to_string()), @@ -411,7 +410,7 @@ impl GetTableRowsParams { if let Some(reverse) = &self.reverse { req.insert("reverse", Value::Bool(*reverse)); } - + if self.lower_bound.is_some() || self.upper_bound.is_some() { if self.upper_bound.is_none() { let lower = self.lower_bound.as_ref().unwrap(); @@ -435,7 +434,6 @@ impl GetTableRowsParams { if let Some(index_position) = &self.index_position { req.insert("index_position", index_position.to_json()); } - } json!(req).to_string() diff --git a/crates/antelope/src/chain/signature.rs b/crates/antelope/src/chain/signature.rs index c41c80b..d46e770 100644 --- a/crates/antelope/src/chain/signature.rs +++ b/crates/antelope/src/chain/signature.rs @@ -4,7 +4,10 @@ use std::fmt::{Display, Formatter}; use ecdsa::RecoveryId; use k256::Secp256k1; use p256::NistP256; -use serde::{de::{self, Visitor}, Deserialize, Deserializer, Serialize}; +use serde::{ + de::{self, Visitor}, + Deserialize, Deserializer, Serialize, +}; use crate::{ base58, diff --git a/crates/antelope/src/serializer/packer.rs b/crates/antelope/src/serializer/packer.rs index a2a3d55..0228c8f 100644 --- a/crates/antelope/src/serializer/packer.rs +++ b/crates/antelope/src/serializer/packer.rs @@ -305,21 +305,19 @@ pub struct Float128 { impl Float128 { /// - pub fn new(data: [u8;16]) -> Self { - Self { - data: data - } + pub fn new(data: [u8; 16]) -> Self { + Self { data } } /// pub fn data(&self) -> &[u8; 16] { - return &self.data; + &self.data } } impl Packer for Float128 { fn size(&self) -> usize { - return 16; + 16 } fn pack(&self, enc: &mut Encoder) -> usize { @@ -332,7 +330,7 @@ impl Packer for Float128 { let size = self.size(); assert!(raw.len() >= size, "Float128.unpack: buffer overflow!"); slice_copy(&mut self.data, &raw[..size]); - return self.size(); + self.size() } } @@ -470,8 +468,8 @@ where /// Implement `Packer` for `Box` type. impl Packer for Box - where - T: Packer + Default, +where + T: Packer + Default, { /// Returns the size of this value in bytes. fn size(&self) -> usize { diff --git a/crates/antelope/tests/client.rs b/crates/antelope/tests/client.rs index 1be1854..c7575f7 100644 --- a/crates/antelope/tests/client.rs +++ b/crates/antelope/tests/client.rs @@ -1,3 +1,4 @@ +use antelope::api::client::DefaultProvider; use antelope::api::v1::structs::{ErrorResponse, IndexPosition, TableIndexType}; use antelope::{ api::{ @@ -9,8 +10,6 @@ use antelope::{ serializer::{Decoder, Encoder, Packer}, StructPacker, }; -use antelope::api::client::DefaultProvider; -use antelope::util::bytes_to_hex; mod utils; use utils::mock_provider::MockProvider; @@ -368,8 +367,6 @@ pub async fn chain_get_table_rows() { // assert.equal(Number(res2.rows[1].balance).toFixed(6), (0.02566).toFixed(6)) } - - #[tokio::test] pub async fn secondary_index() { #[derive(Clone, Eq, PartialEq, Default, Debug, StructPacker)] @@ -381,8 +378,14 @@ pub async fn secondary_index() { let mock_provider = MockProvider {}; //let client = APIClient::custom_provider(mock_provider).unwrap(); - let mut client = APIClient::::default_provider_debug(String::from("https://testnet.telos.caleos.io"), true).unwrap(); - let checksum = Checksum256::from_hex("dc3264876b721aac60fe7270684c58bcd7e2c9e98ccdfdf4ed960a70b94fad32").unwrap(); + let client = APIClient::::default_provider_debug( + String::from("https://testnet.telos.caleos.io"), + true, + ) + .unwrap(); + let checksum = + Checksum256::from_hex("dc3264876b721aac60fe7270684c58bcd7e2c9e98ccdfdf4ed960a70b94fad32") + .unwrap(); let res1 = client .v1_chain @@ -401,7 +404,6 @@ pub async fn secondary_index() { .await .unwrap(); - // "cbcbbc9c75ab9df67dbf6809a57396769c7d38bf922346c908726016bbc28069" // "c8b2bb18bf9e660d4dbe02ffd63a99e1787ad39e805f80454875818b810b4dad" // "0d53b3c0bc34ffefe4f605530fab2d4f567b894fe957034ac2361c63dabf82d1" @@ -441,5 +443,9 @@ pub async fn secondary_index() { } assert_eq!(res1.rows.len(), 1, "Should get exactly 1 row back"); - assert_eq!(res1.rows[0].proof_digest.as_string(), checksum.as_string(), "Should get back the matching proof_digest"); + assert_eq!( + res1.rows[0].proof_digest.as_string(), + checksum.as_string(), + "Should get back the matching proof_digest" + ); } diff --git a/crates/antelope/tests/serializer.rs b/crates/antelope/tests/serializer.rs index 2981f39..3747cc3 100644 --- a/crates/antelope/tests/serializer.rs +++ b/crates/antelope/tests/serializer.rs @@ -476,7 +476,7 @@ fn variant() { MyVariant::StructOption(opt) => { assert!(opt.is_some(), "Option should be Some"); let my_struct = opt.as_ref().unwrap(); - assert_eq!(my_struct.field1, true); + assert!(my_struct.field1); } _ => { panic!("Expected MyUint8"); diff --git a/crates/macros/lib.rs b/crates/macros/lib.rs index 2ed506e..89ca12b 100644 --- a/crates/macros/lib.rs +++ b/crates/macros/lib.rs @@ -73,7 +73,7 @@ pub fn enum_packer_macro(input: TokenStream) -> TokenStream { let gen = match input.data { syn::Data::Enum(data_enum) => { - let size_variants = data_enum.variants.iter().enumerate().map(|(_i, variant)| { + let size_variants = data_enum.variants.iter().map(|variant| { let variant_ident = &variant.ident; match &variant.fields { Fields::Unnamed(fields) => {