Skip to content

Commit 4dfee79

Browse files
committed
feat: add err ctx
1 parent 9fcdbad commit 4dfee79

File tree

9 files changed

+139
-134
lines changed

9 files changed

+139
-134
lines changed

src/api/ing/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,26 @@ pub enum IngSendFrom {
4747

4848
pub fn ing_star_tag_to_text(tag: &str) -> String {
4949
lazy_static! {
50-
static ref REGEX: Regex = Regex::new(r#"<img.*alt="\[(.*?)]"(\n|.)*>"#).unwrap();
50+
static ref REGEX: Regex =
51+
Regex::new(r#"<img.*alt="\[(.*?)]"(\n|.)*>"#).expect("Invalid regexp");
5152
}
52-
let caps = REGEX.captures(tag).expect(tag);
53-
let text = caps.get(1).unwrap().as_str();
53+
let caps = REGEX
54+
.captures(tag)
55+
.unwrap_or_else(|| panic!("No captures for: {}", tag));
56+
let text = caps.get(1).expect("No capture at index 1").as_str();
5457
text.to_string()
5558
}
5659

5760
pub fn fmt_content(content: &str) -> String {
5861
lazy_static! {
5962
static ref REGEX: Regex =
60-
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>(@.*?)</a>"#).unwrap();
63+
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>(@.*?)</a>"#)
64+
.expect("Invalid regexp");
6165
}
6266
REGEX.captures(content).map_or_else(
6367
|| content.to_string(),
6468
|caps| {
65-
let at_user = caps.get(1).unwrap().as_str();
69+
let at_user = caps.get(1).expect("No capture at index 1").as_str();
6670
REGEX.replace(content, at_user).to_string()
6771
},
6872
)
@@ -71,18 +75,25 @@ pub fn fmt_content(content: &str) -> String {
7175
pub fn rm_ing_at_user_tag(text: &str) -> String {
7276
lazy_static! {
7377
static ref REGEX: Regex =
74-
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>(@.*?)</a>:"#).unwrap();
78+
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>(@.*?)</a>:"#)
79+
.expect("Invalid regexp");
7580
}
7681
REGEX.replace(text, "".to_string()).to_string()
7782
}
7883

7984
pub fn get_ing_at_user_tag_text(text: &str) -> String {
8085
lazy_static! {
8186
static ref REGEX: Regex =
82-
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>@(.*?)</a>:"#).unwrap();
87+
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>@(.*?)</a>:"#)
88+
.expect("Invalid regexp");
8389
}
8490
REGEX.captures(text).map_or_else(
8591
|| "".to_string(),
86-
|caps| caps.get(1).unwrap().as_str().to_string(),
92+
|caps| {
93+
caps.get(1)
94+
.expect("No capture at index 1")
95+
.as_str()
96+
.to_string()
97+
},
8798
)
8899
}

src/api/post/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Post {
2727
let id = {
2828
let body = body_or_err(resp).await?;
2929
let json = json::deserialize::<Value>(&body)?;
30-
json["id"].as_u64().unwrap() as usize
30+
json["id"].as_u64().expect("as_u64 failed for `id`") as usize
3131
};
3232

3333
id.into_ok()

src/api/post/get_count.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ impl Post {
2424
let count = {
2525
let body = body_or_err(resp).await?;
2626
let json = json::deserialize::<Value>(&body)?;
27-
json["postsCount"].as_u64().unwrap() as usize
27+
json["postsCount"]
28+
.as_u64()
29+
.expect("as_u64 failed for `postsCount`") as usize
2830
};
2931

3032
count.into_ok()

src/api/post/search.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ impl Post {
3737
{
3838
let body = body_or_err(resp).await?;
3939
let json = json::deserialize::<Value>(&body)?;
40-
json["postsCount"].as_u64().unwrap() as usize
40+
json["postsCount"]
41+
.as_u64()
42+
.expect("as_u64 failed for `postsCount`") as usize
4143
}
4244
};
4345

@@ -62,7 +64,7 @@ impl Post {
6264
let post_id = {
6365
let json = json["postList"].take();
6466
let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?;
65-
post["id"].as_u64().unwrap() as usize
67+
post["id"].as_u64().expect("as_u64 failed for `id`") as usize
6668
};
6769
let zzk_post_id_list = {
6870
let json = json["zzkSearchResult"]["postIds"].take();

src/api/post/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Post {
4141
let id = {
4242
let body = body_or_err(resp).await?;
4343
let json = json::deserialize::<Value>(&body)?;
44-
json["id"].as_u64().unwrap() as usize
44+
json["id"].as_u64().expect("as_u64 failed for `id`") as usize
4545
};
4646

4747
id.into_ok()

src/display/colorful.rs

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,19 @@ pub fn user_info(info: &Result<UserInfo>) {
4949
}
5050

5151
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
52-
if let Err(e) = ing_list {
53-
println_err(e);
54-
return;
55-
}
52+
let ing_list = match ing_list {
53+
Ok(o) => o,
54+
Err(e) => return println_err(e),
55+
};
5656

5757
ing_list
58-
.as_ref()
59-
.unwrap()
6058
.iter()
6159
.dyn_rev(rev)
6260
.for_each(|(ing, comment_list)| {
6361
let create_time = {
6462
let rfc3339 = patch_rfc3339(&ing.create_time);
65-
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
63+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
64+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
6665
dt.format("%m-%d %H:%M").to_string()
6766
};
6867

@@ -122,12 +121,11 @@ pub fn show_post(entry: &Result<PostEntry>) {
122121
}
123122

124123
pub fn show_post_meta(entry: &Result<PostEntry>) {
125-
if let Err(e) = entry {
126-
println_err(e);
127-
return;
128-
}
124+
let entry = match entry {
125+
Ok(entry) => entry,
126+
Err(e) => return println_err(e),
127+
};
129128

130-
let entry = entry.as_ref().unwrap();
131129
println!("Title {}", entry.title.cyan().bold());
132130
{
133131
print!("Status");
@@ -156,26 +154,27 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
156154
}
157155
let create_time = {
158156
let rfc3339 = patch_rfc3339(&entry.create_time);
159-
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
157+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
158+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
160159
dt.format("%Y-%m-%d %H:%M")
161160
};
162161
println!("Create {}", create_time);
163162
let modify_time = {
164163
let rfc3339 = patch_rfc3339(&entry.modify_time);
165-
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
164+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
165+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
166166
dt.format("%Y-%m-%d %H:%M")
167167
};
168168
println!("Modify {}", modify_time);
169169
println!("Link https:{}", entry.url);
170170
}
171171

172172
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
173-
if let Err(e) = result {
174-
println_err(e);
175-
return;
176-
}
173+
let (entry_list, total_count) = match result {
174+
Ok(o) => o,
175+
Err(e) => return println_err(e),
176+
};
177177

178-
let (entry_list, total_count) = result.as_ref().unwrap();
179178
println!("{}/{}", entry_list.len(), total_count);
180179
entry_list.iter().dyn_rev(rev).for_each(|entry| {
181180
print!("{} {}", "#".dimmed(), entry.id.to_string().dimmed());
@@ -193,12 +192,11 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
193192
}
194193

195194
pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) {
196-
if let Err(e) = result {
197-
println_err(e);
198-
return;
199-
}
195+
let (id_list, total_count) = match result {
196+
Ok(o) => o,
197+
Err(e) => return println_err(e),
198+
};
200199

201-
let (id_list, total_count) = result.as_ref().unwrap();
202200
println!("{}/{}", id_list.len(), total_count);
203201
id_list
204202
.iter()
@@ -218,27 +216,23 @@ pub fn println_result<T: Display>(result: &Result<T>) {
218216
}
219217

220218
pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
221-
if let Err(e) = news_list {
222-
println_err(e);
223-
return;
224-
}
225-
226-
news_list
227-
.as_ref()
228-
.unwrap()
229-
.iter()
230-
.dyn_rev(rev)
231-
.for_each(|news| {
232-
let create_time = {
233-
let rfc3339 = patch_rfc3339(&news.create_time);
234-
let dt = DateTime::parse_from_rfc3339(&rfc3339).unwrap();
235-
dt.format("%Y-%m-%d %H:%M").to_string()
236-
};
219+
let news_list = match news_list {
220+
Ok(o) => o,
221+
Err(e) => return println_err(e),
222+
};
237223

238-
let url = format!("https://news.cnblogs.com/n/{}", news.id);
239-
println!("{} {}", create_time.dimmed(), url.dimmed(),);
240-
println!(" {}", news.title);
241-
println!(" {}{}", news.summary.dimmed(), "...".dimmed());
242-
println!();
243-
});
224+
news_list.iter().dyn_rev(rev).for_each(|news| {
225+
let create_time = {
226+
let rfc3339 = patch_rfc3339(&news.create_time);
227+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
228+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
229+
dt.format("%Y-%m-%d %H:%M").to_string()
230+
};
231+
232+
let url = format!("https://news.cnblogs.com/n/{}", news.id);
233+
println!("{} {}", create_time.dimmed(), url.dimmed(),);
234+
println!(" {}", news.title);
235+
println!(" {}{}", news.summary.dimmed(), "...".dimmed());
236+
println!();
237+
});
244238
}

src/display/json.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ pub fn user_info(info: &Result<UserInfo>) {
2424
}
2525

2626
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
27-
if let Err(e) = ing_list {
28-
println_err(e);
29-
return;
30-
}
27+
let ing_list = match ing_list {
28+
Ok(o) => o,
29+
Err(e) => return println_err(e),
30+
};
3131

3232
let vec = ing_list
33-
.as_ref()
34-
.unwrap()
3533
.iter()
3634
.dyn_rev(rev)
3735
.map(|(entry, comment_list)| {
@@ -41,7 +39,9 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
4139
})
4240
})
4341
.collect::<Vec<_>>();
44-
let json = json::serialize(vec).unwrap();
42+
43+
let json =
44+
json::serialize(vec.clone()).unwrap_or_else(|_| panic!("Can not serialize: {:?}", vec));
4545
print!("{}", json);
4646
}
4747

@@ -60,11 +60,11 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
6060
}
6161

6262
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
63-
if let Err(e) = result {
64-
println_err(e);
65-
return;
66-
}
67-
let (entry_list, total_count) = result.as_ref().unwrap();
63+
let (entry_list, total_count) = match result {
64+
Ok(o) => o,
65+
Err(e) => return println_err(e),
66+
};
67+
6868
let vec = entry_list.iter().dyn_rev(rev).collect::<Vec<_>>();
6969
let json = json!({
7070
"listed_count": vec.len(),
@@ -75,12 +75,11 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
7575
}
7676

7777
pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) {
78-
if let Err(e) = result {
79-
println_err(e);
80-
return;
81-
}
78+
let (id_list, total_count) = match result {
79+
Ok(o) => o,
80+
Err(e) => return println_err(e),
81+
};
8282

83-
let (id_list, total_count) = result.as_ref().unwrap();
8483
let id_list = id_list.iter().dyn_rev(rev).collect::<Vec<&usize>>();
8584
let json = json!({
8685
"listed_count": id_list.len(),
@@ -114,16 +113,14 @@ pub fn println_result<T: Serialize, E: ToString>(result: &Result<T, E>) {
114113
}
115114

116115
pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
117-
if let Err(e) = news_list {
118-
println_err(e);
119-
return;
120-
}
121-
let vec = news_list
122-
.as_ref()
123-
.unwrap()
124-
.iter()
125-
.dyn_rev(rev)
126-
.collect::<Vec<_>>();
127-
let json = json::serialize(vec).unwrap();
116+
let news_list = match news_list {
117+
Ok(o) => o,
118+
Err(e) => return println_err(e),
119+
};
120+
121+
let vec = news_list.iter().dyn_rev(rev).collect::<Vec<_>>();
122+
123+
let json =
124+
json::serialize(vec.clone()).unwrap_or_else(|_| panic!("Can not serialize: {:?}", vec));
128125
print!("{}", json);
129126
}

0 commit comments

Comments
 (0)