1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use regex::Regex;

#[derive(PartialEq, Debug)]
pub struct Problem {
    pub number: String,
    pub name: String,
}

impl Problem {
    pub fn new(number: &str, name: &str) -> Self {
        Problem {
            number: number.to_string(),
            name: name.to_string(),
        }
    }
}

pub fn get_problem_from(title: String) -> Problem {
    let re = Regex::new(r"^(\d{1,4})\. ((?:[\w\d]+ ?)+)$").unwrap();

    match re.captures(&title) {
        Some(captures) => {
            let number = format!("{:0>4}", captures.get(1).unwrap().as_str().to_string());
            let name = captures
                .get(2)
                .unwrap()
                .as_str()
                .to_lowercase()
                .split_whitespace()
                .collect::<Vec<&str>>()
                .join("-");

            Problem { number, name }
        }
        None => panic!("Invalid LeetCode title"),
    }
}

#[cfg(test)]
mod tests {
    use crate::{get_problem_from, Problem};

    #[test]
    fn test_separates_number_and_name() {
        assert_eq!(
            get_problem_from("1234. Problem Name One".to_string()),
            Problem::new("1234", "problem-name-one")
        );
    }

    #[test]
    fn test_prepending_zeroes() {
        assert_eq!(
            get_problem_from("234. Problem Name One".to_string()),
            Problem::new("0234", "problem-name-one")
        );
        assert_eq!(
            get_problem_from("34. Problem Name One".to_string()),
            Problem::new("0034", "problem-name-one")
        );
        assert_eq!(
            get_problem_from("4. Problem Name One".to_string()),
            Problem::new("0004", "problem-name-one")
        );
    }

    #[test]
    #[should_panic(expected = "Invalid LeetCode title")]
    fn test_invalid_problem_title() {
        get_problem_from("Problem Name One".to_string());
    }
}