-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlyrics.rs
82 lines (70 loc) · 2.22 KB
/
lyrics.rs
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
73
74
75
76
77
78
79
80
81
82
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
lazy_static! {
static ref ALIASES: HashMap<&'static str, &'static str> = HashMap::from([
("A", "Heavily-processed vocalist"),
("B", "Both"),
("C", "Chorus"),
("G", "Gishnchips"),
("M", "Mos Prob"),
("J", "Jewdacris"),
("?", ""),
]);
}
#[derive(PartialEq, Eq)]
pub struct Phrase {
pub content: String,
pub attribution: String,
}
#[derive(PartialEq, Eq)]
pub struct Line {
pub phrases: Vec<Phrase>,
}
impl Line {
pub fn parse_lyrics(input: &str) -> Vec<Line> {
lazy_static! {
static ref RE: Regex = Regex::new(r"^((\s+)?([^:]+):: )?(.*)$").unwrap();
}
let mut lines = Vec::new();
let mut attribution: &str = "";
let mut line_phrases: Vec<Phrase> = Vec::new();
for input_line in input.lines() {
let mut line_is_over = true;
let cap = RE.captures(input_line).expect("could not parse line");
let content = cap.get(4).expect("could not get line content").as_str();
match cap.get(3) {
Some(a) => {
// this line has attribution to a vocalist
attribution = a.as_str();
match ALIASES.get(attribution) {
Some(a) => attribution = a,
None => (),
}
match cap.get(2) {
Some(_) => {
// there is leading whitespace in this line
line_is_over = false
}
None => (),
}
}
None => (),
}
if line_is_over && line_phrases.len() > 0 {
lines.push(Line {
phrases: line_phrases,
});
line_phrases = Vec::new();
}
line_phrases.push(Phrase {
content: String::from(content),
attribution: String::from(attribution),
});
}
lines.push(Line {
phrases: line_phrases,
});
lines
}
}