Skip to content

Commit

Permalink
chore(rust): apply Clippy suggestions (#1556)
Browse files Browse the repository at this point in the history
* Apply a bunch of Clippy suggestions.
* Update Rust minimal version to 1.75 because
[IpAddr::to_canonical](https://doc.rust-lang.org/std/net/enum.IpAddr.html#method.to_canonical)
is stable since then.
  • Loading branch information
imobachgs authored Sep 19, 2024
2 parents 0b66de2 + f5ae447 commit 7e2488f
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 41 deletions.
10 changes: 3 additions & 7 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
[workspace]
members = [
"agama-cli",
"agama-server",
"agama-lib",
"agama-locale-data",
]
members = ["agama-cli", "agama-server", "agama-lib", "agama-locale-data"]
resolver = "2"

[workspace.package]
rust-version = "1.74"
# IpAddr::to_canonical is stable since 1.75
rust-version = "1.75"
edition = "2021"
2 changes: 1 addition & 1 deletion rust/agama-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn edit(model: &InstallSettings, editor: &str) -> anyhow::Result<InstallSettings
let path = PathBuf::from(file.path());
write!(file, "{}", content)?;

let mut command = editor_command(&editor);
let mut command = editor_command(editor);
let status = command.arg(path.as_os_str()).status()?;
if status.success() {
return Ok(InstallSettings::from_file(path)?);
Expand Down
4 changes: 2 additions & 2 deletions rust/agama-cli/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ fn validate(path: &PathBuf) -> anyhow::Result<()> {
Ok(())
}

fn evaluate(path: &PathBuf) -> anyhow::Result<()> {
fn evaluate(path: &Path) -> anyhow::Result<()> {
let evaluator = ProfileEvaluator {};
evaluator
.evaluate(&path, stdout())
.evaluate(path, stdout())
.context("Could not evaluate the profile".to_string())?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-lib/src/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl StorageStore {
}

pub async fn load(&self) -> Result<StorageSettings, ServiceError> {
Ok(self.storage_client.get_config().await?)
self.storage_client.get_config().await
}

pub async fn store(&self, settings: &StorageSettings) -> Result<(), ServiceError> {
Expand Down
18 changes: 10 additions & 8 deletions rust/agama-lib/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ impl Store {

/// Loads the installation settings from the HTTP interface.
pub async fn load(&self) -> Result<InstallSettings, ServiceError> {
let mut settings: InstallSettings = Default::default();
settings.network = Some(self.network.load().await?);
settings.software = Some(self.software.load().await?);
settings.user = Some(self.users.load().await?);
settings.product = Some(self.product.load().await?);
settings.localization = Some(self.localization.load().await?);
let mut settings = InstallSettings {
network: Some(self.network.load().await?),
software: Some(self.software.load().await?),
user: Some(self.users.load().await?),
product: Some(self.product.load().await?),
localization: Some(self.localization.load().await?),
..Default::default()
};

let storage_settings = self.storage.load().await?;
settings.storage = storage_settings.storage.clone();
settings.storage_autoyast = storage_settings.storage_autoyast.clone();
settings.storage = storage_settings.storage;
settings.storage_autoyast = storage_settings.storage_autoyast;

// TODO: use try_join here
Ok(settings)
Expand Down
9 changes: 3 additions & 6 deletions rust/agama-server/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl Certificate {
let cert_bytes = std::fs::read(cert)?;
let key_bytes = std::fs::read(key)?;

let cert = X509::from_pem(&cert_bytes.as_slice());
let key = PKey::private_key_from_pem(&key_bytes.as_slice());
let cert = X509::from_pem(&cert_bytes);
let key = PKey::private_key_from_pem(&key_bytes);

match (cert, key) {
(Ok(c), Ok(k)) => Ok(Certificate { cert: c, key: k }),
Expand Down Expand Up @@ -106,10 +106,7 @@ impl Certificate {
builder.sign(&key, MessageDigest::sha256())?;
let cert = builder.build();

Ok(Certificate {
cert: cert,
key: key,
})
Ok(Certificate { cert, key })
}
}

Expand Down
4 changes: 2 additions & 2 deletions rust/agama-server/src/l10n/l10n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn run_with_timeout(cmd: &[&str], timeout: u64) -> Result<Option<String>, PopenE
}
}

return Ok(out);
Ok(out)
}

// the default X display to use if not configured or when X forwarding is used
Expand All @@ -84,7 +84,7 @@ fn display() -> String {
match display {
Ok(display) => {
// use the $DISPLAY value only when it is a local X server
if display.starts_with(":") {
if display.starts_with(':') {
display
} else {
// when using SSH X forwarding (e.g. "localhost:10.0") we could
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-server/src/manager/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn manager_service(dbus: zbus::Connection) -> Result<Router, ServiceEr
)
)
)]
async fn probe_action<'a>(State(state): State<ManagerState<'a>>) -> Result<(), Error> {
async fn probe_action(State(state): State<ManagerState<'_>>) -> Result<(), Error> {
let dbus = state.dbus.clone();
tokio::spawn(async move {
let result = dbus
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn ip_config_to_ipv6_dbus(ip_config: &IpConfig) -> HashMap<&str, zvariant::Value
ipv6_dbus
}

fn wireless_config_to_dbus<'a>(config: &'a WirelessConfig) -> NestedHash<'a> {
fn wireless_config_to_dbus(config: &'_ WirelessConfig) -> NestedHash<'_> {
let mut wireless: HashMap<&str, zvariant::Value> = HashMap::from([
("mode", Value::new(config.mode.to_string())),
("ssid", Value::new(config.ssid.to_vec())),
Expand Down
6 changes: 2 additions & 4 deletions rust/agama-server/src/questions/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ impl<'a> QuestionsClient<'a> {
}

pub async fn delete(&self, id: u32) -> Result<(), ServiceError> {
let question_path = ObjectPath::from(
ObjectPath::try_from(format!("/org/opensuse/Agama1/Questions/{}", id))
.context("Failed to create dbus path")?,
);
let question_path = ObjectPath::try_from(format!("/org/opensuse/Agama1/Questions/{}", id))
.context("Failed to create a D-Bus path")?;

self.questions_proxy
.delete(&question_path)
Expand Down
10 changes: 2 additions & 8 deletions rust/agama-server/src/storage/web/dasd/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl DASDDeviceStream {
let mut cache: ObjectsCache<DASDDevice> = Default::default();
let client = DASDClient::new(dbus.clone()).await?;
for (path, device) in client.devices().await? {
cache.add(path.into(), device);
cache.add(path, device);
}

Ok(Self {
Expand Down Expand Up @@ -250,13 +250,7 @@ impl Stream for DASDFormatJobStream {
Poll::Ready(loop {
let item = ready!(pinned.inner.as_mut().poll_next(cx));
let next_value = match item {
Some(change) => {
if let Some(event) = Self::handle_change(change) {
Some(event)
} else {
None
}
}
Some(change) => Self::handle_change(change),
None => break None,
};
if next_value.is_some() {
Expand Down

0 comments on commit 7e2488f

Please sign in to comment.