Skip to content

Commit

Permalink
Merge pull request #185 from sr-gi/clippy-inline
Browse files Browse the repository at this point in the history
Formats strings to use inline params when possible
  • Loading branch information
sr-gi authored Feb 6, 2023
2 parents 125709b + 4bcfbf7 commit 43f9971
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 258 deletions.
4 changes: 2 additions & 2 deletions teos-common/src/appointment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl std::str::FromStr for AppointmentStatus {
"being_watched" => Ok(AppointmentStatus::BeingWatched),
"dispute_responded" => Ok(AppointmentStatus::DisputeResponded),
"not_found" => Ok(AppointmentStatus::NotFound),
_ => Err(format!("Unknown status: {}", s)),
_ => Err(format!("Unknown status: {s}")),
}
}
}
Expand All @@ -107,7 +107,7 @@ impl fmt::Display for AppointmentStatus {
AppointmentStatus::DisputeResponded => "dispute_responded",
AppointmentStatus::NotFound => "not_found",
};
write!(f, "{}", s)
write!(f, "{s}")
}
}

Expand Down
9 changes: 3 additions & 6 deletions teos-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,15 @@ impl TryFrom<serde_json::Value> for UserId {
UserId::try_from(a.pop().unwrap())
} else {
Err(format!(
"Unexpected json format. Expected a single parameter. Received: {}",
param_count
"Unexpected json format. Expected a single parameter. Received: {param_count}"
))
}
}
serde_json::Value::Object(mut m) => {
let param_count = m.len();
if param_count > 1 {
Err(format!(
"Unexpected json format. Expected a single parameter. Received: {}",
param_count
"Unexpected json format. Expected a single parameter. Received: {param_count}"
))
} else {
UserId::try_from(json!(m
Expand All @@ -95,8 +93,7 @@ impl TryFrom<serde_json::Value> for UserId {
}
}
_ => Err(format!(
"Unexpected request format. Expected: user_id/tower_id. Received: '{}'",
value
"Unexpected request format. Expected: user_id/tower_id. Received: '{value}'"
)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion teos-common/src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ impl std::fmt::Display for Endpoint {

impl Endpoint {
pub fn path(&self) -> String {
format!("/{}", self)
format!("/{self}")
}
}
4 changes: 2 additions & 2 deletions teos-common/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl std::str::FromStr for AddressType {
match s {
"ipv4" => Ok(AddressType::IpV4),
"torv3" => Ok(AddressType::TorV3),
_ => Err(format!("Unknown type: {}", s)),
_ => Err(format!("Unknown type: {s}")),
}
}
}
Expand All @@ -38,7 +38,7 @@ impl fmt::Display for AddressType {
AddressType::IpV4 => "ipv4",
AddressType::TorV3 => "torv3",
};
write!(f, "{}", s)
write!(f, "{s}")
}
}

Expand Down
9 changes: 4 additions & 5 deletions teos/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@ impl ApiError {

fn missing_field(field_name: &str) -> Rejection {
reject::custom(Self::new(
format!("missing field `{}`", field_name),
format!("missing field `{field_name}`"),
errors::MISSING_FIELD,
))
}

fn empty_field(field_name: &str) -> Rejection {
reject::custom(Self::new(
format!("`{}` field is empty", field_name),
format!("`{field_name}` field is empty"),
errors::EMPTY_FIELD,
))
}

fn wrong_field_length(field_name: &str, field_size: usize, expected_size: usize) -> Rejection {
reject::custom(Self::new(
format!(
"Wrong `{}` field size. Expected {}, received {}",
field_name, expected_size, field_size
"Wrong `{field_name}` field size. Expected {expected_size}, received {field_size}"
),
errors::WRONG_FIELD_SIZE,
))
Expand Down Expand Up @@ -104,7 +103,7 @@ fn parse_grpc_response<T: serde::Serialize>(
}
Err(s) => {
let (status_code, error_code) = match_status(&s);
log::debug!("Request failed, error_code={}", error_code);
log::debug!("Request failed, error_code={error_code}");
log::debug!("Response: {}", serde_json::json!(s.message()));
(
reply::json(&ApiError::new(s.message().into(), error_code)),
Expand Down
6 changes: 3 additions & 3 deletions teos/src/api/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl PublicTowerServices for Arc<InternalAPI> {
)),
AddAppointmentFailure::SubscriptionExpired(x) => Err(Status::new(
Code::Unauthenticated,
format!("Your subscription expired at {}", x),
format!("Your subscription expired at {x}"),
)),
AddAppointmentFailure::AlreadyTriggered => Err(Status::new(
Code::AlreadyExists,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl PublicTowerServices for Arc<InternalAPI> {
)),
GetAppointmentFailure::SubscriptionExpired(x) => Err(Status::new(
Code::Unauthenticated,
format!("Your subscription expired at {}", x),
format!("Your subscription expired at {x}"),
)),
},
}
Expand All @@ -213,7 +213,7 @@ impl PublicTowerServices for Arc<InternalAPI> {
),
GetSubscriptionInfoFailure::SubscriptionExpired(x) => Status::new(
Code::Unauthenticated,
format!("Your subscription expired at {}", x),
format!("Your subscription expired at {x}"),
),
})?;

Expand Down
6 changes: 3 additions & 3 deletions teos/src/api/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl TorAPI {
log::info!("Loading Tor secret key from disk");
let key = fs::read(path.join("onion_v3_sk"))
.await
.map_err(|e| log::warn!("Tor secret key cannot be loaded. {}", e))
.map_err(|e| log::warn!("Tor secret key cannot be loaded. {e}"))
.ok()?;
let key: [u8; 64] = key
.try_into()
Expand All @@ -62,7 +62,7 @@ impl TorAPI {
/// Stores a Tor key to disk.
async fn store_sk(key: &TorSecretKeyV3, path: PathBuf) {
if let Err(e) = fs::write(path.join("onion_v3_sk"), key.as_bytes()).await {
log::error!("Cannot store Tor secret key. {}", e);
log::error!("Cannot store Tor secret key. {e}");
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ impl TorAPI {
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("failed to create onion hidden service: {}", e),
format!("failed to create onion hidden service: {e}"),
)
})?;

Expand Down
7 changes: 2 additions & 5 deletions teos/src/bitcoin_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> BitcoindClient<'a> {
teos_network: &'a str,
) -> std::io::Result<BitcoindClient<'a>> {
let http_endpoint = HttpEndpoint::for_host(host.to_owned()).with_port(port);
let rpc_credentials = base64::encode(&format!("{}:{}", rpc_user, rpc_password));
let rpc_credentials = base64::encode(&format!("{rpc_user}:{rpc_password}"));
let bitcoind_rpc_client = RpcClient::new(&rpc_credentials, http_endpoint)?;

let client = Self {
Expand All @@ -97,10 +97,7 @@ impl<'a> BitcoindClient<'a> {
if btc_network != teos_network {
Err(Error::new(
ErrorKind::InvalidInput,
format!(
"bitcoind is running on {} but teosd is set to run on {}",
btc_network, teos_network
),
format!("bitcoind is running on {btc_network} but teosd is set to run on {teos_network}"),
))
} else {
Ok(client)
Expand Down
23 changes: 7 additions & 16 deletions teos/src/carrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ impl Carrier {
Err(JsonRpcError(RpcError(rpcerr))) => match rpcerr.code {
// Since we're pushing a raw transaction to the network we can face several rejections
rpc_errors::RPC_VERIFY_REJECTED => {
log::error!("Transaction couldn't be broadcast. {:?}", rpcerr);
log::error!("Transaction couldn't be broadcast. {rpcerr:?}");
ConfirmationStatus::Rejected(rpc_errors::RPC_VERIFY_REJECTED)
}
rpc_errors::RPC_VERIFY_ERROR => {
log::error!("Transaction couldn't be broadcast. {:?}", rpcerr);
log::error!("Transaction couldn't be broadcast. {rpcerr:?}");
ConfirmationStatus::Rejected(rpc_errors::RPC_VERIFY_ERROR)
}
rpc_errors::RPC_VERIFY_ALREADY_IN_CHAIN => {
Expand All @@ -122,10 +122,7 @@ impl Carrier {
}
_ => {
// If something else happens (unlikely but possible) log it so we can treat it in future releases.
log::error!(
"Unexpected rpc error when calling sendrawtransaction: {:?}",
rpcerr
);
log::error!("Unexpected rpc error when calling sendrawtransaction: {rpcerr:?}");
ConfirmationStatus::Rejected(errors::UNKNOWN_JSON_RPC_EXCEPTION)
}
},
Expand All @@ -137,7 +134,7 @@ impl Carrier {
}
Err(e) => {
// TODO: This may need finer catching.
log::error!("Unexpected error when calling sendrawtransaction: {:?}", e);
log::error!("Unexpected error when calling sendrawtransaction: {e:?}");
ConfirmationStatus::Rejected(errors::UNKNOWN_JSON_RPC_EXCEPTION)
}
};
Expand All @@ -159,15 +156,12 @@ impl Carrier {
Ok(tx) => tx.blockhash.is_none(),
Err(JsonRpcError(RpcError(rpcerr))) => match rpcerr.code {
rpc_errors::RPC_INVALID_ADDRESS_OR_KEY => {
log::info!("Transaction not found in mempool: {}", txid);
log::info!("Transaction not found in mempool: {txid}");
false
}
e => {
// DISCUSS: This could result in a silent error with unknown consequences
log::error!(
"Unexpected error code when calling getrawtransaction: {}",
e
);
log::error!("Unexpected error code when calling getrawtransaction: {e}");
false
}
},
Expand All @@ -180,10 +174,7 @@ impl Carrier {
// TODO: This may need finer catching.
Err(e) => {
// DISCUSS: This could result in a silent error with unknown consequences
log::error!(
"Unexpected JSONRPCError when calling getrawtransaction: {}",
e
);
log::error!("Unexpected JSONRPCError when calling getrawtransaction: {e}");
false
}
}
Expand Down
2 changes: 1 addition & 1 deletion teos/src/chain_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
Err(e) => match e.kind() {
BlockSourceErrorKind::Persistent => {
// FIXME: This may need finer catching
log::error!("Unexpected persistent error: {:?}", e);
log::error!("Unexpected persistent error: {e:?}");
}
BlockSourceErrorKind::Transient => {
// Treating all transient as connection errors at least for now.
Expand Down
10 changes: 5 additions & 5 deletions teos/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {

// Create data dir if it does not exist
fs::create_dir_all(&path).await.unwrap_or_else(|e| {
eprintln!("Cannot create data dir: {:?}", e);
eprintln!("Cannot create data dir: {e:?}");
std::process::exit(1);
});

Expand Down Expand Up @@ -51,13 +51,13 @@ async fn main() {
.expect("Cannot create channel from endpoint")
.tls_config(tls)
.unwrap_or_else(|e| {
eprintln!("Could not configure tls: {:?}", e);
eprintln!("Could not configure tls: {e:?}");
std::process::exit(1);
})
.connect()
.await
.unwrap_or_else(|e| {
eprintln!("Could not connect to tower: {:?}", e);
eprintln!("Could not connect to tower: {e:?}");
std::process::exit(1);
});

Expand All @@ -83,7 +83,7 @@ async fn main() {
Err(status) => println!("{}", status.message()),
}
}
Err(e) => println!("{}", e),
Err(e) => println!("{e}"),
};
}
Command::GetTowerInfo => {
Expand All @@ -109,7 +109,7 @@ async fn main() {
Err(status) => println!("{}", status.message()),
}
}
Err(e) => println!("{}", e),
Err(e) => println!("{e}"),
};
}
Command::Stop => {
Expand Down
2 changes: 1 addition & 1 deletion teos/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: PathBuf) -> T {
match std::fs::read(path) {
Ok(file_content) => toml::from_slice::<T>(&file_content).map_or_else(
|e| {
eprintln!("Couldn't parse config file: {}", e);
eprintln!("Couldn't parse config file: {e}");
T::default()
},
|config| config,
Expand Down
Loading

0 comments on commit 43f9971

Please sign in to comment.