diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-03-11 16:51:19 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-03-11 16:51:19 +0100 |
commit | 2d0ce2896868f8fc72b560be5ebaa2a4f729b9dd (patch) | |
tree | cbb31d3d33146e14512c6882bb7f674d2157bd8c | |
parent | 60870db4e80b24b24c28ddd021e0c24711b5992a (diff) |
replace FmError & FmResult by anyhow
-rw-r--r-- | development.md | 4 | ||||
-rw-r--r-- | src/action_map.rs | 4 | ||||
-rw-r--r-- | src/bulkrename.rs | 46 | ||||
-rw-r--r-- | src/completion.rs | 14 | ||||
-rw-r--r-- | src/compress.rs | 17 | ||||
-rw-r--r-- | src/config.rs | 10 | ||||
-rw-r--r-- | src/copy_move.rs | 9 | ||||
-rw-r--r-- | src/cryptsetup.rs | 57 | ||||
-rw-r--r-- | src/decompress.rs | 16 | ||||
-rw-r--r-- | src/event_dispatch.rs | 8 | ||||
-rw-r--r-- | src/event_exec.rs | 311 | ||||
-rw-r--r-- | src/fileinfo.rs | 84 | ||||
-rw-r--r-- | src/fm_error.rs | 190 | ||||
-rw-r--r-- | src/git.rs | 14 | ||||
-rw-r--r-- | src/help.rs | 4 | ||||
-rw-r--r-- | src/keybindings.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/log.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/marks.rs | 25 | ||||
-rw-r--r-- | src/mocp.rs | 10 | ||||
-rw-r--r-- | src/opener.rs | 41 | ||||
-rw-r--r-- | src/preview.rs | 67 | ||||
-rw-r--r-- | src/shell_menu.rs | 19 | ||||
-rw-r--r-- | src/status.rs | 54 | ||||
-rw-r--r-- | src/tab.rs | 45 | ||||
-rw-r--r-- | src/term_manager.rs | 86 | ||||
-rw-r--r-- | src/trash.rs | 83 | ||||
-rw-r--r-- | src/tree.rs | 22 | ||||
-rw-r--r-- | src/utils.rs | 18 |
30 files changed, 516 insertions, 758 deletions
diff --git a/development.md b/development.md index b3085c45..e378b94e 100644 --- a/development.md +++ b/development.md @@ -457,14 +457,14 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. - [ ] Version 0.1.20 - - [ ] display version in help + - [x] display version in help + - [x] replace FmResult & FmError by anyhow since I'm already using it... - [ ] exec multiple flagged files - [ ] shell menu - [ ] allow non tui like wttr, diff, bat, tail -n etc. - [ ] more options like "use flagged files" for diff - - [ ] replace FmResult & FmError by anyhow since I'm already using it... - [ ] build option to force reset of config file, warn the user at first start - [ ] update readme & animation - [ ] optionable "plugin" started from config file. Would require every option to be `Option<Plugin>` and may cause problems with the borrow checker. diff --git a/src/action_map.rs b/src/action_map.rs index 2593eafe..4197b34f 100644 --- a/src/action_map.rs +++ b/src/action_map.rs @@ -1,8 +1,8 @@ +use anyhow::Result; use strum_macros::{Display, EnumIter, EnumString}; use crate::config::Colors; use crate::event_exec::EventExec; -use crate::fm_error::FmResult; use crate::status::Status; /// Different kind of action which can be mapped to a key. @@ -96,7 +96,7 @@ pub enum ActionMap { impl ActionMap { /// Makes the junction between `Actions` and `Events`. /// Every Action links to a different `EventExec` method. - pub fn matcher(&self, status: &mut Status, colors: &Colors) -> FmResult<()> { + pub fn matcher(&self, status: &mut Status, colors: &Colors) -> Result<()> { let current_tab = status.selected(); match *self { ActionMap::Back => EventExec::event_back(status, colors), diff --git a/src/bulkrename.rs b/src/bulkrename.rs index 634a13cd..aedaaec9 100644 --- a/src/bulkrename.rs +++ b/src/bulkrename.rs @@ -1,3 +1,4 @@ +use anyhow::{anyhow, Result}; use log::info; use rand::Rng; use std::io::{BufRead, Write}; @@ -6,7 +7,6 @@ use std::thread; use std::time::{Duration, SystemTime}; use crate::constant_strings_paths::TMP_FOLDER_PATH; -use crate::fm_error::{FmError, FmResult}; use crate::impl_selectable_content; use crate::opener::Opener; use crate::status::Status; @@ -23,7 +23,7 @@ pub struct Bulkrename<'a> { impl<'a> Bulkrename<'a> { /// Creates a new Bulkrename instance. - pub fn renamer(original_filepath: Vec<&'a Path>) -> FmResult<Self> { + pub fn renamer(original_filepath: Vec<&'a Path>) -> Result<Self> { let temp_file = Self::generate_random_filepath()?; Ok(Self { original_filepath: Some(original_filepath), @@ -32,7 +32,7 @@ impl<'a> Bulkrename<'a> { }) } - pub fn creator(path_str: &'a str) -> FmResult<Self> { + pub fn creator(path_str: &'a str) -> Result<Self> { let temp_file = Self::generate_random_filepath()?; info!("created {temp_file:?}"); Ok(Self { @@ -46,7 +46,7 @@ impl<'a> Bulkrename<'a> { /// The tempory file is opened with our `Opener` crate, allowing us /// to use the default text file editor. /// Filenames are sanitized before processing. - fn rename(&mut self, opener: &Opener) -> FmResult<()> { + fn rename(&mut self, opener: &Opener) -> Result<()> { self.write_original_names()?; let original_modification = Self::get_modified_date(&self.temp_file)?; self.open_temp_file_with_editor(opener)?; @@ -57,7 +57,7 @@ impl<'a> Bulkrename<'a> { self.delete_temp_file() } - fn create_files(&mut self, opener: &Opener) -> FmResult<()> { + fn create_files(&mut self, opener: &Opener) -> Result<()> { self.create_random_file()?; let original_modification = Self::get_modified_date(&self.temp_file)?; self.open_temp_file_with_editor(opener)?; @@ -71,7 +71,7 @@ impl<'a> Bulkrename<'a> { fn watch_modification_in_thread( filepath: &Path, original_modification: SystemTime, - ) -> FmResult<()> { + ) -> Result<()> { let filepath = filepath.to_owned(); let handle = thread::spawn(move || loop { if Self::is_file_modified(&filepath, original_modification).unwrap_or(true) { @@ -79,10 +79,13 @@ impl<'a> Bulkrename<'a> { } thread::sleep(Duration::from_millis(100)); }); - Ok(handle.join()?) + match handle.join() { + Ok(handle) => Ok(handle), + Err(e) => Err(anyhow!("watching thread failed {e:?}")), + } } - fn get_modified_date(filepath: &Path) -> FmResult<SystemTime> { + fn get_modified_date(filepath: &Path) -> Result<SystemTime> { Ok(std::fs::metadata(filepath)?.modified()?) } @@ -97,18 +100,18 @@ impl<'a> Bulkrename<'a> { rand_str } - fn generate_random_filepath() -> FmResult<PathBuf> { + fn generate_random_filepath() -> Result<PathBuf> { let mut filepath = PathBuf::from(&TMP_FOLDER_PATH); filepath.push(Self::random_name()); Ok(filepath) } - fn create_random_file(&self) -> FmResult<()> { + fn create_random_file(&self) -> Result<()> { std::fs::File::create(&self.temp_file)?; Ok(()) } - fn write_original_names(&self) -> FmResult<()> { + fn write_original_names(&self) -> Result<()> { let mut file = std::fs::File::create(&self.temp_file)?; for path in self.original_filepath.clone().unwrap().iter() { @@ -121,20 +124,17 @@ impl<'a> Bulkrename<'a> { Ok(()) } - fn open_temp_file_with_editor(&self, opener: &Opener) -> FmResult<()> { + fn open_temp_file_with_editor(&self, opener: &Opener) -> Result<()> { info!("opening tempory file {:?}", self.temp_file); opener.open(&self.temp_file) } - fn is_file_modified( - path: &Path, - original_modification: std::time::SystemTime, - ) -> FmResult<bool> { + fn is_file_modified(path: &Path, original_modification: std::time::SystemTime) -> Result<bool> { let last_modification = Self::get_modified_date(path)?; Ok(last_modification > original_modification) } - fn get_new_filenames(&self) -> FmResult<Vec<String>> { + fn get_new_filenames(&self) -> Result<Vec<String>> { let file = std::fs::File::open(&self.temp_file)?; let reader = std::io::BufReader::new(file); @@ -146,18 +146,18 @@ impl<'a> Bulkrename<'a> { .collect(); if let Some(original_filepath) = self.original_filepath.clone() { if new_names.len() < original_filepath.len() { - return Err(FmError::custom("new filenames", "not enough filenames")); + return Err(anyhow!("new filenames: not enough filenames")); } } Ok(new_names) } - fn delete_temp_file(&self) -> FmResult<()> { + fn delete_temp_file(&self) -> Result<()> { std::fs::remove_file(&self.temp_file)?; Ok(()) } - fn rename_all(&self, new_filenames: Vec<String>) -> FmResult<()> { + fn rename_all(&self, new_filenames: Vec<String>) -> Result<()> { let mut counter = 0; for (path, filename) in self .original_filepath @@ -175,7 +175,7 @@ impl<'a> Bulkrename<'a> { Ok(()) } - fn create_all_files(&self, new_filenames: &[String]) -> FmResult<()> { + fn create_all_files(&self, new_filenames: &[String]) -> Result<()> { let mut counter = 0; for filename in new_filenames.iter() { let mut new_path = std::path::PathBuf::from(self.parent_dir.unwrap()); @@ -202,7 +202,7 @@ impl<'a> Bulkrename<'a> { Ok(()) } - fn rename_file(&self, path: &Path, filename: &str) -> FmResult<()> { + fn rename_file(&self, path: &Path, filename: &str) -> Result<()> { let mut parent = PathBuf::from(path); parent.pop(); std::fs::rename(path, parent.join(filename))?; @@ -232,7 +232,7 @@ impl Bulk { /// First method is a rename of selected files, /// Second is the creation of files, /// Third is the creation of folders. - pub fn execute_bulk(&self, status: &Status) -> FmResult<()> { + pub fn execute_bulk(&self, status: &Status) -> Result<()> { match self.index { 0 => Bulkrename::renamer(status.filtered_flagged_files())?.rename(&status.opener), 1 => Bulkrename::creator(status.selected_path_str())?.create_files(&status.opener), diff --git a/src/completion.rs b/src/completion.rs index a97775e8..15975031 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -1,9 +1,9 @@ use std::fs::{self, ReadDir}; +use anyhow::Result; use strum::IntoEnumIterator; use crate::fileinfo::PathContent; -use crate::fm_error::FmResult; use crate::mode::Mode; use crate::tree::ColoredString; @@ -100,7 +100,7 @@ impl Completion { /// Goto completion. /// Looks for the valid path completing what the user typed. - pub fn goto(&mut self, input_string: &str, current_path: &str) -> FmResult<()> { + pub fn goto(&mut self, input_string: &str, current_path: &str) -> Result<()> { self.update_from_input(input_string, current_path); let (parent, last_name) = split_input_string(input_string); if last_name.is_empty() { @@ -151,7 +151,7 @@ impl Completion { } /// Looks for programs in $PATH completing the one typed by the user. - pub fn exec(&mut self, input_string: &str) -> FmResult<()> { + pub fn exec(&mut self, input_string: &str) -> Result<()> { let mut proposals: Vec<String> = vec![]; if let Some(paths) = std::env::var_os("PATH") { for path in std::env::split_paths(&paths).filter(|path| path.exists()) { @@ -162,7 +162,7 @@ impl Completion { Ok(()) } - pub fn command(&mut self, input_string: &str) -> FmResult<()> { + pub fn command(&mut self, input_string: &str) -> Result<()> { let proposals = crate::action_map::ActionMap::iter() .filter(|command| { command @@ -179,7 +179,7 @@ impl Completion { fn find_completion_in_path( path: std::path::PathBuf, input_string: &str, - ) -> FmResult<Vec<String>> { + ) -> Result<Vec<String>> { Ok(fs::read_dir(path)? .filter_map(|e| e.ok()) .filter(|e| file_match_input(e, input_string)) @@ -192,7 +192,7 @@ impl Completion { &mut self, input_string: &str, path_content: &PathContent, - ) -> FmResult<()> { + ) -> Result<()> { self.update( path_content .content @@ -209,7 +209,7 @@ impl Completion { &mut self, input_string: &str, content: &[(String, ColoredString)], - ) -> FmResult<()> { + ) -> Result<()> { self.update( content .iter() diff --git a/src/compress.rs b/src/compress.rs index ac3ca50c..c41cd510 100644 --- a/src/compress.rs +++ b/src/compress.rs @@ -2,7 +2,8 @@ use std::fmt::Display; use std::io::prelude::*; use std::io::Write; -use crate::fm_error::FmResult; +use anyhow::Result; + use crate::impl_selectable_content; use flate2::write::{DeflateEncoder, GzEncoder, ZlibEncoder}; use flate2::Compression; @@ -55,7 +56,7 @@ impl Default for Compresser { impl Compresser { /// Archive the files with tar and compress them with the selected method. /// The compression method is chosen by the user. - pub fn compress(&self, files: Vec<std::path::PathBuf>) -> FmResult<()> { + pub fn compress(&self, files: Vec<std::path::PathBuf>) -> Result<()> { let Some(selected) = self.selected() else { return Ok(()) }; match selected { CompressionMethod::DEFLATE => Self::compress_deflate("archive.tar.gz", files), @@ -66,7 +67,7 @@ impl Compresser { } } - fn make_tar<W>(files: Vec<std::path::PathBuf>, mut archive: tar::Builder<W>) -> FmResult<()> + fn make_tar<W>(files: Vec<std::path::PathBuf>, mut archive: tar::Builder<W>) -> Result<()> where W: Write, { @@ -80,7 +81,7 @@ impl Compresser { Ok(()) } - fn compress_gzip(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { + fn compress_gzip(archive_name: &str, files: Vec<std::path::PathBuf>) -> Result<()> { let compressed_file = std::fs::File::create(archive_name)?; let mut encoder = GzEncoder::new(compressed_file, Compression::default()); @@ -93,7 +94,7 @@ impl Compresser { Ok(()) } - fn compress_deflate(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { + fn compress_deflate(archive_name: &str, files: Vec<std::path::PathBuf>) -> Result<()> { let compressed_file = std::fs::File::create(archive_name)?; let mut encoder = DeflateEncoder::new(compressed_file, Compression::default()); @@ -106,7 +107,7 @@ impl Compresser { Ok(()) } - fn compress_zlib(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { + fn compress_zlib(archive_name: &str, files: Vec<std::path::PathBuf>) -> Result<()> { let compressed_file = std::fs::File::create(archive_name)?; let mut encoder = ZlibEncoder::new(compressed_file, Compression::default()); @@ -119,7 +120,7 @@ impl Compresser { Ok(()) } - fn compress_lzma(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { + fn compress_lzma(archive_name: &str, files: Vec<std::path::PathBuf>) -> Result<()> { let compressed_file = std::fs::File::create(archive_name)?; let mut encoder = LzmaWriter::new_compressor(compressed_file, 6)?; @@ -132,7 +133,7 @@ impl Compresser { Ok(()) } - fn compress_zip(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { + fn compress_zip(archive_name: &str, files: Vec<std::path::PathBuf>) -> Result<()> { let archive = std::fs::File::create(archive_name).unwrap(); let mut zip = zip::ZipWriter::new(archive); for file in files.iter() { diff --git a/src/config.rs b/src/config.rs index bf5bb444..6d740b48 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,11 @@ use std::{fs::File, path}; +use anyhow::Result; use serde_yaml; use tuikit::attr::Color; use crate::color_cache::ColorCache; use crate::constant_strings_paths::DEFAULT_TERMINAL_APPLICATION; -use crate::fm_error::FmResult; use crate::keybindings::Bindings; use crate::utils::is_program_in_path; @@ -25,7 +25,7 @@ pub struct Config { impl Config { /// Returns a default config with hardcoded values. - fn new() -> FmResult<Self> { + fn new() -> Result<Self> { Ok(Self { colors: Colors::default(), terminal: DEFAULT_TERMINAL_APPLICATION.to_owned(), @@ -33,7 +33,7 @@ impl Config { }) } /// Updates the config from a configuration content. - fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) -> FmResult<()> { + fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) -> Result<()> { self.colors.update_from_config(&yaml["colors"]); self.binds.update_from_config(&yaml["keys"])?; self.terminal = Self::set_terminal(&yaml["terminal"])?; @@ -43,7 +43,7 @@ impl Config { /// First we try to use the current terminal. If it's a fake one (ie. inside neovim float term), /// we look for the configured one, /// else we use the hardcoded one. - fn set_terminal(yaml: &serde_yaml::value::Value) -> FmResult<String> { + fn set_terminal(yaml: &serde_yaml::value::Value) -> Result<String> { let terminal_currently_used = std::env::var("TERM").unwrap_or_default(); if !terminal_currently_used.is_empty() && is_program_in_path(&terminal_currently_used) { Ok(terminal_currently_used) @@ -149,7 +149,7 @@ pub fn str_to_tuikit(color: &str) -> Color { /// 1. hardcoded values /// /// 2. configured values from `~/.config/fm/config_file_name.yaml` if those files exists. -pub fn load_config(path: &str) -> FmResult<Config> { +pub fn load_config(path: &str) -> Result<Config> { let mut config = Config::new()?; if let Ok(file) = File::open(path::Path::new(&shellexpand::tilde(path).to_string())) { diff --git a/src/copy_move.rs b/src/copy_move.rs index 42442363..2179acbb 100644 --- a/src/copy_move.rs +++ b/src/copy_move.rs @@ -3,21 +3,20 @@ use std::path::PathBuf; use std::sync::Arc; use std::thread; +use anyhow::Result; use fs_extra; use indicatif::{InMemoryTerm, ProgressBar, ProgressDrawTarget, ProgressState, ProgressStyle}; use log::info; -// use notify_rust::Notification; use tuikit::prelude::{Attr, Color, Effect, Event, Term}; use crate::fileinfo::human_size; -use crate::fm_error::FmResult; use crate::opener::execute_in_child; fn setup( action: String, height: usize, width: usize, -) -> FmResult<(InMemoryTerm, ProgressBar, fs_extra::dir::CopyOptions)> { +) -> Result<(InMemoryTerm, ProgressBar, fs_extra::dir::CopyOptions)> { let in_mem = InMemoryTerm::new(height as u16, width as u16); let pb = ProgressBar::with_draw_target( Some(100), @@ -93,7 +92,7 @@ pub fn copy_move( sources: Vec<PathBuf>, dest: &str, term: Arc<Term>, -) -> FmResult<()> { +) -> Result<()> { let c_term = term.clone(); let (height, width) = term.term_size()?; let (in_mem, pb, options) = setup(copy_or_move.verb().to_owned(), height, width)?; @@ -137,7 +136,7 @@ pub fn copy_move( /// Send a notification to the desktop. /// Requires "notify-send" to be installed. -fn notify(text: &str) -> FmResult<()> { +fn notify(text: &str) -> Result<()> { execute_in_child("notify-send", &vec![text])?; Ok(()) } diff --git a/src/cryptsetup.rs b/src/cryptsetup.rs index bfe2fa6f..c9dd7793 100644 --- a/src/cryptsetup.rs +++ b/src/cryptsetup.rs @@ -1,10 +1,10 @@ use std::io::Write; use std::process::{Command, Stdio}; +use anyhow::{anyhow, Context, Result}; use log::info; use sysinfo::{DiskExt, System, SystemExt}; -use crate::fm_error::{FmError, FmResult}; use crate::impl_selectable_content; use crate::utils::current_username; @@ -49,17 +49,17 @@ impl PasswordHolder { } /// Reads the cryptsetup password - fn cryptsetup(&self) -> FmResult<String> { + fn cryptsetup(&self) -> Result<String> { self.cryptsetup .clone() - .ok_or_else(|| FmError::custom("PasswordHolder", "cryptsetup password isn't set")) + .context("PasswordHolder: cryptsetup password isn't set") } /// Reads the sudo password - fn sudo(&self) -> FmResult<String> { + fn sudo(&self) -> Result<String> { self.sudo .clone() - .ok_or_else(|| FmError::custom("PasswordHolder", "sudo password isn't set")) + .context("PasswordHolder: sudo password isn't set") } fn has_sudo(&self) -> bool { @@ -82,7 +82,7 @@ impl PasswordHolder { /// lsblk -l -o FSTYPE,PATH,UUID,FSVER,MOUNTPOINT,PARTLABEL /// ``` /// as a String. -fn get_devices() -> FmResult<String> { +fn get_devices() -> Result<String> { let output = Command::new("lsblk") .args(&vec!["-l", "-o", "FSTYPE,PATH,UUID,FSVER,MOUNTPOINT"]) .stdin(Stdio::null()) @@ -103,7 +103,7 @@ fn filter_crypto_devices_lines(output: String, key: &str) -> Vec<String> { /// run a sudo command requiring a password (generally to establish the password.) /// Since I can't send 2 passwords at a time, it will only work with the sudo password -fn sudo_password(args: &[String], password: &str) -> FmResult<(bool, String, String)> { +fn sudo_password(args: &[String], password: &str) -> Result<(bool, String, String)> { info!("sudo {:?}", args); let mut child = Command::new("sudo") .args(args) @@ -115,7 +115,7 @@ fn sudo_password(args: &[String], password: &str) -> FmResult<(bool, String, Str let child_stdin = child .stdin .as_mut() - .ok_or_else(|| FmError::custom("run_privileged_command", "couldn't open child stdin"))?; + .context("run_privileged_command: couldn't open child stdin")?; child_stdin.write_all(format!("{password}\n").as_bytes())?; let output = child.wait_with_output()?; @@ -128,7 +128,7 @@ fn sudo_password(args: &[String], password: &str) -> FmResult<(bool, String, Str /// Run a passwordless sudo command. /// Returns stdout & stderr -fn sudo(args: &[String]) -> FmResult<(bool, String, String)> { +fn sudo(args: &[String]) -> Result<(bool, String, String)> { info!("sudo {:?}", args); let child = Command::new("sudo") .args(args) @@ -158,13 +158,13 @@ pub struct CryptoDevice { impl CryptoDevice { /// Parse the output of a lsblk formated line into a struct - fn from_line(line: &str) -> FmResult<Self> { + fn from_line(line: &str) -> Result<Self> { let mut crypo_device = Self::default(); crypo_device.update_from_line(line)?; Ok(crypo_device) } - fn update_from_line(&mut self, line: &str) -> FmResult<()> { + fn update_from_line(&mut self, line: &str) -> Result<()> { let strings = line.split_whitespace(); let mut params: Vec<Option<String>> = vec![None; 5]; for (count, param) in strings.enumerate() { @@ -172,16 +172,16 @@ impl CryptoDevice { } self.fs_type = params .remove(0) - .ok_or_else(|| FmError::custom("CryptoDevice", "parameter shouldn't be None"))?; + .context("CryptoDevice: parameter shouldn't be None")?; self.path = params .remove(0) - .ok_or_else(|| FmError::custom("CryptoDevice", "parameter shouldn't be None"))?; + .context("CryptoDevice: parameter shouldn't be None")?; self.uuid = params .remove(0) - .ok_or_else(|| FmError::custom("CryptoDevice", "parameter shouldn't be None"))?; + .context("CryptoDevice: parameter shouldn't be None")?; self.fs_ver = params .remove(0) - .ok_or_else(|| FmError::custom("CryptoDevice", "parameter shouldn't be None"))?; + .context("CryptoDevice: parameter shouldn't be None")?; self.mountpoints = params.remove(0); Ok(()) } @@ -265,7 +265,7 @@ impl CryptoDevice { self.mount_point().is_some() } - fn set_device_name(&mut self) -> FmResult<()> { + fn set_device_name(&mut self) -> Result<()> { let child = Command::new("lsblk") .arg("-l") .arg("-n") @@ -285,7 +285,7 @@ impl CryptoDevice { self.device_name = Some( s.split_whitespace() .next() - .ok_or_else(|| FmError::custom("mapped point", "shouldn't be empty"))? + .context("mapped point: shouldn't be empty")? .to_owned(), ); } else { @@ -295,7 +295,7 @@ impl CryptoDevice { } /// String representation of the device. - pub fn as_string(&self) -> FmResult<String> { + pub fn as_string(&self) -> Result<String> { Ok(if let Some(mount_point) = self.mount_point() { format!("{} -> {}", self.path, mount_point) } else { @@ -303,13 +303,10 @@ impl CryptoDevice { }) } - fn open_mount(&mut self, username: &str, passwords: &mut PasswordHolder) -> FmResult<bool> { + fn open_mount(&mut self, username: &str, passwords: &mut PasswordHolder) -> Result<bool> { self.set_device_name()?; if self.is_mounted() { - Err(FmError::custom( - "luks open mount", - "device is already mounted", - )) + Err(anyhow!("luks open mount: device is already mounted")) } else { // sudo let (success, _, _) = sudo_password( @@ -345,7 +342,7 @@ impl CryptoDevice { } } - fn umount_close(&mut self, username: &str, passwords: &mut PasswordHolder) -> FmResult<bool> { + fn umount_close(&mut self, username: &str, passwords: &mut PasswordHolder) -> Result<bool> { self.set_device_name()?; // sudo let (success, _, _) = sudo_password( @@ -380,7 +377,7 @@ pub struct Device { impl Device { /// Reads a device from a line of text from cryptsetup. - pub fn from_line(line: &str) -> FmResult<Self> { + pub fn from_line(line: &str) -> Result<Self> { Ok(Self { cryptdevice: CryptoDevice::from_line(line)?, password_holder: PasswordHolder::default(), @@ -399,7 +396,7 @@ pub struct DeviceOpener { impl DeviceOpener { /// Updates itself from the output of cryptsetup. - pub fn update(&mut self) -> FmResult<()> { + pub fn update(&mut self) -> Result<()> { self.content = filter_crypto_devices_lines(get_dev |