Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Aug 27, 2024
1 parent 420ba62 commit fddf9cb
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 115 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ares-device/src/picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ impl PickDevice for DeviceManager {
}
None => devices.iter().find(|d| d.default.unwrap_or(false)).cloned(),
};
return Ok(device);
Ok(device)
}
}

impl FromStr for DeviceSelection {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
return if s.is_empty() {
if s.is_empty() {
Ok(Self::Pick)
} else {
Ok(Self::Name(s.to_string()))
};
}
}
}
}
20 changes: 11 additions & 9 deletions ares-device/src/picker/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,30 @@ impl PickPrompt for PickPromptWindows {

nwg::dispatch_thread_events();

return devices
devices
.get(ui.index.lock().unwrap().clone() as usize)
.map(|d| d.as_ref().clone());
.map(|d| d.as_ref().clone())
}
}

#[derive(Default, NwgUi)]
pub struct PickPromptApp {
#[nwg_control(size: (400, 500), center: true, topmost:true, title: "Select Device", flags: "WINDOW|VISIBLE")]
#[nwg_events( OnWindowClose: [PickPromptApp::on_close] )]
#[nwg_control(size: (400, 500), center: true, topmost:true, title: "Select Device", flags: "WINDOW|VISIBLE"
)]
#[nwg_events( OnWindowClose: [PickPromptApp::on_close])]
window: nwg::Window,

#[nwg_control(size: (380, 420), position: (10, 10))]
#[nwg_events( OnListBoxSelect: [PickPromptApp::on_selection_change], OnListBoxDoubleClick: [PickPromptApp::on_confirm] )]
#[nwg_events( OnListBoxSelect: [PickPromptApp::on_selection_change], OnListBoxDoubleClick: [PickPromptApp::on_confirm]
)]
devices: nwg::ListBox<DeviceEntry>,

#[nwg_control(text: "Select", size: (185, 60), position: (10, 420), enabled: false)]
#[nwg_events( OnButtonClick: [PickPromptApp::on_confirm] )]
#[nwg_events( OnButtonClick: [PickPromptApp::on_confirm])]
ok: nwg::Button,

#[nwg_control(text: "Cancel", size: (185, 60), position: (205, 420))]
#[nwg_events( OnButtonClick: [PickPromptApp::on_cancel] )]
#[nwg_events( OnButtonClick: [PickPromptApp::on_cancel])]
cancel: nwg::Button,

index: Mutex<i32>,
Expand All @@ -73,11 +75,11 @@ struct DeviceEntry {

impl Display for DeviceEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
return if let Some(device) = &self.device {
if let Some(device) = &self.device {
f.write_str(&device.name)
} else {
f.write_str("<none>")
};
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ares-install/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ impl RemoveApp for DeviceSession {
Err(e) => Some(Err(e.into())),
};

return result.unwrap();
result.unwrap()
}
}
6 changes: 4 additions & 2 deletions ares-package/src/input/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::needless_return)]

use std::io::{Error, ErrorKind, Read, Result};

use serde::Deserialize;
Expand All @@ -16,11 +18,11 @@ pub struct AppInfo {

impl ParseFrom for AppInfo {
fn parse_from<R: Read>(reader: R) -> Result<AppInfo> {
return serde_json::from_reader(reader).map_err(|e| {
serde_json::from_reader(reader).map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("Invalid appinfo.json: {e:?}"),
)
});
})
}
}
10 changes: 5 additions & 5 deletions ares-package/src/input/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl DataInfo {
app: app_info.id.clone(),
services: services.iter().map(|info| info.info.id.clone()).collect(),
};
let mut package_info_data = serde_json::to_vec_pretty(&package_info).unwrap();
let mut package_info_data = serde_json::to_vec_pretty(&package_info)?;
package_info_data.push(b'\n');
return Ok(DataInfo {
Ok(DataInfo {
package: package_info,
package_data: package_info_data,
app: ComponentInfo {
Expand All @@ -83,7 +83,7 @@ impl DataInfo {
},
services,
excludes,
});
})
}
}

Expand Down Expand Up @@ -111,9 +111,9 @@ impl Validation for DataInfo {
"Mixed architecture is not allowed",
));
}
return Ok(ValidationInfo {
Ok(ValidationInfo {
arch: archs.iter().next().cloned(),
size: size_sum,
});
})
}
}
4 changes: 2 additions & 2 deletions ares-package/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn filter_by_excludes(entry: &DirEntry, excludes: Option<&Regex>) ->
if let Some(exclude) = excludes {
return !exclude.is_match(entry.path().to_slash_lossy().as_ref());
}
return true;
true
}

