Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
losynix committed Jan 3, 2024
1 parent 7797bd4 commit 06af34e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
19 changes: 11 additions & 8 deletions usbsas-tools/src/fswriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ enum Error {
#[error("io error: {0}")]
IO(#[from] std::io::Error),
#[error("{0}")]
Error(String),
Arg(String),
#[error("{0}")]
Write(String),
#[error("sandbox: {0}")]
Sandbox(#[from] usbsas_sandbox::Error),
#[error("process: {0}")]
Process(#[from] usbsas_process::Error),
#[error("Bad Request")]
BadRequest,
#[error("progress error: {0}")]
Progress(#[from] indicatif::style::TemplateError),
}
type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -75,7 +79,7 @@ impl FsWriter {
let fs_size = self.fs.seek(SeekFrom::End(0))?;
self.fs.rewind()?;
if fs_size % SECTOR_SIZE != 0 {
return Err(Error::Error(format!(
return Err(Error::Write(format!(
"fs size ({fs_size}) % sector size ({SECTOR_SIZE}) != 0"
)));
}
Expand All @@ -85,7 +89,7 @@ impl FsWriter {
.size(proto::fs2dev::RequestDevSize {})?
.size;
if fs_size > dev_size {
return Err(Error::Error(format!(
return Err(Error::Write(format!(
"filesystem size ({fs_size}) > device size ({dev_size}), aborting"
)));
}
Expand Down Expand Up @@ -113,8 +117,7 @@ impl FsWriter {
let pb = indicatif::ProgressBar::new(fs_size);
pb.set_style(
indicatif::ProgressStyle::default_bar()
.template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")
.map_err(|err| Error::Error(format!("progress bar err: {err}")))?
.template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")?
.progress_chars("#>-"),
);

Expand All @@ -128,8 +131,8 @@ impl FsWriter {
pb.set_position(fs_size);
break;
}
Msg::Error(msg) => return Err(Error::Error(msg.err)),
_ => return Err(Error::Error("an error occured".to_string())),
Msg::Error(msg) => return Err(Error::Write(msg.err)),
_ => return Err(Error::Write("bad resp from fs2dev".to_string())),
}
}

Expand Down Expand Up @@ -196,7 +199,7 @@ fn main() -> Result<()> {
matches.get_one::<u32>("devnum"),
) {
(Some(fs), Some(bn), Some(dn)) => (fs, bn, dn),
_ => return Err(Error::Error("missing arg".to_string())),
_ => return Err(Error::Arg("missing arg".to_string())),
};

let mut fswriter = FsWriter::new(fs_path.to_owned(), busnum.to_owned(), devnum.to_owned())?;
Expand Down
14 changes: 9 additions & 5 deletions usbsas-tools/src/imager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ enum Error {
#[error("int error: {0}")]
Tryfromint(#[from] std::num::TryFromIntError),
#[error("{0}")]
Error(String),
Arg(String),
#[error("{0}")]
OpenDevice(String),
#[error("persist error: {0}")]
Persist(#[from] tempfile::PersistError),
#[error("sandbox: {0}")]
Sandbox(#[from] usbsas_sandbox::Error),
#[error("process: {0}")]
Process(#[from] usbsas_process::Error),
#[error("progress error: {0}")]
Progress(#[from] indicatif::style::TemplateError),
}
type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -101,7 +105,7 @@ impl Imager {
if let Some(proto::scsi::response::Msg::OpenDevice(rep)) = rep.msg {
(rep.dev_size, rep.block_size)
} else {
return Err(Error::Error("Couldn't open device".to_string()));
return Err(Error::OpenDevice("Couldn't open device".to_string()));
};

let mut todo = dev_size;
Expand All @@ -112,8 +116,8 @@ impl Imager {
let pb = indicatif::ProgressBar::new(dev_size);
pb.set_style(
indicatif::ProgressStyle::default_bar()
.template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")
.map_err(|err| Error::Error(format!("progress bar err: {err}")))?
.template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")?
// .map_err(|err| Error::Progress(format!("progress bar err: {err}")))?
.progress_chars("#>-"),
);

Expand Down Expand Up @@ -250,7 +254,7 @@ fn main() -> Result<()> {
) {
(Some(busnum), Some(devnum)) => (busnum.to_owned(), devnum.to_owned()),
_ => {
return Err(Error::Error(
return Err(Error::Arg(
"Must specify both busnum and devnum".to_string(),
));
}
Expand Down
10 changes: 5 additions & 5 deletions usbsas-tools/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ enum Error {
Upload(String),
#[error("analyze error: {0}")]
Analyze(String),
#[error("Error: {0}")]
Error(String),
#[error("download error: {0}")]
Download(String),
#[error("configuration or argument error: {0}")]
ArgConf(String),
}
type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -66,7 +66,7 @@ fn upload(config_path: &str, bundle_path: &str, id: &str) -> Result<()> {

let networks = &config
.networks
.ok_or_else(|| Error::Error("No networks in conf".into()))?;
.ok_or_else(|| Error::ArgConf("No networks".into()))?;

let network = if networks.len() == 1 {
&networks[0]
Expand Down Expand Up @@ -288,12 +288,12 @@ fn main() -> Result<()> {
"analyze" => analyze(config_path, path, id)?,
"download" => download(config_path, path, id)?,
_ => {
return Err(Error::Error(
return Err(Error::ArgConf(
"Bad action specified, either: upload, analyze or download".to_owned(),
))
}
},
_ => return Err(Error::Error("args parse failed".to_owned())),
_ => return Err(Error::ArgConf("args parse failed".to_owned())),
}

Ok(())
Expand Down
45 changes: 24 additions & 21 deletions usbsas-usbsas/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ use usbsas_utils::{self, clap::UsbsasClap, READ_FILE_MAX_SIZE, TAR_DATA_DIR};
enum Error {
#[error("io error: {0}")]
IO(#[from] std::io::Error),
#[error("{0}")]
Error(String),
#[error("analyze error: {0}")]
Analyze(String),
#[error("download error: {0}")]
Expand All @@ -44,8 +42,16 @@ enum Error {
Process(#[from] usbsas_process::Error),
#[error("Not enough space on destination device")]
NotEnoughSpace,
#[error("Error filtering files (bad count)")]
Filter,
#[error("File too large")]
FileTooLarge,
#[error("{0}")]
Wipe(String),
#[error("{0}")]
WriteFs(String),
#[error("serde_json: {0}")]
JsonError(#[from] serde_json::Error),
Json(#[from] serde_json::Error),
#[error("Bad Request")]
BadRequest,
#[error("State error")]
Expand Down Expand Up @@ -820,7 +826,7 @@ impl CopyFilesState {
path: files.to_vec(),
})?;
if rep.results.len() != files_count {
return Err(Error::Error("filter error".to_string()));
return Err(Error::Filter);
}
for (i, f) in files.iter().enumerate().take(files_count) {
if rep.results[i] == proto::filter::FilterResult::PathOk as i32 {
Expand Down Expand Up @@ -876,7 +882,7 @@ impl CopyFilesState {
"File '{}' is larger ({}B) than max size ({}B)",
&path, attrs.size, max_size
);
return Err(Error::Error("file too large".into()));
return Err(Error::FileTooLarge);
}
}

Expand Down Expand Up @@ -1211,7 +1217,7 @@ impl AnalyzeState {
Msg::Analyze(res) => {
let report_json: serde_json::Value = serde_json::from_str(&res.report)?;
log::trace!("analyzer report: {:?}", report_json);
let files_status = report_json["files"].as_object().ok_or(Error::Error(
let files_status = report_json["files"].as_object().ok_or(Error::Analyze(
"Couldn't get files from analyzer report".into(),
))?;

Expand Down Expand Up @@ -1526,19 +1532,14 @@ impl WriteFsState {
} else {
READ_FILE_MAX_SIZE
};
let rep = match children
let rep = children
.tar2files
.comm
.readfile(proto::files::RequestReadFile {
path: path.to_string(),
offset,
size: size_todo,
}) {
Ok(rep) => rep,
Err(err) => {
return Err(Error::Error(format!("{err}")));
}
};
})?;
children
.files2fs
.comm
Expand Down Expand Up @@ -1615,8 +1616,8 @@ impl WriteFsState {
comm.finalcopystatusdone(proto::usbsas::ResponseFinalCopyStatusDone {})?;
break;
}
Msg::Error(msg) => return Err(Error::Error(msg.err)),
_ => return Err(Error::Error("error writing fs".into())),
Msg::Error(msg) => return Err(Error::WriteFs(msg.err)),
_ => return Err(Error::WriteFs("error writing fs".into())),
}
}
Ok(())
Expand Down Expand Up @@ -1739,8 +1740,12 @@ impl WipeState {
})?
}
Msg::CopyStatusDone(_) => break,
Msg::Error(err) => {
log::error!("{}", err.err);
return Err(Error::Wipe(err.err));
}
_ => {
return Err(Error::Error("fs2dev err while wiping".into()));
return Err(Error::Wipe("fs2dev err while wiping".into()));
}
}
}
Expand Down Expand Up @@ -2013,7 +2018,7 @@ impl Children {
Ok(())
}

// If destination is USB, check that device will have enough space to stores
// If destination is USB, check that device will have enough space to store
// src files.
// Returns max size of a single file (4GB if dest is FAT, None otherwise)
fn check_dst_size(
Expand Down Expand Up @@ -2043,10 +2048,8 @@ impl Children {
error!("Aborting, dest dev too small");
return Err(Error::NotEnoughSpace);
}
match OutFsType::try_from(usb.fstype)
.map_err(|err| Error::Error(format!("{err}")))?
{
OutFsType::Fat => Ok(Some(0xFFFF_FFFF)),
match OutFsType::try_from(usb.fstype) {
Ok(OutFsType::Fat) => Ok(Some(0xFFFF_FFFF)),
_ => Ok(None),
}
}
Expand Down

0 comments on commit 06af34e

Please sign in to comment.