Skip to content

Commit 17f9dee

Browse files
committed
fix: update code to solve clippy issues
1 parent 55f4df3 commit 17f9dee

20 files changed

Lines changed: 49 additions & 36 deletions

File tree

updatehub-cloud-sdk/src/api.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
use serde::Serialize;
6-
use std::{collections::BTreeMap, fs, path::Path};
6+
use std::{collections::BTreeMap, fmt::Write, fs, path::Path};
77

88
#[derive(Debug)]
99
pub enum ProbeResponse {
@@ -33,7 +33,7 @@ pub struct FirmwareMetadata<'a> {
3333

3434
pub struct MetadataValue<'a>(pub &'a BTreeMap<String, Vec<String>>);
3535

36-
impl<'a> serde::ser::Serialize for MetadataValue<'a> {
36+
impl serde::ser::Serialize for MetadataValue<'_> {
3737
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
3838
where
3939
S: serde::ser::Serializer,
@@ -59,7 +59,11 @@ impl UpdatePackage {
5959
}
6060

6161
pub fn package_uid(&self) -> String {
62-
openssl::sha::sha256(&self.raw).iter().map(|c| format!("{:02x}", c)).collect()
62+
openssl::sha::sha256(&self.raw).iter().fold(String::new(), |mut output, c| {
63+
let _ = write!(output, "{c:02x}");
64+
65+
output
66+
})
6367
}
6468

6569
pub fn version(&self) -> &str {

updatehub-cloud-sdk/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'a> Client<'a> {
8585

8686
let response = self
8787
.client
88-
.post(&format!("{}/upgrades", &self.server))
88+
.post(format!("{}/upgrades", &self.server))
8989
.header("api-retries", num_retries.to_string())
9090
.json(&firmware)
9191
.send()
@@ -181,7 +181,7 @@ impl<'a> Client<'a> {
181181
let payload =
182182
Payload { state, firmware, package_uid, previous_state, error_message, current_log };
183183

184-
self.client.post(&format!("{}/report", &self.server)).json(&payload).send().await?;
184+
self.client.post(format!("{}/report", &self.server)).json(&payload).send().await?;
185185
Ok(())
186186
}
187187
}

updatehub-sdk/src/api/info/firmware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'de> Deserialize<'de> for MetadataValue {
125125
}
126126
}
127127

128-
impl<'a> Index<&'a str> for MetadataValue {
128+
impl Index<&str> for MetadataValue {
129129
type Output = Vec<String>;
130130

131131
#[inline]

updatehub-sdk/src/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Client {
4343
/// This method fails when cannot complete the request at the address or
4444
/// cannot parse the body json as a `info::Response`.
4545
pub async fn info(&self) -> Result<api::info::Response> {
46-
let response = self.client.get(&format!("{}/info", self.server_address)).send().await?;
46+
let response = self.client.get(format!("{}/info", self.server_address)).send().await?;
4747

4848
match response.status() {
4949
StatusCode::OK => Ok(response.json().await?),
@@ -110,7 +110,7 @@ impl Client {
110110
pub async fn local_install(&self, file: &Path) -> Result<api::state::Response> {
111111
let response = self
112112
.client
113-
.post(&format!("{}/local_install", self.server_address))
113+
.post(format!("{}/local_install", self.server_address))
114114
.json(&api::local_install::Request { file: file.to_owned() })
115115
.send()
116116
.await?;
@@ -139,7 +139,7 @@ impl Client {
139139
pub async fn remote_install(&self, url: &str) -> Result<api::state::Response> {
140140
let response = self
141141
.client
142-
.post(&format!("{}/remote_install", self.server_address))
142+
.post(format!("{}/remote_install", self.server_address))
143143
.json(&api::remote_install::Request { url: url.to_owned() })
144144
.send()
145145
.await?;
@@ -167,7 +167,7 @@ impl Client {
167167
pub async fn abort_download(&self) -> Result<api::state::Response> {
168168
let response = self
169169
.client
170-
.post(&format!("{}/update/download/abort", self.server_address))
170+
.post(format!("{}/update/download/abort", self.server_address))
171171
.send()
172172
.await?;
173173

@@ -193,7 +193,7 @@ impl Client {
193193
/// This method fails when cannot complete the request at the address or
194194
/// cannot parse the body json as a `log::Log`.
195195
pub async fn log(&self) -> Result<api::log::Log> {
196-
let response = self.client.get(&format!("{}/log", self.server_address)).send().await?;
196+
let response = self.client.get(format!("{}/log", self.server_address)).send().await?;
197197

198198
match response.status() {
199199
StatusCode::OK => Ok(response.json().await?),

updatehub-sdk/src/listener.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl StateChange {
103103
F: Fn(Handler) -> Fut + 'static,
104104
Fut: Future<Output = Result<()>> + 'static,
105105
{
106-
self.callbacks.entry(state).or_insert_with(Vec::new).push(Box::new(move |d| Box::pin(f(d))))
106+
self.callbacks.entry(state).or_default().push(Box::new(move |d| Box::pin(f(d))))
107107
}
108108

109109
/// Start the agent to listen for messages on the socket.

updatehub/src/cloud_mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use cloud::{Error, Result, api};
66
use std::{cell::RefCell, marker::PhantomData, path::Path};
77

88
std::thread_local! {
9-
static RESPONSE_CONFIG: RefCell<FakeResponse> = RefCell::new(FakeResponse::NoUpdate);
9+
static RESPONSE_CONFIG: RefCell<FakeResponse> = const { RefCell::new(FakeResponse::NoUpdate) };
1010
}
1111

1212
std::thread_local! {
13-
static OBJECT_DATA: RefCell<Option<Vec<u8>>> = RefCell::new(Option::None);
13+
static OBJECT_DATA: RefCell<Option<Vec<u8>>> = const { RefCell::new(Option::None) };
1414
}
1515

1616
pub(crate) enum FakeResponse {

updatehub/src/firmware/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ pub(crate) fn create_fake_starup_callbacks(metadata_dir: &Path, output_file: &Pa
124124
let mut file = fs::OpenOptions::new()
125125
.write(true)
126126
.create(true)
127-
.open(&metadata_dir.join(script))
127+
.truncate(false)
128+
.open(metadata_dir.join(script))
128129
.unwrap();
129130
writeln!(file, "#!/bin/sh\necho $0 >> {}", output_file.to_string_lossy()).unwrap();
130131
let mut permissions = fs::metadata(metadata_dir).unwrap().permissions();

updatehub/src/mem_drain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Serialize for MemDrain {
4949
}
5050
}
5151

52+
#[allow(clippy::to_string_trait_impl)]
5253
impl ToString for MemDrain {
5354
fn to_string(&self) -> String {
5455
let records = self.records.read().unwrap();

updatehub/src/object/installer/copy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Installer for objects::Copy {
7878
.read(true)
7979
.write(true)
8080
.create(true)
81-
.truncate(true)
81+
.truncate(false)
8282
.open(&dest)
8383
.await
8484
.log_error_msg("failed to open target file")?,
@@ -165,7 +165,7 @@ mod tests {
165165
// Generate the source file
166166
let download_dir = tempfile::tempdir()?;
167167
let mut source = tempfile::NamedTempFile::new_in(download_dir.path())?;
168-
let original_data = iter::repeat(DEFAULT_BYTE).take(FILE_SIZE).collect::<Vec<_>>();
168+
let original_data = std::iter::repeat_n(DEFAULT_BYTE, FILE_SIZE).collect::<Vec<_>>();
169169
let data = if compressed {
170170
let mut e = GzEncoder::new(Vec::new(), Compression::default());
171171
e.write_all(&original_data).unwrap();
@@ -182,7 +182,7 @@ mod tests {
182182

183183
fs::File::create(&file)
184184
.await?
185-
.write_all(&iter::repeat(ORIGINAL_BYTE).take(FILE_SIZE).collect::<Vec<_>>())
185+
.write_all(&std::iter::repeat_n(ORIGINAL_BYTE, FILE_SIZE).collect::<Vec<_>>())
186186
.await?;
187187

188188
if let Some(mode) = perm.target_mode {

updatehub/src/object/installer/imxkobs.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ impl Installer for objects::Imxkobs {
2525

2626
let should_skip_install =
2727
super::should_skip_install(&self.install_if_different, &self.sha256sum, async {
28-
let path = self
29-
.chip_0_device_path
30-
.as_ref()
31-
.map(PathBuf::clone)
32-
.unwrap_or_else(|| PathBuf::from("/dev/mtd0"));
28+
let path =
29+
self.chip_0_device_path.clone().unwrap_or_else(|| PathBuf::from("/dev/mtd0"));
3330
let f = path.file_name().ok_or(Error::InvalidPath)?;
3431
let mut file_name = f.to_os_string();
3532
file_name.push("ro");

0 commit comments

Comments
 (0)