use super::{Priority, Task, TaskType};
use uuid::Uuid;
pub struct TaskBuilder {
description: String,
priority: Option<Priority>,
task_type: Option<TaskType>,
details: Option<String>,
depends_on: Vec<String>,
estimated_duration: Option<u64>,
}
impl TaskBuilder {
pub fn new(description: String) -> Self {
Self {
description,
priority: None,
task_type: None,
details: None,
depends_on: Vec::new(),
estimated_duration: None,
}
}
pub fn parse(input: &str) -> Self {
let (desc, priority, task_type) = Self::parse_modifiers(input);
let mut builder = Self::new(desc);
builder.priority = Some(priority);
builder.task_type = Some(task_type);
builder
}
pub fn parse_modifiers(desc: &str) -> (String, Priority, TaskType) {
let mut description = desc.to_string();
let mut priority = Priority::Medium;
let mut task_type = TaskType::Development;
if let Some(start) = description.find('[')
&& let Some(end) = description[start..].find(']')
{
let modifier = &description[start + 1..start + end].to_lowercase();
if let Ok(p) = modifier.parse::<Priority>() {
priority = p;
description = format!(
"{}{}",
&description[..start],
&description[start + end + 1..]
)
.trim()
.to_string();
}
if let Ok(t) = modifier.parse::<TaskType>() {
task_type = t;
description = format!(
"{}{}",
&description[..start],
&description[start + end + 1..]
)
.trim()
.to_string();
}
}
if let Some(start) = description.find('[')
&& let Some(end) = description[start..].find(']')
{
let modifier = &description[start + 1..start + end].to_lowercase();
if let Ok(t) = modifier.parse::<TaskType>() {
task_type = t;
description = format!(
"{}{}",
&description[..start],
&description[start + end + 1..]
)
.trim()
.to_string();
}
}
(description.trim().to_string(), priority, task_type)
}
pub fn priority(mut self, priority: Priority) -> Self {
self.priority = Some(priority);
self
}
pub fn task_type(mut self, task_type: TaskType) -> Self {
self.task_type = Some(task_type);
self
}
pub fn details(mut self, details: String) -> Self {
self.details = Some(details);
self
}
pub fn depends_on(mut self, task_id: String) -> Self {
self.depends_on.push(task_id);
self
}
pub fn estimated_duration(mut self, minutes: u64) -> Self {
self.estimated_duration = Some(minutes);
self
}
pub fn build(self) -> Task {
Task {
id: Uuid::new_v4().to_string(),
description: self.description,
priority: self.priority.unwrap_or(Priority::Medium),
task_type: self.task_type.unwrap_or(TaskType::Development),
details: self.details,
estimated_duration: self.estimated_duration.map(|d| d as u32),
assigned_to: None,
parent_task_id: None,
quality_issues: None,
metadata: None,
}
}
}