Skip to content

Commit 6d0ec16

Browse files
Reisenguibescos
andauthored
Apply Clippy & Rustfmt to project. (#299)
* style: Rust comment fixes and One grouping * chore: apply clippy to tests * Add clippy tests * Fix asserts clippy * Remove allow * Fix list comparison Co-authored-by: Guillermo Bescos Alapont <[email protected]>
1 parent b283bbc commit 6d0ec16

37 files changed

+1055
-919
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ repos:
1717
- id: cargo-clippy
1818
name: Cargo clippy
1919
language: "rust"
20-
entry : cargo +nightly clippy -- -D warnings
20+
entry : cargo +nightly clippy --tests -- -D warnings
2121
pass_filenames : false

program/rust/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use bindgen::Builder;
44
fn main() {
55
println!("cargo:rustc-link-search=./program/c/target");
66

7-
//make a parser and to it type, traits pairs
7+
// Make a parser and to it type, traits pairs
88
let parser = build_utils::DeriveAdderParserCallback::new();
99

10-
//generate and write bindings
10+
// Generate and write bindings
1111
let bindings = Builder::default()
1212
.clang_arg("-I../../../solana/sdk/bpf/c/inc/")
1313
.header("./src/bindings.h")

program/rust/build_utils.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
use bindgen::callbacks::ParseCallbacks;
2-
use std::collections::HashMap;
3-
use std::panic::UnwindSafe;
1+
use {
2+
bindgen::callbacks::ParseCallbacks,
3+
std::{
4+
collections::HashMap,
5+
panic::UnwindSafe,
6+
},
7+
};
48

5-
///This type stores a hashmap from structnames
6-
///to vectors of trait names, and ensures
7-
///that the traits of each struct are added to its
8-
///definition when an instance of this struct
9-
///is provided as a ParseCallback for bindgen
9+
/// This type stores a hashmap from structnames to vectors of trait names, and ensures that the
10+
/// traits of each struct are added to its definition when an instance of this struct is provided
11+
/// as a ParseCallback for bindgen
1012
#[derive(Debug, Default)]
1113
pub struct DeriveAdderParserCallback<'a> {
1214
pub types_to_traits: HashMap<&'a str, Vec<String>>,
1315
}
1416

1517
impl<'a> DeriveAdderParserCallback<'a> {
16-
///create a parser that does not add any traits
18+
/// Create a parser that does not add any traits
1719
pub fn new() -> Self {
1820
Default::default()
1921
}
20-
//add pairs of types and their desired traits
22+
/// Add pairs of types and their desired traits
2123
#[allow(dead_code)]
2224
pub fn register_traits(&mut self, type_name: &'a str, traits: Vec<String>) {
2325
self.types_to_traits.insert(type_name, traits);
2426
}
2527
}
2628

27-
//this is required to implement the callback trait
29+
/// This is required to implement the callback trait.
2830
impl UnwindSafe for DeriveAdderParserCallback<'_> {
2931
}
3032

program/rust/src/c_oracle_header.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#![allow(non_camel_case_types)]
22
#![allow(non_snake_case)]
3-
//we do not use all the variables in oracle.h, so this helps with the warnings
3+
// We do not use all the variables in oracle.h, so this helps with the warnings
44
#![allow(dead_code)]
5-
//All the custom trait imports should go here
6-
use bytemuck::{
7-
Pod,
8-
Zeroable,
5+
// All the custom trait imports should go here
6+
use {
7+
crate::instruction::OracleCommand,
8+
bytemuck::{
9+
Pod,
10+
Zeroable,
11+
},
12+
solana_program::pubkey::Pubkey,
13+
std::mem::size_of,
914
};
10-
use solana_program::pubkey::Pubkey;
11-
use std::mem::size_of;
1215

13-
use crate::instruction::OracleCommand;
14-
//bindings.rs is generated by build.rs to include
15-
//things defined in bindings.h
16+
// Bindings.rs is generated by build.rs to include things defined in bindings.h
1617
include!("../bindings.rs");
1718

1819
pub const PERMISSIONS_SEED: &str = "permissions";
@@ -21,6 +22,7 @@ pub const PERMISSIONS_SEED: &str = "permissions";
2122
/// If ci > price / PC_MAX_CI_DIVISOR, set publisher status to unknown.
2223
/// (e.g., 20 means ci must be < 5% of price)
2324
pub const MAX_CI_DIVISOR: i64 = 20;
25+
2426
/// Bound on the range of the exponent in price accounts. This number is set such that the
2527
/// PD-based EMA computation does not lose too much precision.
2628
pub const MAX_NUM_DECIMALS: i32 = 8;

program/rust/src/deserialize.rs

+35-29
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
use std::mem::size_of;
2-
3-
use bytemuck::{
4-
try_from_bytes,
5-
try_from_bytes_mut,
6-
Pod,
7-
};
8-
use solana_program::pubkey::Pubkey;
9-
10-
use crate::c_oracle_header::{
11-
AccountHeader,
12-
PythAccount,
13-
PC_MAGIC,
14-
};
15-
use crate::error::OracleError;
16-
use crate::utils::{
17-
allocate_data,
18-
assign_owner,
19-
check_valid_fresh_account,
20-
clear_account,
21-
get_rent,
22-
pyth_assert,
23-
send_lamports,
24-
};
25-
use solana_program::account_info::AccountInfo;
26-
use solana_program::program_error::ProgramError;
27-
use std::cell::{
28-
Ref,
29-
RefMut,
1+
use {
2+
crate::{
3+
c_oracle_header::{
4+
AccountHeader,
5+
PythAccount,
6+
PC_MAGIC,
7+
},
8+
error::OracleError,
9+
utils::{
10+
allocate_data,
11+
assign_owner,
12+
check_valid_fresh_account,
13+
clear_account,
14+
get_rent,
15+
pyth_assert,
16+
send_lamports,
17+
},
18+
},
19+
bytemuck::{
20+
try_from_bytes,
21+
try_from_bytes_mut,
22+
Pod,
23+
},
24+
solana_program::{
25+
account_info::AccountInfo,
26+
program_error::ProgramError,
27+
pubkey::Pubkey,
28+
},
29+
std::{
30+
cell::{
31+
Ref,
32+
RefMut,
33+
},
34+
mem::size_of,
35+
},
3036
};
3137

3238
/// Interpret the bytes in `data` as a value of type `T`

program/rust/src/error.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Error types
2-
use solana_program::program_error::ProgramError;
3-
use thiserror::Error;
2+
use {
3+
solana_program::program_error::ProgramError,
4+
thiserror::Error,
5+
};
46

57
/// Errors that may be returned by the oracle program
68
#[derive(Clone, Debug, Eq, Error, PartialEq)]

program/rust/src/instruction.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use crate::c_oracle_header::PC_VERSION;
2-
use crate::deserialize::load;
3-
use crate::error::OracleError;
4-
use bytemuck::{
5-
Pod,
6-
Zeroable,
1+
use {
2+
crate::{
3+
c_oracle_header::PC_VERSION,
4+
deserialize::load,
5+
error::OracleError,
6+
},
7+
bytemuck::{
8+
Pod,
9+
Zeroable,
10+
},
11+
num_derive::{
12+
FromPrimitive,
13+
ToPrimitive,
14+
},
15+
num_traits::FromPrimitive,
16+
solana_program::pubkey::Pubkey,
717
};
8-
use num_derive::{
9-
FromPrimitive,
10-
ToPrimitive,
11-
};
12-
use num_traits::FromPrimitive;
13-
use solana_program::pubkey::Pubkey;
1418

1519
/// WARNING : NEW COMMANDS SHOULD BE ADDED AT THE END OF THE LIST
1620
#[repr(i32)]

program/rust/src/lib.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@ mod tests;
1717
#[cfg(feature = "debug")]
1818
mod log;
1919

20-
use crate::error::OracleError;
21-
use processor::process_instruction;
22-
23-
use solana_program::entrypoint;
24-
25-
//Below is a high lever description of the rust/c setup.
26-
27-
//As we migrate from C to Rust, our Rust code needs to be able to interact with C
28-
//build-bpf.sh is set up to compile the C code into a two archive files
29-
//contained in `./program/c/target/`
30-
// - `libcpyth-bpf.a` contains the bpf version for production code
31-
// - `libcpyth-native.a` contains the systems architecture version for tests
32-
33-
//We also generate bindings for the types and constants in oracle.h (as well as other things
34-
//included in bindings.h), these bindings can be accessed through c_oracle_header.rs
35-
//Bindings allow us to access type definitions, function definitions and constants. In order to
36-
//add traits to the bindings, we use the parser in build.rs. The traits must be defined/included
37-
//at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh
38-
//serialization traits.
20+
use {
21+
crate::error::OracleError,
22+
processor::process_instruction,
23+
solana_program::entrypoint,
24+
};
25+
26+
// Below is a high lever description of the rust/c setup.
27+
28+
// As we migrate from C to Rust, our Rust code needs to be able to interact with C
29+
// build-bpf.sh is set up to compile the C code into a two archive files
30+
// contained in `./program/c/target/`
31+
// - `libcpyth-bpf.a` contains the bpf version for production code
32+
// - `libcpyth-native.a` contains the systems architecture version for tests
33+
34+
// We also generate bindings for the types and constants in oracle.h (as well as other things
35+
// included in bindings.h), these bindings can be accessed through c_oracle_header.rs
36+
// Bindings allow us to access type definitions, function definitions and constants. In order to
37+
// add traits to the bindings, we use the parser in build.rs. The traits must be defined/included
38+
// at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh
39+
// serialization traits.
3940

4041
entrypoint!(process_instruction);

program/rust/src/log.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
use crate::c_oracle_header::*;
2-
use crate::deserialize::{
3-
load,
4-
load_account_as,
1+
use {
2+
crate::{
3+
c_oracle_header::*,
4+
deserialize::{
5+
load,
6+
load_account_as,
7+
},
8+
error::OracleError,
9+
},
10+
solana_program::{
11+
account_info::AccountInfo,
12+
clock::Clock,
13+
entrypoint::ProgramResult,
14+
msg,
15+
program_error::ProgramError,
16+
sysvar::Sysvar,
17+
},
518
};
6-
use crate::error::OracleError;
7-
use solana_program::account_info::AccountInfo;
8-
use solana_program::clock::Clock;
9-
use solana_program::entrypoint::ProgramResult;
10-
use solana_program::msg;
11-
use solana_program::program_error::ProgramError;
12-
use solana_program::sysvar::Sysvar;
1319

1420
pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
1521
msg!("Pyth oracle contract");
@@ -87,7 +93,7 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
8793
}
8894

8995
command_t_e_cmd_resize_price_account => {
90-
//accounts[1] is the updated account
96+
// accounts[1] is the updated account
9197
msg!("ResizePriceAccount: {}", accounts[1].key);
9298
}
9399
_ => {

program/rust/src/processor.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
use crate::error::OracleError;
2-
use crate::instruction::{
3-
load_command_header_checked,
4-
OracleCommand,
1+
use {
2+
crate::{
3+
error::OracleError,
4+
instruction::{
5+
load_command_header_checked,
6+
OracleCommand,
7+
},
8+
rust_oracle::{
9+
add_mapping,
10+
add_price,
11+
add_product,
12+
add_publisher,
13+
del_price,
14+
del_product,
15+
del_publisher,
16+
init_mapping,
17+
init_price,
18+
resize_price_account,
19+
set_min_pub,
20+
upd_permissions,
21+
upd_price,
22+
upd_price_no_fail_on_error,
23+
upd_product,
24+
},
25+
},
26+
solana_program::{
27+
entrypoint::ProgramResult,
28+
pubkey::Pubkey,
29+
sysvar::slot_history::AccountInfo,
30+
},
531
};
6-
use solana_program::entrypoint::ProgramResult;
7-
use solana_program::pubkey::Pubkey;
8-
use solana_program::sysvar::slot_history::AccountInfo;
932

10-
use crate::rust_oracle::{
11-
add_mapping,
12-
add_price,
13-
add_product,
14-
add_publisher,
15-
del_price,
16-
del_product,
17-
del_publisher,
18-
init_mapping,
19-
init_price,
20-
resize_price_account,
21-
set_min_pub,
22-
upd_permissions,
23-
upd_price,
24-
upd_price_no_fail_on_error,
25-
upd_product,
26-
};
27-
28-
///dispatch to the right instruction in the oracle
33+
/// Dispatch to the right instruction in the oracle.
2934
pub fn process_instruction(
3035
program_id: &Pubkey,
3136
accounts: &[AccountInfo],

0 commit comments

Comments
 (0)