Skip to content

Commit

Permalink
Merge pull request #4 from mark-pitblado/add-with-task-description
Browse files Browse the repository at this point in the history
Add a description to a task via {}
  • Loading branch information
mark-pitblado authored Nov 6, 2024
2 parents 4b6501f + a92eaa2 commit 425eeb0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub async fn create_new_task(
instance_url: &str,
api_key: &str,
task_title: &str,
description: Option<&str>,
priority: Option<u8>,
) -> Result<(), Box<dyn Error>> {
let client = Client::new();
Expand All @@ -58,6 +59,10 @@ pub async fn create_new_task(
"title": task_title
});

if let Some(desc) = description {
task_data["description"] = json!(desc);
}

if let Some(priority_value) = priority {
task_data["priority"] = json!(priority_value);
}
Expand Down
3 changes: 1 addition & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ impl App {
match key.code {
KeyCode::Enter => {
if !self.new_task_title.trim().is_empty() {
// Use the parser to extract the task title, priority, and label titles
let parsed_task = parse_task_input(&self.new_task_title);

// Create the new task with the parsed title, priority, and labels
if let Err(err) = create_new_task(
instance_url,
api_key,
&parsed_task.title,
parsed_task.description.as_deref(),
parsed_task.priority,
)
.await
Expand Down
77 changes: 75 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ use regex::Regex;
#[derive(Debug, PartialEq)]
pub struct ParsedTask {
pub title: String,
pub description: Option<String>,
pub priority: Option<u8>,
}

pub fn parse_task_input(input: &str) -> ParsedTask {
let priority_re = Regex::new(r"!(\d+)\s*").unwrap();
let description_re = Regex::new(r"\{([^}]*)\}").unwrap();

let mut priority = None;
let mut description = None;

// Priority
for caps in priority_re.captures_iter(input) {
if let Some(priority_match) = caps.get(1) {
if let Ok(p) = priority_match.as_str().parse::<u8>() {
Expand All @@ -21,27 +25,90 @@ pub fn parse_task_input(input: &str) -> ParsedTask {
}
}

let title = priority_re.replace_all(input, "");
// Description
let mut title = input.to_string();
if let Some(caps) = description_re.captures(&title) {
if let Some(desc_match) = caps.get(1) {
description = Some(desc_match.as_str().trim().to_string());
}
}

title = description_re.replace_all(&title, "").to_string();

title = priority_re.replace_all(&title, "").to_string();

let title = Regex::new(r"\s+")
.unwrap()
.replace_all(&title, " ")
.trim()
.to_string();

ParsedTask { title, priority }
ParsedTask {
title,
priority,
description,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_with_description() {
let input = "Implement feature X {This is the description of the task}";
let expected = ParsedTask {
title: "Implement feature X".to_string(),
description: Some("This is the description of the task".to_string()),
priority: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
}

#[test]
fn test_parse_with_description_and_priority() {
let input = "Fix bug in module !2 {Critical issue that needs immediate attention}";
let expected = ParsedTask {
title: "Fix bug in module".to_string(),
description: Some("Critical issue that needs immediate attention".to_string()),
priority: Some(2),
};
let result = parse_task_input(input);
assert_eq!(result, expected);
}

#[test]
fn test_parse_with_description_and_priority_in_any_order() {
let input = "{Detailed description here} Update documentation !3";
let expected = ParsedTask {
title: "Update documentation".to_string(),
description: Some("Detailed description here".to_string()),
priority: Some(3),
};
let result = parse_task_input(input);
assert_eq!(result, expected);
}

#[test]
fn test_parse_with_multiple_descriptions() {
let input = "Task title {First description} {Second description}";
let expected = ParsedTask {
title: "Task title".to_string(),
description: Some("First description".to_string()),
priority: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
}

#[test]
fn test_parse_with_priority_in_middle() {
let input = "Update !4 software documentation";
let expected = ParsedTask {
title: "Update software documentation".to_string(),
priority: Some(4),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -53,6 +120,7 @@ mod tests {
let expected = ParsedTask {
title: "Fix bugs in the code".to_string(),
priority: Some(2),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -64,6 +132,7 @@ mod tests {
let expected = ParsedTask {
title: "Write tests for the parser".to_string(),
priority: Some(3),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -75,6 +144,7 @@ mod tests {
let expected = ParsedTask {
title: "Deploy to production".to_string(),
priority: Some(5),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -86,6 +156,7 @@ mod tests {
let expected = ParsedTask {
title: "Prepare presentation slides".to_string(),
priority: Some(2),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -97,6 +168,7 @@ mod tests {
let expected = ParsedTask {
title: "Organize team building event".to_string(),
priority: Some(1),
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand All @@ -108,6 +180,7 @@ mod tests {
let expected = ParsedTask {
title: "Check logs immediately".to_string(),
priority: None,
description: None,
};
let result = parse_task_input(input);
assert_eq!(result, expected);
Expand Down

0 comments on commit 425eeb0

Please sign in to comment.