Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust Code Cleanup #293

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/src/app_dirs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait GetConfDir {
if !dir.exists() {
create_dir_all(&dir)?;
}
return Ok(dir);
Ok(dir)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/byte_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize, Serializer};
use serde::{Deserialize, Serialize};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Encoding {
Expand Down
22 changes: 11 additions & 11 deletions src-tauri/src/conn_pool/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl DeviceConnection {
last_ok: Mutex::new(true),
};
log::info!("{:?} created", connection);
return Ok(connection);
Ok(connection)
}

pub(super) fn reset_last_ok(&self) {
Expand All @@ -71,7 +71,7 @@ impl DeviceConnection {
}

pub(crate) fn session_init(session: &Session) -> Result<(), Error> {
let kex = vec![
let kex = [
"curve25519-sha256",
"[email protected]",
"ecdh-sha2-nistp256",
Expand All @@ -84,7 +84,7 @@ impl DeviceConnection {
"diffie-hellman-group1-sha1",
"diffie-hellman-group14-sha1",
];
let hmac = vec![
let hmac = [
"[email protected]",
"[email protected]",
"hmac-sha2-256",
Expand All @@ -93,7 +93,7 @@ impl DeviceConnection {
"hmac-sha1",
"hmac-md5",
];
let key_types = vec![
let key_types = [
"ssh-ed25519",
"ecdsa-sha2-nistp521",
"ecdsa-sha2-nistp384",
Expand All @@ -120,21 +120,21 @@ impl DeviceConnection {
session.set_option(SshOption::KnownHosts(Some(format!("/dev/null"))))?;
session.set_option(SshOption::GlobalKnownHosts(Some(format!("/dev/null"))))?;
}
return Ok(());
Ok(())
}
}

impl Deref for DeviceConnection {
type Target = Session;

fn deref(&self) -> &Self::Target {
return &self.session;
&self.session
}
}

impl DerefMut for DeviceConnection {
fn deref_mut(&mut self) -> &mut Self::Target {
return &mut self.session;
&mut self.session
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ impl DeviceConnectionUserInfo {
unhandled: false,
});
}
return Ok(DeviceConnectionUserInfo::parse(&buf));
Ok(DeviceConnectionUserInfo::parse(&buf))
}

fn parse(s: &str) -> Option<DeviceConnectionUserInfo> {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl DeviceConnectionUserInfo {
let (Some(uid), Some(gid)) = (uid, gid) else {
return None;
};
return Some(DeviceConnectionUserInfo { uid, gid, groups });
Some(DeviceConnectionUserInfo { uid, gid, groups })
}
}

Expand All @@ -224,13 +224,13 @@ impl Id {
let Some(caps) = regex.captures(s) else {
return None;
};
return Some(Self {
Some(Self {
id: u32::from_str_radix(caps.get(1).unwrap().as_str(), 10).unwrap(),
name: caps.get(2).map(|s| {
s.as_str()
.trim_matches(|c| c == '(' || c == ')')
.to_string()
}),
});
})
}
}
18 changes: 9 additions & 9 deletions src-tauri/src/conn_pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ impl DeviceConnectionPool {
last_error: last_error.clone(),
}))
.build_unchecked(DeviceConnectionManager { device, ssh_dir });
return DeviceConnectionPool { inner, last_error };
DeviceConnectionPool { inner, last_error }
}

pub fn get(&self) -> Result<ManagedDeviceConnection, Error> {
return match self.inner.get() {
match self.inner.get() {
Ok(c) => {
c.reset_last_ok();
Ok(c)
Expand All @@ -39,7 +39,7 @@ impl DeviceConnectionPool {
}
}));
}
};
}
}
}

Expand All @@ -48,18 +48,18 @@ impl ManageConnection for DeviceConnectionManager {
type Error = Error;

fn connect(&self) -> Result<Self::Connection, Self::Error> {
return DeviceConnection::new(self.device.clone(), self.ssh_dir.as_deref());
DeviceConnection::new(self.device.clone(), self.ssh_dir.as_deref())
}

fn is_valid(&self, _: &mut Self::Connection) -> Result<(), Self::Error> {
return Ok(());
Ok(())
}

fn has_broken(&self, conn: &mut Self::Connection) -> bool {
if !conn.is_connected() {
return true;
}
return conn.last_ok.lock().unwrap().eq(&false);
conn.last_ok.lock().unwrap().eq(&false)
}
}

