Skip to content

Commit 146cd25

Browse files
authored
feat(err): wrap NoneError into Error (#51)
* feat(err): use NoneError instead of feature try_traits and remove unused fields * chore(clippy): make clippy happy * chore(ci): use latest nightly toolchain * chore(ci): remove all-features testing in CI * chore(ci): use nightly toolchain in CI * chore(ci): switch stable toolchain * ci(toolchain): switch stable * bump: bumps version to v0.3.10
1 parent 3fabbde commit 146cd25

File tree

21 files changed

+113
-154
lines changed

21 files changed

+113
-154
lines changed

.github/workflows/clippy.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ jobs:
1616
- uses: actions/checkout@v1
1717
- uses: actions-rs/toolchain@v1
1818
with:
19-
toolchain: nightly-2021-05-16
2019
components: clippy
21-
override: true
20+
toolchain: stable
2221
- uses: actions-rs/clippy-check@v1
2322
with:
2423
token: ${{ secrets.GITHUB_TOKEN }}
25-
args: --all-features

.github/workflows/rust.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ jobs:
1818
- name: Set nightly toolchain
1919
uses: actions-rs/toolchain@v1
2020
with:
21-
toolchain: nightly-2021-05-16
22-
override: true
21+
toolchain: stable
2322
- name: Environment
2423
run: |
2524
if [[ "$(uname)" == 'Darwin' ]]; then
@@ -30,6 +29,6 @@ jobs:
3029
sudo apt-get install -y libsqlite3-dev libdbus-1-dev
3130
fi
3231
- name: Build
33-
run: cargo build --all-features -vv
32+
run: cargo build
3433
- name: Run tests
35-
run: cargo test --all-features -vv
34+
run: cargo test

Cargo.toml

Lines changed: 1 addition & 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.3.9"
7+
version = "0.3.10"
88
authors = ["clearloop <cdr.today@foxmail.com>"]
99
edition = "2018"
1010
description = "Leet your code in command-line."

README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
[![gitter](https://img.shields.io/gitter/room/odditypark/leetcode-cli)](https://gitter.im/Odditypark/leetcode-cli)
77
[![LICENSE](https://img.shields.io/crates/l/leetcode-cli.svg)](https://choosealicense.com/licenses/mit/)
88

9-
## Features
10-
11-
+ [x] the edit flow —— solution files will generate automatically!
12-
+ [x] support Python script to filter questions
13-
+ [ ] doc support, `lc-rs` can compile the annotation of your solutions to Markdown!
14-
+ [ ] support local signal to keep coding as longer as you want
15-
169
## Installing
1710

1811
```sh
@@ -26,21 +19,12 @@
2619
cargo install leetcode-cli
2720
```
2821

29-
### `error[E0554]`
30-
31-
If this happens when compiling the program, it means that the package cannot be compiled with stable Rust. To fix this, install Rust Nightly and try the following:
32-
33-
```sh
34-
rustup install nightly
35-
cargo +nightly install leetcode-cli
36-
```
37-
3822
## Usage
3923

4024
**Make sure you have logged in to `leetcode.com` with `Chrome`**. See [Cookies](#cookies) for why you need to do this first.
4125

4226
```sh
43-
leetcode 0.3.9
27+
leetcode 0.3.10
4428
May the Code be with You 👻
4529

4630
USAGE:

rust-toolchain

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/cache/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ impl Cache {
6767
let json = self
6868
.0
6969
.clone()
70-
.get_category_problems(&i)
70+
.get_category_problems(i)
7171
.await?
7272
.json()
7373
.await?;
74-
parser::problem(&mut ps, json)?;
74+
parser::problem(&mut ps, json).ok_or(Error::NoneError)?;
7575
}
7676

7777
diesel::replace_into(problems)
@@ -135,7 +135,7 @@ impl Cache {
135135
.json()
136136
.await?;
137137
debug!("{:#?}", &json);
138-
parser::desc(&mut rdesc, json)?;
138+
parser::desc(&mut rdesc, json).ok_or(Error::NoneError)?;
139139

140140
// update the question
141141
let sdesc = serde_json::to_string(&rdesc)?;
@@ -160,11 +160,11 @@ impl Cache {
160160
ids = parser::tags(
161161
self.clone()
162162
.0
163-
.get_question_ids_by_tag(&rslug)
163+
.get_question_ids_by_tag(rslug)
164164
.await?
165165
.json()
166166
.await?,
167-
)?;
167+
).ok_or(Error::NoneError)?;
168168
let t = Tag {
169169
r#tag: rslug.to_string(),
170170
r#refs: serde_json::to_string(&ids)?,
@@ -235,18 +235,18 @@ impl Cache {
235235
json.insert("data_input", testcase);
236236

237237
let url = match run {
238-
Run::Test => conf.sys.urls.get("test")?.replace("$slug", &p.slug),
238+
Run::Test => conf.sys.urls.get("test").ok_or(Error::NoneError)?.replace("$slug", &p.slug),
239239
Run::Submit => {
240240
json.insert("judge_type", "large".to_string());
241-
conf.sys.urls.get("submit")?.replace("$slug", &p.slug)
241+
conf.sys.urls.get("submit").ok_or(Error::NoneError)?.replace("$slug", &p.slug)
242242
}
243243
};
244244

245245
Ok((
246246
json,
247247
[
248248
url,
249-
conf.sys.urls.get("problems")?.replace("$slug", &p.slug),
249+
conf.sys.urls.get("problems").ok_or(Error::NoneError)?.replace("$slug", &p.slug),
250250
],
251251
))
252252
}
@@ -308,8 +308,8 @@ impl Cache {
308308
}
309309
trace!("Recur verify result {:#?}", res);
310310

311-
res.name = json.get("name")?.to_string();
312-
res.data_input = json.get("data_input")?.to_string();
311+
res.name = json.get("name").ok_or(Error::NoneError)?.to_string();
312+
res.data_input = json.get("data_input").ok_or(Error::NoneError)?.to_string();
313313
res.result_type = run;
314314
Ok(res)
315315
}

src/cache/models.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ pub struct VerifyResult {
209209
pub data_input: String,
210210
#[serde(skip)]
211211
pub result_type: Run,
212-
#[serde(default)]
213-
lang: String,
212+
// #[serde(default)]
213+
// lang: String,
214214
#[serde(default)]
215215
pretty_lang: String,
216-
#[serde(default)]
217-
submission_id: String,
218-
#[serde(default)]
219-
run_success: bool,
216+
// #[serde(default)]
217+
// submission_id: String,
218+
// #[serde(default)]
219+
// run_success: bool,
220220
#[serde(default)]
221221
correct_answer: bool,
222222
#[serde(default, deserialize_with = "ssr")]
@@ -229,8 +229,8 @@ pub struct VerifyResult {
229229
std_output: String,
230230

231231
// flatten
232-
#[serde(flatten, default)]
233-
info: VerifyInfo,
232+
// #[serde(flatten, default)]
233+
// info: VerifyInfo,
234234
#[serde(flatten, default)]
235235
status: VerifyStatus,
236236
#[serde(flatten, default)]
@@ -462,15 +462,15 @@ mod verify {
462462
pub compare_result: String,
463463
}
464464

465-
#[derive(Debug, Default, Deserialize)]
466-
pub struct VerifyInfo {
467-
#[serde(default)]
468-
memory: i64,
469-
#[serde(default)]
470-
elapsed_time: i64,
471-
#[serde(default)]
472-
task_finish_time: i64,
473-
}
465+
// #[derive(Debug, Default, Deserialize)]
466+
// pub struct VerifyInfo {
467+
// #[serde(default)]
468+
// memory: i64,
469+
// #[serde(default)]
470+
// elapsed_time: i64,
471+
// #[serde(default)]
472+
// task_finish_time: i64,
473+
// }
474474

475475
#[derive(Debug, Default, Deserialize)]
476476
pub struct Analyse {
@@ -500,30 +500,30 @@ mod verify {
500500

501501
#[derive(Debug, Default, Deserialize)]
502502
pub struct CompileError {
503-
#[serde(default)]
504-
compile_error: String,
503+
// #[serde(default)]
504+
// compile_error: String,
505505
#[serde(default)]
506506
pub full_compile_error: String,
507507
}
508508

509509
#[derive(Debug, Default, Deserialize)]
510510
pub struct Expected {
511-
#[serde(default)]
512-
expected_status_code: i32,
513-
#[serde(default)]
514-
expected_lang: String,
515-
#[serde(default)]
516-
expected_run_success: bool,
517-
#[serde(default)]
518-
expected_status_runtime: String,
519-
#[serde(default)]
520-
expected_memory: i64,
521-
#[serde(default, deserialize_with = "ssr")]
522-
expected_code_output: Vec<String>,
523-
#[serde(default)]
524-
expected_elapsed_time: i64,
525-
#[serde(default)]
526-
expected_task_finish_time: i64,
511+
// #[serde(default)]
512+
// expected_status_code: i32,
513+
// #[serde(default)]
514+
// expected_lang: String,
515+
// #[serde(default)]
516+
// expected_run_success: bool,
517+
// #[serde(default)]
518+
// expected_status_runtime: String,
519+
// #[serde(default)]
520+
// expected_memory: i64,
521+
// #[serde(default, deserialize_with = "ssr")]
522+
// expected_code_output: Vec<String>,
523+
// #[serde(default)]
524+
// expected_elapsed_time: i64,
525+
// #[serde(default)]
526+
// expected_task_finish_time: i64,
527527
#[serde(default, deserialize_with = "ssr")]
528528
pub expected_code_answer: Vec<String>,
529529
}

src/cache/parser.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! Sub-Module for parsing resp data
22
use super::models::*;
3-
use crate::err::Error;
43
use serde_json::Value;
54

65
/// problem parser
7-
pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Result<(), Error> {
6+
pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {
87
let pairs = v.get("stat_status_pairs")?.as_array()?;
98
for p in pairs {
109
let stat = p.get("stat")?.as_object()?;
@@ -26,11 +25,11 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Result<(), Error> {
2625
});
2726
}
2827

29-
Ok(())
28+
Some(())
3029
}
3130

3231
/// desc parser
33-
pub fn desc(q: &mut Question, v: Value) -> Result<(), Error> {
32+
pub fn desc(q: &mut Question, v: Value) -> Option<()> {
3433
let o = &v
3534
.as_object()?
3635
.get("data")?
@@ -40,14 +39,14 @@ pub fn desc(q: &mut Question, v: Value) -> Result<(), Error> {
4039

4140
*q = Question {
4241
content: o.get("content")?.as_str().unwrap_or("").to_string(),
43-
stats: serde_json::from_str(o.get("stats")?.as_str()?)?,
44-
defs: serde_json::from_str(o.get("codeDefinition")?.as_str()?)?,
42+
stats: serde_json::from_str(o.get("stats")?.as_str()?).ok()?,
43+
defs: serde_json::from_str(o.get("codeDefinition")?.as_str()?).ok()?,
4544
case: o.get("sampleTestCase")?.as_str()?.to_string(),
4645
all_cases: o.get("exampleTestcases")
4746
.unwrap_or(o.get("sampleTestCase")?) // soft fail to the sampleTestCase
4847
.as_str()?
4948
.to_string(),
50-
metadata: serde_json::from_str(o.get("metaData")?.as_str()?)?,
49+
metadata: serde_json::from_str(o.get("metaData")?.as_str()?).ok()?,
5150
test: o.get("enableRunCode")?.as_bool()?,
5251
t_content: o
5352
.get("translatedContent")?
@@ -56,16 +55,16 @@ pub fn desc(q: &mut Question, v: Value) -> Result<(), Error> {
5655
.to_string(),
5756
};
5857

59-
Ok(())
58+
Some(())
6059
}
6160

6261
/// tag parser
63-
pub fn tags(v: Value) -> Result<Vec<String>, Error> {
62+
pub fn tags(v: Value) -> Option<Vec<String>> {
6463
trace!("Parse tags...");
6564
let tag = v.as_object()?.get("data")?.as_object()?.get("topicTag")?;
6665

6766
if tag.is_null() {
68-
return Ok(vec![]);
67+
return Some(vec![]);
6968
}
7069

7170
let arr = tag.as_object()?.get("questions")?.as_array()?;
@@ -75,7 +74,7 @@ pub fn tags(v: Value) -> Result<Vec<String>, Error> {
7574
res.push(q.as_object()?.get("questionId")?.as_str()?.to_string())
7675
}
7776

78-
Ok(res)
77+
Some(res)
7978
}
8079

8180
pub use ss::ssr;

src/cfg.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! + Use `leetcode config` to update it
88
use serde::{Deserialize, Serialize};
99
use std::{collections::HashMap, fs, path::PathBuf};
10+
use crate::Error;
1011

1112
const DEFAULT_CONFIG: &str = r#"
1213
# usually you don't wanna change those
@@ -81,8 +82,8 @@ pub struct Config {
8182

8283
impl Config {
8384
/// Sync new config to config.toml
84-
pub fn sync(&self) -> Result<(), crate::Error> {
85-
let home = dirs::home_dir()?;
85+
pub fn sync(&self) -> Result<(), Error> {
86+
let home = dirs::home_dir().ok_or(Error::NoneError)?;
8687
let conf = home.join(".leetcode/leetcode.toml");
8788
fs::write(conf, toml::ser::to_string_pretty(&self)?)?;
8889

@@ -148,8 +149,8 @@ pub struct Storage {
148149

149150
impl Storage {
150151
/// convert root path
151-
pub fn root(&self) -> Result<String, crate::Error> {
152-
let home = dirs::home_dir()?.to_string_lossy().to_string();
152+
pub fn root(&self) -> Result<String, Error> {
153+
let home = dirs::home_dir().ok_or(Error::NoneError)?.to_string_lossy().to_string();
153154
let path = self.root.replace("~", &home);
154155
Ok(path)
155156
}
@@ -178,11 +179,11 @@ impl Storage {
178179
pub fn scripts(mut self) -> Result<String, crate::Error> {
179180
let root = &self.root()?;
180181
if self.scripts.is_none() {
181-
let tmp = toml::from_str::<Config>(&DEFAULT_CONFIG)?;
182-
self.scripts = Some(tmp.storage.scripts?);
182+
let tmp = toml::from_str::<Config>(DEFAULT_CONFIG)?;
183+
self.scripts = Some(tmp.storage.scripts.ok_or(Error::NoneError)?);
183184
}
184185

185-
let p = PathBuf::from(root).join(&self.scripts?);
186+
let p = PathBuf::from(root).join(&self.scripts.ok_or(Error::NoneError)?);
186187
if !PathBuf::from(&p).exists() {
187188
std::fs::create_dir(&p)?
188189
}
@@ -203,8 +204,8 @@ pub fn locate() -> Result<Config, crate::Error> {
203204
}
204205

205206
/// Get root path of leetcode-cli
206-
pub fn root() -> Result<std::path::PathBuf, crate::Error> {
207-
let dir = dirs::home_dir()?.join(".leetcode");
207+
pub fn root() -> Result<std::path::PathBuf, Error> {
208+
let dir = dirs::home_dir().ok_or(Error::NoneError)?.join(".leetcode");
208209
if !dir.is_dir() {
209210
info!("Generate root dir at {:?}.", &dir);
210211
fs::DirBuilder::new().recursive(true).create(&dir)?;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy