Skip to content

Commit b02ffe9

Browse files
committed
fix: normalize json output style
1 parent 71b8251 commit b02ffe9

File tree

6 files changed

+38
-43
lines changed

6 files changed

+38
-43
lines changed

src/display/json/fav.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use crate::api::fav::get_list::FavEntry;
2-
use crate::display::json::fmt_err;
3-
use crate::infra::json;
4-
use crate::infra::result::IntoResult;
2+
use crate::display::json::{fmt_err, fmt_ok};
53
use anyhow::Result;
64

7-
pub fn list_fav(fav_iter: Result<impl ExactSizeIterator<Item = FavEntry>>) -> Result<String> {
5+
pub fn list_fav(fav_iter: Result<impl ExactSizeIterator<Item = FavEntry>>) -> String {
86
let fav_iter = match fav_iter {
97
Ok(o) => o,
10-
Err(e) => return fmt_err(&e).into_ok(),
8+
Err(e) => return fmt_err(&e),
119
};
1210

1311
let vec = fav_iter.collect::<Vec<_>>();
1412

15-
json::serialize(vec)
13+
fmt_ok(vec)
1614
}

src/display/json/ing.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use crate::api::ing::get_comment_list::IngCommentEntry;
22
use crate::api::ing::get_list::IngEntry;
3-
use crate::display::json::fmt_err;
4-
use crate::infra::json;
5-
use crate::infra::result::IntoResult;
3+
use crate::display::json::{fmt_err, fmt_ok};
64
use anyhow::Result;
75
use serde_json::json;
86

97
pub fn list_ing(
108
ing_with_comment_list: Result<impl ExactSizeIterator<Item = (IngEntry, Vec<IngCommentEntry>)>>,
11-
) -> Result<String> {
9+
) -> String {
1210
let ing_with_comment_list = match ing_with_comment_list {
1311
Ok(o) => o,
14-
Err(e) => return fmt_err(&e).into_ok(),
12+
Err(e) => return fmt_err(&e),
1513
};
1614

1715
let json_vec = ing_with_comment_list
@@ -23,5 +21,5 @@ pub fn list_ing(
2321
})
2422
.collect::<Vec<_>>();
2523

26-
json::serialize(json_vec)
24+
fmt_ok(json_vec)
2725
}

src/display/json/mod.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ pub mod user;
77
use anyhow::Result;
88
use serde::Serialize;
99
use serde_json::json;
10+
use std::fmt::Display;
1011

