Skip to content

Commit 521b918

Browse files
committed
regular matching of READMD.md failed, unable to parse the field
1 parent 5996485 commit 521b918

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/file.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use regex::Regex;
66
use crate::http::{Resp, Data, Ques, Difficulty};
77

88
lazy_static!(
9-
static ref RE: Regex = Regex::new(r"\|\s*([0-9]*)\s*\|\s*(.*?)\s*\|.*?bin/(.*?)\.rs.*?\|.*?\|").unwrap();
9+
static ref RE: Regex = Regex::new(r"\|\s*([0-9]*)\s*\|\s*(.*?)\s*\|.*?bin/(.*?)\.rs.*?\|.*?\|\s*?(\w*)\s*?\|").unwrap();
1010
);
1111

1212
/// 将结果写入README.md中
@@ -77,7 +77,7 @@ fn parse(contents: &str) -> Vec<Resp> {
7777
translated_title: i.get(2).unwrap().as_str().to_string(),
7878
title_slug: i.get(3).unwrap().as_str().to_string(),
7979
code_snippets: vec![],
80-
difficulty: Difficulty::Easy,
80+
difficulty: Difficulty::new(i.get(4).unwrap().as_str()),
8181
}
8282
},
8383
})
@@ -99,7 +99,12 @@ mod tests {
9999
println!("{}", x.len());
100100

101101
for i in x {
102-
println!("{}, {}, {}", i.data.question.translated_title, i.data.question.question_id, i.data.question.title_slug);
102+
println!("{}, {}, {}, {:?}",
103+
i.data.question.translated_title,
104+
i.data.question.question_id,
105+
i.data.question.title_slug,
106+
i.data.question.difficulty
107+
);
103108
}
104109
}
105110

src/http.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ pub enum Difficulty {
2020
Hard,
2121
}
2222

23+
impl Difficulty {
24+
pub fn new(s: &str) -> Self {
25+
match s {
26+
"Easy" => Self::Easy,
27+
"Medium" => Self::Medium,
28+
"Hard" => Self::Hard,
29+
_ => Self::Easy
30+
}
31+
}
32+
}
33+
2334
impl Serialize for Difficulty {
2435
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2536
where
@@ -117,7 +128,7 @@ pub fn get_question_info(mut ques: &str) -> Resp {
117128
.send()
118129
.unwrap()
119130
.json::<Resp>()
120-
.unwrap()
131+
.expect(format!("{} download failed", ques).as_str())
121132
}
122133

123134
#[cfg(test)]

0 commit comments

Comments
 (0)