typeless jobs

feat/vaults
Tomáš Mládek 2022-02-10 10:47:10 +01:00
parent 7338a27c22
commit bbc871ccf6
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
1 changed files with 22 additions and 16 deletions

View File

@ -5,16 +5,20 @@ use uuid::Uuid;
#[derive(Default, Serialize, Clone)]
pub struct Job {
job_type: JobType,
job_type: Option<JobType>,
title: String,
progress: f32,
state: State,
}
impl Job {
pub fn new<S: AsRef<str>>(job_type: S, title: S) -> Self {
pub fn new<S, IS>(job_type: IS, title: S) -> Self
where
S: AsRef<str>,
IS: Into<Option<S>>,
{
return Job {
job_type: String::from(job_type.as_ref()),
job_type: job_type.into().map(|jt| String::from(jt.as_ref())),
title: String::from(title.as_ref()),
..Default::default()
};
@ -66,20 +70,22 @@ pub struct JobInProgessError(String);
impl JobContainer {
pub fn add_job(&mut self, job: Job) -> Result<JobId, JobInProgessError> {
if self
.jobs
.iter()
.any(|(_, j)| j.state == State::InProgress && j.job_type == job.job_type)
{
Err(JobInProgessError(format!(
"Job of type \"{}\" currently in progress.",
job.job_type
)))
} else {
let uuid = Uuid::new_v4();
self.jobs.insert(JobId::from(uuid), job);
Ok(JobId::from(uuid))
if let Some(job_type) = &job.job_type {
if self
.jobs
.iter()
.any(|(_, j)| j.state == State::InProgress && j.job_type == job.job_type)
{
return Err(JobInProgessError(format!(
"Job of type \"{}\" currently in progress.",
job_type
)))
}
}
let uuid = Uuid::new_v4();
self.jobs.insert(JobId::from(uuid), job);
Ok(JobId::from(uuid))
}
pub fn get_jobs(&self) -> HashMap<JobId, Job> {