diff options
author | Omer Ben-Amram <omerbenamram@gmail.com> | 2020-01-06 22:11:48 +0300 |
---|---|---|
committer | Omer Ben-Amram <omerbenamram@gmail.com> | 2020-01-06 22:11:48 +0300 |
commit | aea96c09d0581f7b289d1f1da06cd0191e87ace3 (patch) | |
tree | c9ccca054b14bc0b3be63768d09f172e4b6b4ac6 | |
parent | 84daee189019564510255d4fd861712d5e127ad3 (diff) |
migrated to anyhow, and fixed more lints
-rw-r--r-- | src/attribute/raw.rs | 2 | ||||
-rw-r--r-- | src/attribute/x30.rs | 2 | ||||
-rw-r--r-- | src/attribute/x80.rs | 2 | ||||
-rw-r--r-- | src/bin/mft_dump.rs | 37 | ||||
-rw-r--r-- | src/entry.rs | 2 | ||||
-rw-r--r-- | src/err.rs | 10 | ||||
-rw-r--r-- | src/mft.rs | 2 | ||||
-rw-r--r-- | tests/fixtures.rs | 4 | ||||
-rw-r--r-- | tests/test_cli_interactive.rs | 10 |
9 files changed, 26 insertions, 45 deletions
diff --git a/src/attribute/raw.rs b/src/attribute/raw.rs index c00fb1d..d416340 100644 --- a/src/attribute/raw.rs +++ b/src/attribute/raw.rs @@ -1,5 +1,5 @@ use crate::attribute::MftAttributeType; -use crate::err::{self, Result}; +use crate::err::{Result}; use crate::{utils, ReadSeek}; use serde::{ser, Serialize}; diff --git a/src/attribute/x30.rs b/src/attribute/x30.rs index 1228039..22aa8ec 100644 --- a/src/attribute/x30.rs +++ b/src/attribute/x30.rs @@ -1,5 +1,5 @@ use crate::attribute::FileAttributeFlags; -use crate::err::{self, Error, Result}; +use crate::err::{Error, Result}; use crate::ReadSeek; use log::trace; diff --git a/src/attribute/x80.rs b/src/attribute/x80.rs index b9d8ec0..d295e16 100644 --- a/src/attribute/x80.rs +++ b/src/attribute/x80.rs @@ -1,4 +1,4 @@ -use crate::err::{self, Result}; +use crate::err::{Result}; use crate::{utils, ReadSeek}; use serde::ser; diff --git a/src/bin/mft_dump.rs b/src/bin/mft_dump.rs index 793581a..6655524 100644 --- a/src/bin/mft_dump.rs +++ b/src/bin/mft_dump.rs @@ -13,7 +13,6 @@ use anyhow::{anyhow, Context, Error, Result}; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; -use std::process::exit; use mft::entry::ZERO_HEADER; use std::fmt::Write as FmtWrite; @@ -136,7 +135,6 @@ struct MftDump { verbosity_level: Option<Level>, output_format: OutputFormat, ranges: Option<Ranges>, - backtraces: bool, } impl MftDump { @@ -145,7 +143,9 @@ impl MftDump { OutputFormat::from_str(matches.value_of("output-format").unwrap_or_default()) .expect("Validated with clap default values"); - let backtraces = matches.is_present("backtraces"); + if matches.is_present("backtraces") { + std::env::set_var("RUST_LIB_BACKTRACE", "1"); + } let output: Option<Box<dyn Write>> = if let Some(path) = matches.value_of("output-target") { match Self::create_output_file(path, !matches.is_present("no-confirm-overwrite")) { @@ -193,7 +193,6 @@ impl MftDump { verbosity_level, output_format, ranges, - backtraces, }) } @@ -268,16 +267,7 @@ impl MftDump { pub fn run(&mut self) -> Result<()> { self.try_to_initialize_logging(); - let mut parser = match MftParser::from_path(&self.filepath) { - Ok(parser) => parser, - Err(e) => { - return Err(anyhow!( - "Failed to open file {}.\n\tcaused by: {}", - self.filepath.display(), - &e - )) - } - }; + let mut parser = MftParser::from_path(&self.filepath)?; // Since the JSON parser can do away with a &mut Write, but the csv parser needs ownership // of `Write`, we eagerly create the csv writer here, moving the Box<Write> out from @@ -456,7 +446,7 @@ pub fn sanitized(component: &str) -> String { buf } -fn main() { +fn main() -> Result<()> { let matches = App::new("MFT Parser") .version(env!("CARGO_PKG_VERSION")) .author("Omer B. <omerbenamram@gmail.com>") @@ -520,19 +510,8 @@ fn main() { .help("If set, a backtrace will be printed with some errors if available")) .get_matches(); - let mut app = match MftDump::from_cli_matches(&matches) { - Ok(app) => app, - Err(e) => { - eprintln!("An error occurred while setting up the app: {}", &e); - exit(1); - } - }; + let mut app = MftDump::from_cli_matches(&matches).context("Failed setting up the app")?; + app.run().context("A runtime error has occurred")?; - match app.run() { - Ok(()) => {} - Err(e) => { - eprintln!("A runtime error has occurred: {}", &e); - exit(1); - } - }; + Ok(()) } diff --git a/src/entry.rs b/src/entry.rs index 2f89d04..1ce8f35 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,4 @@ -use crate::err::{self, Error, Result}; +use crate::err::{Error, Result}; use crate::impl_serialize_for_bitflags; use log::trace; @@ -5,12 +5,12 @@ pub type Result<T> = ::std::result::Result<T, Error>; #[derive(Debug, Error)] pub enum Error { - #[error("An I/O error has occurred: {}", source)] + #[error("An I/O error has occurred")] IoError { #[from] source: std::io::Error, }, - #[error("Failed to open file {}: {}", path.display(), source)] + #[error("Failed to open file {}", path.display())] FailedToOpenFile { path: PathBuf, source: std::io::Error, @@ -39,11 +39,11 @@ pub enum Error { end_of_sector_bytes: Vec<u8>, fixup_bytes: Vec<u8>, }, - #[error("Failed to read MftReference: `{}`", source)] + #[error("Failed to read MftReference")] FailedToReadMftReference { source: winstructs::err::Error }, - #[error("Failed to read WindowsTime: `{}`", source)] + #[error("Failed to read WindowsTime")] FailedToReadWindowsTime { source: winstructs::err::Error }, - #[error("Failed to read GUID: `{}`", source)] + #[error("Failed to read GUID")] FailedToReadGuid { source: winstructs::err::Error }, #[error("An unexpected error has occurred: {}", detail)] Any { detail: String }, @@ -180,6 +180,8 @@ mod tests { count += 1; } } + + assert!(count > 0) } #[test] diff --git a/tests/fixtures.rs b/tests/fixtures.rs index 17dfed6..580c4e8 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -1,9 +1,9 @@ #![allow(dead_code)] use std::path::PathBuf; -use std::sync::{Once, ONCE_INIT}; +use std::sync::{Once}; -static LOGGER_INIT: Once = ONCE_INIT; +static LOGGER_INIT: Once = Once::new(); // Rust runs the tests concurrently, so unless we synchronize logging access // it will crash when attempting to run `cargo test` with some logging facilities. diff --git a/tests/test_cli_interactive.rs b/tests/test_cli_interactive.rs index 6667ac0..987f54a 100644 --- a/tests/test_cli_interactive.rs +++ b/tests/test_cli_interactive.rs @@ -4,13 +4,13 @@ mod fixtures; -use fixtures::*; -use std::fs::File; -use std::io::{Read, Write}; -use tempfile::tempdir; -use assert_cmd::cargo::cargo_bin; + + + + + #[cfg(not(target_os = "windows"))] use rexpect::spawn; |