Expand All @@ -77,15 +77,15 @@ impl HandleError<Error> for DeviceConnectionErrorHandler {
if *error == Error::Disconnected {
return num_retries < 3;
}
return false;
false
}
}

impl Clone for DeviceConnectionPool {
fn clone(&self) -> Self {
return DeviceConnectionPool {
DeviceConnectionPool {
inner: self.inner.clone(),
last_error: self.last_error.clone(),
};
}
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/device_manager/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use crate::device_manager::Device;

impl Device {
pub(crate) fn valid_passphrase(&self) -> Option<String> {
return self.passphrase.clone().filter(|s| !s.is_empty());
self.passphrase.clone().filter(|s| !s.is_empty())
}
}
14 changes: 7 additions & 7 deletions src-tauri/src/device_manager/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::error::Error;

pub(crate) async fn read(conf_dir: &Path) -> Result<Vec<Device>, Error> {
let conf_dir = conf_dir.to_path_buf();
return tauri::async_runtime::spawn_blocking(move || -> Result<Vec<Device>, Error> {
tauri::async_runtime::spawn_blocking(move || -> Result<Vec<Device>, Error> {
let path = devices_file_path(&conf_dir);
let file = match File::open(path.as_path()) {
Ok(file) => file,
Expand All @@ -30,12 +30,12 @@ pub(crate) async fn read(conf_dir: &Path) -> Result<Vec<Device>, Error> {
.collect());
})
.await
.expect("critical failure in app::io::read task");
.expect("critical failure in app::io::read task")
}

pub(crate) async fn write(devices: Vec<Device>, conf_dir: &Path) -> Result<(), Error> {
let conf_dir = conf_dir.to_path_buf();
return tauri::async_runtime::spawn_blocking(move || -> Result<(), Error> {
tauri::async_runtime::spawn_blocking(move || -> Result<(), Error> {
let path = devices_file_path(&conf_dir);
let file = match File::create(path.as_path()) {
Ok(file) => file,
Expand All @@ -60,25 +60,25 @@ pub(crate) async fn write(devices: Vec<Device>, conf_dir: &Path) -> Result<(), E
return Ok(());
})
.await
.expect("critical failure in app::io::write task");
.expect("critical failure in app::io::write task")
}

fn devices_file_path(conf_dir: &Path) -> PathBuf {
return conf_dir.join("novacom-devices.json");
conf_dir.join("novacom-devices.json")
}

#[cfg(not(unix))]
fn fix_devices_json_perm(path: PathBuf) -> Result<(), Error> {
let mut perm = fs::metadata(path.clone())?.permissions();
perm.set_readonly(false);
fs::set_permissions(path, perm)?;
return Ok(());
Ok(())
}

#[cfg(unix)]
fn fix_devices_json_perm(path: PathBuf) -> Result<(), Error> {
use std::os::unix::fs::PermissionsExt;
let perm = fs::Permissions::from_mode(0o644);
fs::set_permissions(path, perm)?;
return Ok(());
Ok(())
}
8 changes: 4 additions & 4 deletions src-tauri/src/device_manager/privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::Error;

impl PrivateKey {
pub fn content(&self, ssh_dir: Option<&Path>) -> Result<String, Error> {
return match self {
match self {
PrivateKey::Path { name } => {
let mut secret_file =
std::fs::File::open(ssh_dir.ok_or(Error::bad_config())?.join(name))?;
Expand All @@ -17,18 +17,18 @@ impl PrivateKey {
Ok(secret)
}
PrivateKey::Data { data } => Ok(data.clone()),
};
}
}

pub fn name(&self, passphrase: Option<String>) -> Result<String, Error> {
return match self {
match self {
PrivateKey::Path { name } => Ok(name.clone()),
PrivateKey::Data { data } => Ok(String::from(
&hex::encode(
SshKey::from_privkey_base64(data, passphrase.as_deref())?
.get_public_key_hash(PublicKeyHashType::Sha256)?,
)[..10],
)),
};
}
}
}
Loading
Loading