Skip to content

Commit

Permalink
feat: add description parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-pitblado committed Nov 6, 2024
1 parent 4b6501f commit 2987f2c
Showing 1 changed file with 75 additions and 2 deletions.
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 2987f2c

Please sign in to comment.