major cleanup + clippy

This commit is contained in:
brxken128 2023-01-18 14:50:36 +00:00
parent 0af8f5bf57
commit 4d79239957
5 changed files with 27 additions and 21 deletions

View file

@ -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,

View file

@ -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<String, JobError> {
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)?,
);

View file

@ -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());

View file

@ -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,
},

View file

@ -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<String, JobError> {
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(