Skip to content

Commit

Permalink
wip: remove description parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-pitblado committed Nov 11, 2024
1 parent 86db899 commit d38d07a
Showing 1 changed file with 2 additions and 67 deletions.
69 changes: 2 additions & 67 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ 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) {
Expand All @@ -25,83 +22,21 @@ pub fn parse_task_input(input: &str) -> ParsedTask {
}
}

// 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 = priority_re.replace_all(&input, "").to_string();

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

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

#[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";
Expand Down

0 comments on commit d38d07a

Please sign in to comment.