From 4d79239957228e7b0bdacdc75c8a7f6993157040 Mon Sep 17 00:00:00 2001 From: brxken128 <77554505+brxken128@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:50:36 +0000 Subject: [PATCH] major cleanup + clippy --- core/src/job/mod.rs | 2 +- core/src/object/fs/copy.rs | 18 ++++-------------- core/src/object/fs/cut.rs | 8 +++++++- core/src/object/fs/erase.rs | 4 ++-- core/src/object/fs/mod.rs | 16 +++++++++++++--- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/core/src/job/mod.rs b/core/src/job/mod.rs index 0dab92de7..f378f0941 100644 --- a/core/src/job/mod.rs +++ b/core/src/job/mod.rs @@ -47,7 +47,7 @@ pub enum JobError { MissingData { value: String }, #[error("Location manager error: {0}")] LocationManager(#[from] LocationManagerError), - #[error("error handling OS strings")] + #[error("error converting/handling OS strings")] OsStr, #[error("error converting/handling paths")] Path, diff --git a/core/src/object/fs/copy.rs b/core/src/object/fs/copy.rs index f02c6f3bc..68f55bef0 100644 --- a/core/src/object/fs/copy.rs +++ b/core/src/object/fs/copy.rs @@ -1,8 +1,8 @@ -use super::{context_menu_fs_info, get_path_from_location_id, FsInfo, ObjectType}; +use super::{context_menu_fs_info, get_path_from_location_id, osstr_to_string, FsInfo, ObjectType}; use crate::job::{JobError, JobReportUpdate, JobResult, JobState, StatefulJob, WorkerContext}; use serde::{Deserialize, Serialize}; use specta::Type; -use std::{collections::VecDeque, ffi::OsStr, hash::Hash, path::PathBuf}; +use std::{collections::VecDeque, hash::Hash, path::PathBuf}; pub struct FileCopierJob {} @@ -29,16 +29,6 @@ pub struct FileCopierJobStep { const JOB_NAME: &str = "file_copier"; -pub fn osstr_to_string(os_str: Option<&OsStr>) -> Result { - let string = os_str - .ok_or(JobError::OsStr)? - .to_str() - .ok_or(JobError::OsStr)? - .to_string(); - - Ok(string) -} - #[async_trait::async_trait] impl StatefulJob for FileCopierJob { type Data = FileCopierJobState; @@ -123,7 +113,7 @@ impl StatefulJob for FileCopierJob { // if root type is a dir, we need to preserve structure by making paths relative path.push( info.obj_path - .strip_prefix(job_state.source_path.clone()) + .strip_prefix(&job_state.source_path) .map_err(|_| JobError::Path)?, ); } @@ -155,7 +145,7 @@ impl StatefulJob for FileCopierJob { path.push( entry .path() - .strip_prefix(job_state.source_path.clone()) + .strip_prefix(&job_state.source_path) .map_err(|_| JobError::Path)?, ); diff --git a/core/src/object/fs/cut.rs b/core/src/object/fs/cut.rs index 60203c6e4..f727fb469 100644 --- a/core/src/object/fs/cut.rs +++ b/core/src/object/fs/cut.rs @@ -67,7 +67,13 @@ impl StatefulJob for FileCutterJob { let source_info = &step.source_fs_info; let mut full_output = step.target_directory.clone(); - full_output.push(source_info.obj_path.clone().file_name().unwrap()); + full_output.push( + source_info + .obj_path + .clone() + .file_name() + .ok_or(JobError::OsStr)?, + ); dbg!(source_info.obj_path.clone()); dbg!(full_output.clone()); diff --git a/core/src/object/fs/erase.rs b/core/src/object/fs/erase.rs index 316965f89..e61468ecf 100644 --- a/core/src/object/fs/erase.rs +++ b/core/src/object/fs/erase.rs @@ -1,4 +1,4 @@ -use super::{context_menu_fs_info, FsInfo, ObjectType}; +use super::{context_menu_fs_info, osstr_to_string, FsInfo, ObjectType}; use crate::job::{JobError, JobReportUpdate, JobResult, JobState, StatefulJob, WorkerContext}; use serde::{Deserialize, Serialize}; use specta::Type; @@ -105,7 +105,7 @@ impl StatefulJob for FileEraserJob { state.steps.push_back(FileEraserJobStep { fs_info: FsInfo { obj_id: None, - obj_name: entry.file_name().to_str().unwrap().to_string(), + obj_name: osstr_to_string(Some(&entry.file_name()))?, obj_path: entry.path(), obj_type, }, diff --git a/core/src/object/fs/mod.rs b/core/src/object/fs/mod.rs index 4e7724324..01e21f170 100644 --- a/core/src/object/fs/mod.rs +++ b/core/src/object/fs/mod.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{ffi::OsStr, path::PathBuf}; use serde::{Deserialize, Serialize}; @@ -28,6 +28,16 @@ pub struct FsInfo { pub obj_type: ObjectType, } +pub fn osstr_to_string(os_str: Option<&OsStr>) -> Result { + let string = os_str + .ok_or(JobError::OsStr)? + .to_str() + .ok_or(JobError::OsStr)? + .to_string(); + + Ok(string) +} + pub async fn get_path_from_location_id( db: &PrismaClient, location_id: i32, @@ -41,13 +51,13 @@ pub async fn get_path_from_location_id( value: String::from("location which matches location_id"), })?; - Ok(location + location .local_path .as_ref() .map(PathBuf::from) .ok_or(JobError::MissingData { value: String::from("path when cast as `PathBuf`"), - })?) + }) } pub async fn context_menu_fs_info(