Skip to content

Commit f5e602b

Browse files
authored
refactor(config): make config more simple (#88)
* feat(config): add module config instead of cfg.rs * feat(config): refactor sys * chore(config): wrap util functions in to Config * feat(config): add default values for config * feat(config): make config more simple * docs(README): add telegram link * chore(lock): udpate cargo.lock * fix(config): path of cache * chore(plugin): disable chrome plugin * docs(README): update config example * feat(config): hide submission and pick * chore(husky): remove cargo-husky
1 parent ca4fe98 commit f5e602b

File tree

18 files changed

+441
-374
lines changed

18 files changed

+441
-374
lines changed

.cargo-husky/hooks/pre-commit

Lines changed: 0 additions & 11 deletions
This file was deleted.

Cargo.lock

Lines changed: 41 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

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

55
[package]
66
name = "leetcode-cli"
7-
version = "0.3.13"
8-
authors = ["clearloop <cdr.today@foxmail.com>"]
7+
version = "0.4.0"
8+
authors = ["clearloop <tianyi.gc@gmail.com>"]
99
edition = "2021"
1010
description = "Leet your code in command-line."
1111
repository = "https://github.com/clearloop/leetcode-cli"
@@ -32,6 +32,7 @@ serde_json = "1.0.82"
3232
toml = "0.5.9"
3333
regex = "1.6.0"
3434
scraper = "0.13.0"
35+
anyhow = "1.0.71"
3536

3637
[dependencies.diesel]
3738
version = "2.0.3"
@@ -41,11 +42,6 @@ features = ["sqlite"]
4142
version = "0.11.11"
4243
features = ["gzip", "json"]
4344

44-
[dev-dependencies.cargo-husky]
45-
version = "1.5.0"
46-
default-features = false
47-
features = ["precommit-hook", "user-hooks"]
48-
4945
[features]
5046
pym = ["pyo3"]
5147

README.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# leetcode-cli
2-
![Rust](https://github.com/clearloop/leetcode-cli/workflows/Rust/badge.svg)
2+
3+
![Rust](https://github.com/clearloop/leetcode-cli/workflows/leetcode-cli/badge.svg)
34
[![crate](https://img.shields.io/crates/v/leetcode-cli.svg)](https://crates.io/crates/leetcode-cli)
45
[![doc](https://img.shields.io/badge/current-docs-brightgreen.svg)](https://docs.rs/leetcode-cli/)
56
[![downloads](https://img.shields.io/crates/d/leetcode-cli.svg)](https://crates.io/crates/leetcode-cli)
6-
[![gitter](https://img.shields.io/gitter/room/odditypark/leetcode-cli)](https://gitter.im/Odditypark/leetcode-cli)
7+
[![telegram](https://img.shields.io/badge/telegram-blue?logo=telegram)](https://t.me/+U_5si6PhWykxZTI1)
78
[![LICENSE](https://img.shields.io/crates/l/leetcode-cli.svg)](https://choosealicense.com/licenses/mit/)
89

910
## Installing
1011

1112
```sh
1213
# Required dependencies:
13-
#
14+
#
1415
# gcc
1516
# libssl-dev
1617
# libdbus-1-dev
@@ -48,14 +49,24 @@ SUBCOMMANDS:
4849

4950
## Example
5051

51-
For example, given this config (can be found in `~/.leetcode/leetcode.toml`, it can be generated automatically with command: `leetcode list` if you are a new user):
52+
For example, given this config (could be found at `~/.leetcode/leetcode.toml`):
5253

5354
```toml
5455
[code]
55-
lang = "rust"
56-
editor = "emacs"
56+
editor = emacs
5757
# Optional parameter
58-
editor_args = ['-nw']
58+
editor-args = ['-nw']
59+
lang = 'rust'
60+
61+
[cookies]
62+
csrf = '<your-leetcode-csrf-token>'
63+
session = '<your-leetcode-session-key>'
64+
65+
[storage]
66+
cache = 'Problems'
67+
code = 'code'
68+
root = '~/.leetcode'
69+
scripts = 'scripts'
5970
```
6071

6172
#### 1. <kbd>pick</kbd>
@@ -144,7 +155,8 @@ leetcode exec 1
144155

145156
## Cookies
146157

147-
The cookie plugin of leetcode-cli can work on OSX and [Linux][#1]. **If you are on a different platform, there are problems with caching the cookies**, you can manually input your LeetCode Cookies to the configuration file.
158+
The cookie plugin of leetcode-cli can work on OSX and [Linux][#1]. **If you are on a different platform, there are problems with caching the cookies**,
159+
you can manually input your LeetCode Cookies to the configuration file.
148160

149161
```toml
150162
[cookies]
@@ -154,7 +166,6 @@ session = "..."
154166

155167
For Example, using Chrome (after logging in to LeetCode):
156168

157-
158169
#### Step 1
159170

160171
Open Chrome and navigate to the link below:
@@ -166,6 +177,7 @@ chrome://settings/cookies/detail?site=leetcode.com
166177
#### Step 2
167178

168179
Copy `Content` from `LEETCODE_SESSION` and `csrftoken` to `session` and `csrf` in your configuration file, respectively:
180+
169181
```toml
170182
[cookies]
171183
csrf = "${csrftoken}"
@@ -189,13 +201,13 @@ import json;
189201

190202
def plan(sps, stags):
191203
##
192-
# `print` in python is supported,
193-
# if you want to know the data structures of these two args,
204+
# `print` in python is supported,
205+
# if you want to know the data structures of these two args,
194206
# just print them
195207
##
196208
problems = json.loads(sps)
197209
tags = json.loads(stags)
198-
210+
199211
ret = []
200212
tm = {}
201213
for tag in tags:
@@ -215,17 +227,15 @@ Then run `list` with the filter that you just wrote:
215227
leetcode list -p plan1
216228
```
217229

218-
And that's it! Enjoy!
230+
That's it! Enjoy!
219231

232+
## Contributions
220233

221-
## PR
222-
223-
[PRs][pr] are more than welcome!
234+
Feel free to add your names and emails in the `authors` field of `Cargo.toml` !
224235

225236
## LICENSE
226237

227238
MIT
228239

229-
230240
[pr]: https://github.com/clearloop/leetcode-cli/pulls
231241
[#1]: https://github.com/clearloop/leetcode-cli/issues/1

src/cache/mod.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use self::models::*;
77
use self::schemas::{problems::dsl::*, tags::dsl::*};
88
use self::sql::*;
99
use crate::helper::test_cases_path;
10-
use crate::{cfg, err::Error, plugins::LeetCode};
10+
use crate::{config::Config, err::Error, plugins::LeetCode};
11+
use anyhow::anyhow;
1112
use colored::Colorize;
1213
use diesel::prelude::*;
1314
use reqwest::Response;
@@ -309,33 +310,14 @@ impl Cache {
309310
json.insert("data_input", test_case);
310311

311312
let url = match run {
312-
Run::Test => conf
313-
.sys
314-
.urls
315-
.get("test")
316-
.ok_or(Error::NoneError)?
317-
.replace("$slug", &p.slug),
313+
Run::Test => conf.sys.urls.test(&p.slug),
318314
Run::Submit => {
319315
json.insert("judge_type", "large".to_string());
320-
conf.sys
321-
.urls
322-
.get("submit")
323-
.ok_or(Error::NoneError)?
324-
.replace("$slug", &p.slug)
316+
conf.sys.urls.submit(&p.slug)
325317
}
326318
};
327319

328-
Ok((
329-
json,
330-
[
331-
url,
332-
conf.sys
333-
.urls
334-
.get("problems")
335-
.ok_or(Error::NoneError)?
336-
.replace("$slug", &p.slug),
337-
],
338-
))
320+
Ok((json, [url, conf.sys.urls.problem(&p.slug)]))
339321
}
340322

341323
/// TODO: The real delay
@@ -361,15 +343,20 @@ impl Cache {
361343
) -> Result<VerifyResult, Error> {
362344
trace!("Exec problem filter —— Test or Submit");
363345
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
364-
trace!("Pre run code result {:?}, {:?}, {:?}", json, url, refer);
346+
trace!("Pre run code result {:#?}, {}, {}", json, url, refer);
365347

366-
let run_res: RunCode = self
348+
let text = self
367349
.0
368350
.clone()
369351
.run_code(json.clone(), url.clone(), refer.clone())
370352
.await?
371-
.json() // does not require LEETCODE_SESSION (very oddly)
353+
.text()
372354
.await?;
355+
356+
let run_res: RunCode = serde_json::from_str(&text).map_err(|e| {
357+
anyhow!("json error: {e}, plz double check your session and csrf config.")
358+
})?;
359+
373360
trace!("Run code result {:#?}", run_res);
374361

375362
// Check if leetcode accepted the Run request
@@ -397,7 +384,7 @@ impl Cache {
397384

398385
/// New cache
399386
pub fn new() -> Result<Self, Error> {
400-
let conf = cfg::locate()?;
387+
let conf = Config::locate()?;
401388
let mut c = conn(conf.storage.cache()?);
402389
diesel::sql_query(CREATE_PROBLEMS_IF_NOT_EXISTS).execute(&mut c)?;
403390
diesel::sql_query(CREATE_TAGS_IF_NOT_EXISTS).execute(&mut c)?;

src/cache/models.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl Problem {
3939
_ => "Unknown",
4040
}
4141
}
42+
4243
pub fn desc_comment(&self, conf: &Config) -> String {
4344
let mut res = String::new();
4445
let comment_leading = &conf.code.comment_leading;

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