Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0ba9da

Browse files
feat(async): use async-trait instead of blocking with tokio runtime everywhere
1 parent a6aa1b5 commit b0ba9da

File tree

16 files changed

+82
-72
lines changed

16 files changed

+82
-72
lines changed

‎CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v0.3.0
2+
3+
+ Upgrade reqwest to async mode
4+
+ Format code using clippy
5+
16
## v0.2.23
27

38
+ support color display

‎Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ path = "src/bin/lc.rs"
44

55
[package]
66
name = "leetcode-cli"
7-
version = "0.2.25"
7+
version = "0.3.0"
88
authors = ["clearloop <cdr.today@foxmail.com>"]
99
edition = "2018"
1010
description = "Leet your code in command-line."
@@ -16,6 +16,7 @@ keywords = ["cli", "games", "leetcode"]
1616
readme = './README.md'
1717

1818
[dependencies]
19+
async-trait = "0.1.41"
1920
tokio = "0.2.22"
2021
clap = "2.33.0"
2122
colored = "1.9.1"

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cargo install leetcode-cli
3030
**Please make sure you have logined in `leetcode.com` with `chrome`**, more info plz checkout [this](#cookies)
3131

3232
```sh
33-
leetcode 0.2.24
33+
leetcode 0.3.0
3434
May the Code be with You 👻
3535

3636
USAGE:

‎src/bin/lc.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
use leetcode_cli::cli;
2+
use tokio::runtime::Builder;
23

34
fn main() {
4-
let r = cli::main();
5-
if r.is_err() {
6-
println!("{:?}", r.err().expect("This won't happend."));
5+
if let Err(err) = Builder::new()
6+
.basic_scheduler()
7+
.enable_all()
8+
.build()
9+
.expect("Build tokio runtime failed")
10+
.block_on(cli::main())
11+
{
12+
println!("{:?}", err);
713
}
814
}

‎src/cache/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ impl Cache {
206206
use std::fs::File;
207207
use std::io::Read;
208208

209-
let p = &self.get_problem(rfid)?;
210-
// if p.desc.is_empty() {
211-
// trace!("Problem description does not exist, pull desc and exec again...");
212-
// self.get_question(rfid).await?;
213-
// return self.pre_run_code(run, rfid, testcase).await;
214-
// }
209+
let mutp = self.get_problem(rfid)?;
210+
if p.desc.is_empty() {
211+
trace!("Problem description does not exist, pull desc and exec again...");
212+
self.get_question(rfid).await?;
213+
p = self.get_problem(rfid)?;
214+
}
215215

216216
let d: Question = serde_json::from_str(&p.desc)?;
217217
let conf = &self.0.conf;
@@ -274,12 +274,6 @@ impl Cache {
274274
let debug_json: Result<VerifyResult, SJError> = from_str(&debug_raw);
275275
debug!("debug json deserializing: \n{:#?}", &debug_json);
276276

277-
// let mut res = debug_json?;
278-
// res = match res.state.as_str() {
279-
// "SUCCESS" => res,
280-
// _ => self.recur_verify(rid).await?,
281-
// };
282-
283277
Ok(debug_json?)
284278
}
285279

@@ -302,10 +296,13 @@ impl Cache {
302296
.json()
303297
.await?;
304298

305-
let mut res = match run {
306-
Run::Test => self.recur_verify(run_res.interpret_id).await?,
307-
Run::Submit => self.recur_verify(run_res.submission_id.to_string()).await?,
308-
};
299+
let mut res: VerifyResult = VerifyResult::default();
300+
while res.state != "SUCCESS" {
301+
res = match run {
302+
Run::Test => self.recur_verify(run_res.interpret_id.clone()).await?,
303+
Run::Submit => self.recur_verify(run_res.submission_id.to_string()).await?,
304+
};
305+
}
309306

310307
res.name = json.get("name")?.to_string();
311308
res.data_input = json.get("data_input")?.to_string();

‎src/cache/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ use super::parser::ssr;
199199
use crate::cache::Run;
200200

201201
/// verify result model
202-
#[derive(Debug, Deserialize)]
202+
#[derive(Default,Debug, Deserialize)]
203203
pub struct VerifyResult {
204204
pub state: String,
205205
#[serde(skip)]

‎src/cli.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use crate::{
88
flag::{Debug, Flag},
99
};
1010
use clap::{crate_name, crate_version, App, AppSettings};
11-
use tokio::runtime::Builder;
1211

1312
/// Get maches
14-
pub fn main() -> Result<(), Error> {
13+
pub asyncfn main() -> Result<(), Error> {
1514
let m = App::new(crate_name!())
1615
.version(crate_version!())
1716
.about("May the Code be with You 👻")
@@ -36,19 +35,14 @@ pub fn main() -> Result<(), Error> {
3635
.init();
3736
}
3837

39-
let mut runtime = Builder::new()
40-
.basic_scheduler()
41-
.enable_all()
42-
.build()
43-
.unwrap();
4438
match m.subcommand() {
45-
("data", Some(sub_m)) => Ok(DataCommand::handler(sub_m,&mut runtime)?),
46-
("edit", Some(sub_m)) => Ok(EditCommand::handler(sub_m,&mut runtime)?),
47-
("exec", Some(sub_m)) => Ok(ExecCommand::handler(sub_m,&mut runtime)?),
48-
("list", Some(sub_m)) => Ok(ListCommand::handler(sub_m,&mut runtime)?),
49-
("pick", Some(sub_m)) => Ok(PickCommand::handler(sub_m,&mut runtime)?),
50-
("stat", Some(sub_m)) => Ok(StatCommand::handler(sub_m,&mut runtime)?),
51-
("test", Some(sub_m)) => Ok(TestCommand::handler(sub_m,&mut runtime)?),
39+
("data", Some(sub_m)) => Ok(DataCommand::handler(sub_m).await?),
40+
("edit", Some(sub_m)) => Ok(EditCommand::handler(sub_m).await?),
41+
("exec", Some(sub_m)) => Ok(ExecCommand::handler(sub_m).await?),
42+
("list", Some(sub_m)) => Ok(ListCommand::handler(sub_m).await?),
43+
("pick", Some(sub_m)) => Ok(PickCommand::handler(sub_m).await?),
44+
("stat", Some(sub_m)) => Ok(StatCommand::handler(sub_m).await?),
45+
("test", Some(sub_m)) => Ok(TestCommand::handler(sub_m).await?),
5246
_ => Err(Error::MatchError),
5347
}
5448
}

‎src/cmds/data.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Cache managger
22
use super::Command;
33
use crate::{cache::Cache, helper::Digit};
4+
use async_trait::async_trait;
45
use clap::{App, Arg, ArgMatches, SubCommand};
56
use colored::Colorize;
6-
use tokio::runtime::Runtime;
77

88
/// Abstract `data` command
99
///
@@ -22,6 +22,7 @@ use tokio::runtime::Runtime;
2222
/// ```
2323
pub struct DataCommand;
2424

25+
#[async_trait]
2526
impl Command for DataCommand {
2627
/// `data` command usage
2728
fn usage<'a, 'cache>() -> App<'a, 'cache> {
@@ -45,7 +46,7 @@ impl Command for DataCommand {
4546
}
4647

4748
/// `data` handler
48-
fn handler(m: &ArgMatches,runtime:&mutRuntime) -> Result<(), crate::err::Error> {
49+
asyncfn handler(m: &ArgMatches<'_>) -> Result<(), crate::err::Error> {
4950
use std::fs::File;
5051
use std::path::Path;
5152

@@ -79,7 +80,7 @@ impl Command for DataCommand {
7980

8081
if m.is_present("update") {
8182
flags += 1;
82-
runtime.block_on(cache.update())?;
83+
cache.update().await?;
8384
println!("{}", "ok!".bright_green());
8485
}
8586

‎src/cmds/edit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Edit command
22
use super::Command;
3+
use async_trait::async_trait;
34
use clap::{App, ArgMatches};
4-
use tokio::runtime::Runtime;
55

66
/// Abstract `edit` command
77
///
@@ -21,6 +21,7 @@ use tokio::runtime::Runtime;
2121
/// ```
2222
pub struct EditCommand;
2323

24+
#[async_trait]
2425
impl Command for EditCommand {
2526
/// `edit` usage
2627
fn usage<'a, 'edit>() -> App<'a, 'edit> {
@@ -44,7 +45,7 @@ impl Command for EditCommand {
4445
}
4546

4647
/// `edit` handler
47-
fn handler(m: &ArgMatches,runtime:&mutRuntime) -> Result<(), crate::Error> {
48+
asyncfn handler(m: &ArgMatches<'_>) -> Result<(), crate::Error> {
4849
use crate::{cache::models::Question, Cache};
4950
use std::fs::File;
5051
use std::io::Write;
@@ -66,7 +67,7 @@ impl Command for EditCommand {
6667
if !Path::new(&path).exists() {
6768
let mut qr = serde_json::from_str(&target.desc);
6869
if qr.is_err() {
69-
qr = Ok(runtime.block_on(cache.get_question(id))?);
70+
qr = Ok(cache.get_question(id).await?);
7071
}
7172

7273
let question: Question = qr?;

‎src/cmds/exec.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Exec command
22
use super::Command;
3+
use async_trait::async_trait;
34
use clap::{App, ArgMatches};
4-
use tokio::runtime::Runtime;
55

66
/// Abstract Exec Command
77
///
@@ -21,6 +21,7 @@ use tokio::runtime::Runtime;
2121
/// ```
2222
pub struct ExecCommand;
2323

24+
#[async_trait]
2425
impl Command for ExecCommand {
2526
/// `exec` usage
2627
fn usage<'a, 'edit>() -> App<'a, 'edit> {
@@ -37,12 +38,12 @@ impl Command for ExecCommand {
3738
}
3839

3940
/// `exec` handler
40-
fn handler(m: &ArgMatches,runtime:&mutRuntime) -> Result<(), crate::Error> {
41+
asyncfn handler(m: &ArgMatches<'_>) -> Result<(), crate::Error> {
4142
use crate::cache::{Cache, Run};
4243

4344
let id: i32 = m.value_of("id")?.parse()?;
4445
let cache = Cache::new()?;
45-
let res = runtime.block_on(cache.exec_problem(id, Run::Submit, None))?;
46+
let res = cache.exec_problem(id, Run::Submit, None).await?;
4647

4748
println!("{}", res);
4849
Ok(())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /