Skip to content

Commit

Permalink
style: 🎨 fix clippy
Browse files Browse the repository at this point in the history
fix clippy warnings and bump to 2021 edition rust
  • Loading branch information
Forsworns committed Jul 7, 2023
1 parent 520b6b6 commit b72cd36
Show file tree
Hide file tree
Showing 71 changed files with 402 additions and 424 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
run: rustup component add rustfmt
- name: "rustfmt --check"
run: |
if ! rustfmt --check --edition 2018 $(find . -name '*.rs' -print); then
printf "Please run \`rustfmt --edition 2018 \$(find . -name '*.rs' -print)\` to fix rustfmt errors.\nSee CONTRIBUTING.md for more details.\n" >&2
if ! rustfmt --check --edition 2021 $(find . -name '*.rs' -print); then
printf "Please run \`rustfmt --edition 2021 \$(find . -name '*.rs' -print)\` to fix rustfmt errors.\nSee CONTRIBUTING.md for more details.\n" >&2
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ check:
cargo check --all-features

clippy:
cargo clippy --all-targets
cargo clippy --all-targets --all-features

doc: clean
cargo doc --lib --no-deps --all-features --document-private-items
Expand All @@ -14,7 +14,7 @@ clean:
cargo clean

fmt:
@rustfmt --edition 2018 $(SRC_FILES)
@rustfmt --edition 2021 $(SRC_FILES)

unit: unit_single unit_parallel

Expand Down
6 changes: 2 additions & 4 deletions examples/datasources/consul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
use consul::{kv::KVPair, kv::KV, Client, Config, QueryOptions};
use sentinel_core::{
base,
datasource::{
ds_consul::ConsulDataSource, new_flow_rule_handler, rule_json_array_parser, DataSource,
},
datasource::{ds_consul::ConsulDataSource, new_flow_rule_handler, rule_json_array_parser},
flow,
utils::sleep_for_ms,
EntryBuilder, Result,
Expand Down Expand Up @@ -34,7 +32,7 @@ fn main() -> Result<()> {
};

client.put(&pair, None).unwrap();
println!("list: {:?}", client.list(&key, None));
println!("list: {:?}", client.list(key, None));
}

// Sleep 3 seconds and then read the consul
Expand Down
4 changes: 1 addition & 3 deletions examples/datasources/etcdv3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
use etcd_rs::{Client, ClientConfig, PutRequest};
use sentinel_core::{
base,
datasource::{
ds_etcdv3::Etcdv3DataSource, new_flow_rule_handler, rule_json_array_parser, DataSource,
},
datasource::{ds_etcdv3::Etcdv3DataSource, new_flow_rule_handler, rule_json_array_parser},
flow, EntryBuilder, Result,
};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion examples/datasources/k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async fn dynamic_update(
},
);

let flow_rule: Api<flow::FlowResource> = Api::namespaced(client.clone(), namespace.into());
let flow_rule: Api<flow::FlowResource> = Api::namespaced(client.clone(), namespace);
flow_rule.create(&PostParams::default(), &cr).await?;
println!(
"Dynamically change custom resource: {} to: {:?}",
Expand Down
2 changes: 2 additions & 0 deletions examples/exporter/prometheus/prometheus.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::needless_update)]

use rand::prelude::*;
use sentinel_core::base::Snapshot;
use sentinel_core::circuitbreaker::{
Expand Down
2 changes: 1 addition & 1 deletion middleware/actix/example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sentinel_actix::Sentinel;
use sentinel_core::flow;
use std::sync::Arc;

const RESOURCE_NAME: &'static str = "actix_example";
const RESOURCE_NAME: &str = "actix_example";

fn custom_extractor(_req: &ServiceRequest) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/axum/example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::net::SocketAddr;
use std::sync::Arc;
use tower::ServiceBuilder;

const RESOURCE_NAME: &'static str = "axum_example";
const RESOURCE_NAME: &str = "axum_example";

type Request = http::Request<Body>;

Expand Down
2 changes: 1 addition & 1 deletion middleware/motore/example/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use volo_grpc::{
Request, Response,
};

const RESOURCE_NAME: &'static str = "motore_example";
const RESOURCE_NAME: &str = "motore_example";

fn custom_extractor(_cx: &ClientContext, _req: &Request<HelloServiceRequestSend>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/motore/example/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use volo_grpc::{
Request, Response,
};

const RESOURCE_NAME: &'static str = "motore_example";
const RESOURCE_NAME: &str = "motore_example";

fn custom_extractor(_cx: &ServerContext, _req: &Request<HelloServiceRequestRecv>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/rocket/example/src/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sentinel_core::flow;
use sentinel_rocket::{SentinelFairing, SentinelFairingState};
use std::sync::Arc;

const RESOURCE_NAME: &'static str = "rocket_example";
const RESOURCE_NAME: &str = "rocket_example";

fn custom_extractor(_req: &Request<'_>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/rocket/example/src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sentinel_core::flow;
use sentinel_rocket::{SentinelConfigForGuard, SentinelGuard};
use std::sync::Arc;

const RESOURCE_NAME: &'static str = "rocket_example";
const RESOURCE_NAME: &str = "rocket_example";

fn custom_extractor(_req: &Request<'_>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/tonic/example/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod hello_world {
tonic::include_proto!("helloworld");
}

const RESOURCE_NAME: &'static str = "tonic_example";
const RESOURCE_NAME: &str = "tonic_example";

fn custom_extractor(_req: &Request<()>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/tonic/example/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod hello_world {
tonic::include_proto!("helloworld");
}

const RESOURCE_NAME: &'static str = "tonic_example";
const RESOURCE_NAME: &str = "tonic_example";

fn custom_extractor(_req: &Request<()>) -> String {
RESOURCE_NAME.into()
Expand Down
2 changes: 1 addition & 1 deletion middleware/tower/example/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod hello_world {
tonic::include_proto!("helloworld");
}

const RESOURCE_NAME: &'static str = "tonic_example";
const RESOURCE_NAME: &str = "tonic_example";

type Request = http::Request<tonic::body::BoxBody>;
// type Response = http::Response<tonic::transport::Body>;
Expand Down
2 changes: 1 addition & 1 deletion middleware/tower/example/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod hello_world {
tonic::include_proto!("helloworld");
}

const RESOURCE_NAME: &'static str = "tonic_example";
const RESOURCE_NAME: &str = "tonic_example";

type Request = http::Request<tonic::transport::Body>;
type Response = http::Response<tonic::body::BoxBody>;
Expand Down
2 changes: 1 addition & 1 deletion sentinel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "sentinel-core"
version = "0.1.3"
authors = ["Forsworns <378974295@qq.com>"]
edition = "2018"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
documentation = "https://docs.rs/sentinel-core/latest"
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions sentinel-core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//! 3. `init_with_config_file(config_path: String)`, using yaml file to initialize.
//! For the examples, visit the [Sentinel repository](https://github.com/sentinel-group/sentinel-rust)
pub mod api;
pub mod init;
pub mod slot_chain;
mod base;
mod init;
mod slot_chain;

pub use api::*;
pub use base::*;
pub use init::*;
pub use slot_chain::*;
28 changes: 13 additions & 15 deletions sentinel-core/src/core/base/block_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl BlockError {

impl fmt::Display for BlockError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.block_msg.len() == 0 {
if self.block_msg.is_empty() {
write!(f, "SentinelBlockError: {}", self.block_type)
} else {
write!(
Expand All @@ -97,7 +97,7 @@ mod test {

impl SentinelRule for MockRule {
fn resource_name(&self) -> String {
return "mock resource".into();
"mock resource".into()
}
}

Expand Down Expand Up @@ -130,20 +130,18 @@ mod test {
&block_err.triggered_value().unwrap(),
&snapshot_value
));
} else if let Some(block_msg) = block_msg {
block_err = BlockError::new_with_msg(block_type, block_msg.clone());
assert_eq!(block_err.block_type, block_type);
assert_eq!(block_err.block_msg, block_msg);
assert!(block_err.triggered_rule().is_none());
assert!(block_err.triggered_value().is_none());
} else {
if let Some(block_msg) = block_msg {
block_err = BlockError::new_with_msg(block_type, block_msg.clone());
assert_eq!(block_err.block_type, block_type);
assert_eq!(block_err.block_msg, block_msg);
assert_eq!(block_err.triggered_rule().is_none(), true);
assert_eq!(block_err.triggered_value().is_none(), true);
} else {
block_err = BlockError::new(block_type);
assert_eq!(block_err.block_type, block_type);
assert_eq!(block_err.block_msg, String::default());
assert_eq!(block_err.triggered_rule().is_none(), true);
assert_eq!(block_err.triggered_value().is_none(), true);
}
block_err = BlockError::new(block_type);
assert_eq!(block_err.block_type, block_type);
assert_eq!(block_err.block_msg, String::default());
assert!(block_err.triggered_rule().is_none());
assert!(block_err.triggered_value().is_none());
}
}

Expand Down
4 changes: 2 additions & 2 deletions sentinel-core/src/core/base/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ mod test {
#[test]
fn is_blocked() {
let mut ctx = EntryContext::new();
assert_eq!(ctx.is_blocked(), false);
assert!(!ctx.is_blocked());
ctx.set_result(TokenResult::new_blocked(BlockType::Other(1)));
assert_eq!(ctx.is_blocked(), true);
assert!(ctx.is_blocked());
}
}
2 changes: 1 addition & 1 deletion sentinel-core/src/core/base/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SentinelEntry {
// todo: cleanup
pub fn exit(&self) {
for handler in &self.exit_handlers {
handler(&self, self.ctx.clone()) // Rc/Arc clone
handler(self, self.ctx.clone()) // Rc/Arc clone
.map_err(|err: Error| {
logging::error!("ERROR: {}", err);
})
Expand Down
20 changes: 11 additions & 9 deletions sentinel-core/src/core/base/metric_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,23 @@ impl fmt::Display for MetricItem {
impl MetricItem {
/// cannot use String trait, since conversion may fail
pub fn from_string(line: &str) -> Result<Self> {
if line.len() == 0 {
if line.is_empty() {
return Err(Error::msg(METRIC_EMPTY_STRING_ERROR));
}
let arr: Vec<&str> = line.split(METRIC_PART_SEPARATOR).collect();
if arr.len() < 8 {
return Err(Error::msg(METRIC_INVALID_FORMAT_ERROR));
}
let mut item = MetricItem::default();
item.timestamp = arr[0].parse::<u64>()?;
item.resource = arr[2].into();
item.pass_qps = arr[3].parse::<u64>()?;
item.block_qps = arr[4].parse::<u64>()?;
item.complete_qps = arr[5].parse::<u64>()?;
item.error_qps = arr[6].parse::<u64>()?;
item.avg_rt = arr[7].parse::<u64>()?;
let mut item = MetricItem {
timestamp: arr[0].parse::<u64>()?,
resource: arr[2].into(),
pass_qps: arr[3].parse::<u64>()?,
block_qps: arr[4].parse::<u64>()?,
complete_qps: arr[5].parse::<u64>()?,
error_qps: arr[6].parse::<u64>()?,
avg_rt: arr[7].parse::<u64>()?,
..Default::default()
};
if arr.len() >= 9 {
item.occupied_pass_qps = arr[8].parse::<u64>()?;
if arr.len() >= 10 {
Expand Down
31 changes: 12 additions & 19 deletions sentinel-core/src/core/base/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ const EXIST_BLOCK_ERROR: &str = "Block type existed!";
pub fn registry_block_type(other: BlockType, desc: &'static str) -> Result<()> {
match other {
BlockType::Other(id) => {
if BLOCK_TYPE_MAP.lock().unwrap().contains_key(&id) {
Err(Error::msg(EXIST_BLOCK_ERROR))
} else {
BLOCK_TYPE_MAP.lock().unwrap().insert(id, desc);
if let std::collections::hash_map::Entry::Vacant(e) =
BLOCK_TYPE_MAP.lock().unwrap().entry(id)
{
e.insert(desc);
Ok(())
} else {
Err(Error::msg(EXIST_BLOCK_ERROR))
}
}
_ => Err(Error::msg(EXIST_BLOCK_ERROR)),
Expand All @@ -51,11 +53,11 @@ impl fmt::Display for BlockType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let BlockType::Other(id) = self {
match BLOCK_TYPE_MAP.lock().unwrap().get(id) {
Some(&desc) => return write!(f, "{}", desc),
None => return write!(f, "{}", id),
Some(&desc) => write!(f, "{}", desc),
None => write!(f, "{}", id),
}
} else {
return write!(f, "{:?}", self);
write!(f, "{:?}", self)
}
}
}
Expand Down Expand Up @@ -127,24 +129,15 @@ impl TokenResult {
}

pub fn is_pass(&self) -> bool {
match self {
Self::Pass => true,
_ => false,
}
matches!(self, Self::Pass)
}

pub fn is_blocked(&self) -> bool {
match self {
Self::Blocked(_) => true,
_ => false,
}
matches!(self, Self::Blocked(_))
}

pub fn is_wait(&self) -> bool {
match self {
Self::Wait(_) => true,
_ => false,
}
matches!(self, Self::Wait(_))
}

pub fn block_err(&self) -> Option<BlockError> {
Expand Down
Loading

0 comments on commit b72cd36

Please sign in to comment.