1112
#[inline]
12-
pub fn fmt_err(e: &anyhow::Error) -> String {
13+
pub fn fmt_ok(t: impl Serialize) -> String {
14+
let json = json!({
15+
"is_ok": true,
16+
"msg": t
17+
});
18+
json.to_string()
19+
}
20+
21+
#[inline]
22+
pub fn fmt_err(e: impl ToString) -> String {
1323
let json = json!({
1424
"is_ok": false,
1525
"msg": e.to_string()
1626
});
1727
json.to_string()
1828
}
1929

20-
pub fn fmt_result<T: Serialize, E: ToString>(result: &Result<T, E>) -> String {
21-
let json = match result {
22-
Ok(t) => json!({
23-
"is_ok": true,
24-
"msg": t
25-
}),
26-
Err(e) => json!({
27-
"is_ok": false,
28-
"msg": e.to_string()
29-
}),
30-
};
31-
json.to_string()
30+
pub fn fmt_result<T: Serialize, E: Display>(result: &Result<T, E>) -> String {
31+
match result {
32+
Ok(t) => fmt_ok(t),
33+
Err(e) => fmt_err(e),
34+
}
3235
}

src/display/json/news.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use crate::api::news::get_list::NewsEntry;
2-
use crate::display::json::fmt_err;
3-
use crate::infra::json;
4-
use crate::infra::result::IntoResult;
2+
use crate::display::json::{fmt_err, fmt_ok};
53
use anyhow::Result;
64

7-
pub fn list_news(news_iter: Result<impl ExactSizeIterator<Item = NewsEntry>>) -> Result<String> {
5+
pub fn list_news(news_iter: Result<impl ExactSizeIterator<Item = NewsEntry>>) -> String {
86
let news_iter = match news_iter {
97
Ok(o) => o,
10-
Err(e) => return fmt_err(&e).into_ok(),
8+
Err(e) => return fmt_err(&e),
119
};
1210

1311
let vec = news_iter.collect::<Vec<_>>();
1412

15-
json::serialize(vec)
13+
fmt_ok(vec)
1614
}

src/display/json/post.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::api::post::get_comment_list::PostCommentEntry;
22
use crate::api::post::get_one::PostEntry;
3-
use crate::display::json::{fmt_err, fmt_result};
4-
use crate::infra::json;
5-
use crate::infra::result::IntoResult;
3+
use crate::display::json::{fmt_err, fmt_ok, fmt_result};
64
use anyhow::Result;
75
use serde_json::json;
86

@@ -18,7 +16,7 @@ pub fn list_post(result: Result<(impl ExactSizeIterator<Item = PostEntry>, usize
1816
"total_count": total_count,
1917
"entry_list": vec,
2018
});
21-
json.to_string()
19+
fmt_ok(json)
2220
}
2321

2422
pub fn show_post(entry: &Result<PostEntry>) -> String {
@@ -37,14 +35,14 @@ pub fn show_post_meta(entry: &Result<PostEntry>) -> String {
3735

3836
pub fn show_post_comment(
3937
comment_iter: Result<impl ExactSizeIterator<Item = PostCommentEntry>>,
40-
) -> Result<String> {
38+
) -> String {
4139
let comment_iter = match comment_iter {
4240
Ok(entry) => entry,
43-
Err(e) => return fmt_err(&e).into_ok(),
41+
Err(e) => return fmt_err(&e),
4442
};
4543

4644
let comment_vec = comment_iter.collect::<Vec<_>>();
47-
json::serialize(comment_vec)
45+
fmt_ok(comment_vec)
4846
}
4947

5048
pub fn search_post(result: Result<(impl ExactSizeIterator<Item = usize>, usize)>) -> String {
@@ -59,5 +57,5 @@ pub fn search_post(result: Result<(impl ExactSizeIterator<Item = usize>, usize)>
5957
"total_count": total_count,
6058
"id_list": id_list,
6159
});
62-
json.to_string()
60+
fmt_ok(json)
6361
}

src/display/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn list_ing(
4747
match style {
4848
Style::Colorful => colorful::ing::list_ing(time_style, ing_with_comment_iter, align),
4949
Style::Normal => normal::ing::list_ing(time_style, ing_with_comment_iter, align),
50-
Style::Json => json::ing::list_ing(ing_with_comment_iter),
50+
Style::Json => json::ing::list_ing(ing_with_comment_iter).into_ok(),
5151
}
5252
}
5353

@@ -106,7 +106,7 @@ pub fn show_post_comment(
106106
match style {
107107
Style::Colorful => colorful::post::show_post_comment(time_style, comment_iter),
108108
Style::Normal => normal::post::show_post_comment(time_style, comment_iter),
109-
Style::Json => json::post::show_post_comment(comment_iter),
109+
Style::Json => json::post::show_post_comment(comment_iter).into_ok(),
110110
}
111111
}
112112

@@ -153,7 +153,7 @@ pub fn list_news(
153153
match style {
154154
Style::Colorful => colorful::news::list_news(time_style, news_iter),
155155
Style::Normal => normal::news::list_news(time_style, news_iter),
156-
Style::Json => json::news::list_news(news_iter),
156+
Style::Json => json::news::list_news(news_iter).into_ok(),
157157
}
158158
}
159159

@@ -165,6 +165,6 @@ pub fn list_fav(
165165
match style {
166166
Style::Colorful => colorful::fav::list_fav(time_style, fav_iter),
167167
Style::Normal => normal::fav::list_fav(time_style, fav_iter),
168-
Style::Json => json::fav::list_fav(fav_iter),
168+
Style::Json => json::fav::list_fav(fav_iter).into_ok(),
169169
}
170170
}

0 commit comments

Comments
 (0)