pub(crate) fn dir_size<P: AsRef<Path>>(path: P, excludes: Option<&Regex>) -> Result<u64> {
Expand All @@ -27,5 +27,5 @@ pub(crate) fn dir_size<P: AsRef<Path>>(path: P, excludes: Option<&Regex>) -> Res
let entry = entry?;
size += entry.metadata()?.len();
}
return Ok(size);
Ok(size)
}
4 changes: 2 additions & 2 deletions ares-package/src/input/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub struct ServiceInfo {

impl ParseFrom for ServiceInfo {
fn parse_from<R: Read>(reader: R) -> Result<ServiceInfo> {
return serde_json::from_reader(reader).map_err(|e| {
serde_json::from_reader(reader).map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("Invalid services.json: {e:?}"),
)
});
})
}
}
22 changes: 11 additions & 11 deletions ares-package/src/input/validation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fmt::Display;
use std::fs::File;
use std::io::{Error, ErrorKind, Result};
use std::path::Path;
use std::str::FromStr;

use elf::ElfStream;
use elf::endian::AnyEndian;
use elf::to_str::e_machine_to_string;
use elf::ElfStream;

use crate::input::app::AppInfo;
use crate::input::data::ComponentInfo;
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Validation for ComponentInfo<AppInfo> {
if self.info.r#type == "native" {
arch = infer_arch(self.path.join(&self.info.main))?;
}
return Ok(ValidationInfo { arch, size });
Ok(ValidationInfo { arch, size })
}
}

Expand All @@ -48,17 +49,18 @@ impl Validation for ComponentInfo<ServiceInfo> {
arch = infer_arch(self.path.join(executable))?;
}
}
return Ok(ValidationInfo { arch, size });
Ok(ValidationInfo { arch, size })
}
}

impl ToString for PackageArch {
fn to_string(&self) -> String {
match self {
impl Display for PackageArch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
PackageArch::ARM => String::from("arm"),
PackageArch::ALL => String::from("all"),
PackageArch::X86(s) => s.clone(),
}
};
write!(f, "{}", str)
}
}

Expand All @@ -75,17 +77,15 @@ impl FromStr for PackageArch {
}
}



fn infer_arch<P: AsRef<Path>>(path: P) -> Result<Option<PackageArch>> {
let elf = ElfStream::<AnyEndian, _>::open_stream(File::open(path.as_ref())?)
.map_err(|e| Error::new(ErrorKind::InvalidData, format!("Bad binary: {e:?}")))?;
return match elf.ehdr.e_machine {
match elf.ehdr.e_machine {
elf::abi::EM_ARM => Ok(Some(PackageArch::ARM)),
elf::abi::EM_386 => Ok(Some(PackageArch::X86(String::from("x86")))),
e => Err(Error::new(
ErrorKind::InvalidData,
format!("Unsupported binary machine type {}", e_machine_to_string(e)),
)),
};
}
}
33 changes: 12 additions & 21 deletions ares-package/src/packaging/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,25 @@ where
let mut ar_header = ArHeader::new(b"control.tar.gz".to_vec(), control_tar_gz.len() as u64);
ar_header.set_mode(0o100644);
ar_header.set_mtime(mtime);
return self.append(&ar_header, Cursor::new(control_tar_gz));
self.append(&ar_header, Cursor::new(control_tar_gz))
}
}

impl Display for ControlInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Package: {}\n", self.package))
.unwrap();
f.write_fmt(format_args!("Version: {}\n", self.version))
.unwrap();
f.write_fmt(format_args!("Section: {}\n", "misc")).unwrap();
f.write_fmt(format_args!("Priority: {}\n", "optional"))
.unwrap();
f.write_fmt(format_args!("Architecture: {}\n", self.architecture))
.unwrap();
f.write_fmt(format_args!("Installed-Size: {}\n", self.installed_size))
.unwrap();
f.write_fmt(format_args!("Maintainer: {}\n", "N/A <[email protected]>"))
.unwrap();
f.write_fmt(format_args!("Package: {}\n", self.package))?;
f.write_fmt(format_args!("Version: {}\n", self.version))?;
f.write_fmt(format_args!("Section: {}\n", "misc"))?;
f.write_fmt(format_args!("Priority: {}\n", "optional"))?;
f.write_fmt(format_args!("Architecture: {}\n", self.architecture))?;
f.write_fmt(format_args!("Installed-Size: {}\n", self.installed_size))?;
f.write_fmt(format_args!("Maintainer: {}\n", "N/A <[email protected]>"))?;
f.write_fmt(format_args!(
"Description: {}\n",
"This is a webOS application."
))
.unwrap();
f.write_fmt(format_args!("webOS-Package-Format-Version: {}\n", 2))
.unwrap();
f.write_fmt(format_args!("webOS-Packager-Version: {}\n", "x.y.x"))
.unwrap();
return Ok(());
))?;
f.write_fmt(format_args!("webOS-Package-Format-Version: {}\n", 2))?;
f.write_fmt(format_args!("webOS-Packager-Version: {}\n", "x.y.x"))?;
Ok(())
}
}
12 changes: 6 additions & 6 deletions ares-package/src/packaging/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
let mut ar_header = ArHeader::new(b"data.tar.gz".to_vec(), data_tar_gz.len() as u64);
ar_header.set_mode(0o100644);
ar_header.set_mtime(mtime);
return self.append(&ar_header, Cursor::new(data_tar_gz));
self.append(&ar_header, Cursor::new(data_tar_gz))
}
}

Expand Down Expand Up @@ -101,19 +101,19 @@ where
println!("Adding {path}", path = dir);
tar.append_data(&mut header, &dir, empty.deref())?;
}
return Ok(());
Ok(())
}

fn tar_path<S, P>(prefix: S, path: P) -> PathBuf
where
S: AsRef<str>,
P: AsRef<Path>,
{
return PathBuf::from(format!(
PathBuf::from(format!(
"{}{}",
prefix.as_ref(),
path.as_ref().to_slash_lossy()
));
))
}

fn append_tree<W, S, P>(
Expand Down Expand Up @@ -170,7 +170,7 @@ where
tar.append_data(&mut header, tar_path, &mut File::open(entry_path)?)?;
}
}
return Ok(());
Ok(())
}

fn append_package_info<W>(
Expand All @@ -195,5 +195,5 @@ where
header.set_cksum();
tar.append_data(&mut header, &pkg_info_path, details.package_data.deref())?;
println!("Adding {path}", path = pkg_info_path);
return Ok(());
Ok(())
}
1 change: 1 addition & 0 deletions ares-push/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "Apache-2.0"
common-device = { path = "../common/device" }
common-connection = { path = "../common/connection" }
clap = { workspace = true, features = ["derive", "env"] }
libssh-rs = { workspace = true }
path-slash = "0.2.1"
walkdir = "2.5.0"

Expand Down
9 changes: 6 additions & 3 deletions ares-push/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use walkdir::WalkDir;

use ares_connection_lib::session::NewSession;
use ares_device_lib::DeviceManager;
use libssh_rs::OpenFlags;

#[derive(Parser, Debug)]
#[command(about)]
Expand Down Expand Up @@ -56,14 +57,16 @@ fn main() {
match entry {
Ok(entry) => {
let file_type = entry.file_type();
let dest_path = dest_base.join(entry.path().strip_prefix(source_prefix).unwrap());
let dest_path =
dest_base.join(entry.path().strip_prefix(source_prefix).unwrap());
if file_type.is_dir() {
println!(
"{} => {}",
entry.path().to_string_lossy(),
dest_path.to_slash_lossy()
);
sftp.create_dir(dest_path.to_slash_lossy().as_ref(), 0o755).unwrap_or(());
sftp.create_dir(dest_path.to_slash_lossy().as_ref(), 0o755)
.unwrap_or(());
} else if file_type.is_file() {
println!(
"{} => {}",
Expand All @@ -72,7 +75,7 @@ fn main() {
);
let mut file = match sftp.open(
dest_path.to_slash_lossy().as_ref(),
0o1101, /*O_WRONLY | O_CREAT | O_TRUNC*/
OpenFlags::READ_ONLY | OpenFlags::CREATE | OpenFlags::TRUNCATE,
0o644,
) {
Ok(file) => file,
Expand Down
4 changes: 2 additions & 2 deletions ares-shell/src/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ pub(crate) fn shell(ch: Channel) -> Result<i32, Error> {
}
}
Err(e) => {
return Err(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()));
return Err(Error::new(std::io::ErrorKind::Other, e.to_string()));
}
}
stderr = !stderr;
}
}
}
drop(events);
return Ok(ch.get_exit_status().unwrap_or(-1) as i32);
Ok(ch.get_exit_status().unwrap_or(-1) as i32)
}

struct EventThread {
Expand Down
Loading

0 comments on commit fddf9cb

Please sign in to comment.