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 de33c69

Browse files
author
curtain
committed
feat: refactor config
1 parent 930064f commit de33c69

File tree

8 files changed

+213
-244
lines changed

8 files changed

+213
-244
lines changed

β€ŽCargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ async-trait = "0.1.56"
2020
tokio = { version = "1.19.2", features = ["full"] }
2121
clap = { version = "4", features = ["cargo"] }
2222
colored = "2.0.0"
23-
dirs = "4.0.0"
2423
env_logger = "0.9.0"
2524
keyring = "1.2.0"
2625
log = "0.4.17"
@@ -32,6 +31,7 @@ serde_json = "1.0.82"
3231
toml = "0.5.9"
3332
regex = "1.6.0"
3433
scraper = "0.13.0"
34+
etcetera = "0.4.0"
3535

3636
[dependencies.diesel]
3737
version = "1.4.8"

β€Žsrc/cache/mod.rs

Lines changed: 9 additions & 25 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;
11+
use crate::{err::Error, plugins::LeetCode};
1112
use colored::Colorize;
1213
use diesel::prelude::*;
1314
use reqwest::Response;
@@ -40,12 +41,12 @@ pub struct Cache(pub LeetCode);
4041
impl Cache {
4142
/// Ref to sqlite connection
4243
fn conn(&self) -> Result<SqliteConnection, Error> {
43-
Ok(conn(self.0.conf.storage.cache()?))
44+
Ok(conn(Config::problems_filepath()?))
4445
}
4546

4647
/// Clean cache
4748
pub fn clean(&self) -> Result<(), Error> {
48-
Ok(std::fs::remove_file(&self.0.conf.storage.cache()?)?)
49+
Ok(std::fs::remove_file(Config::problems_filepath()?)?)
4950
}
5051

5152
/// ref to download probems
@@ -55,7 +56,7 @@ impl Cache {
5556
}
5657

5758
pub fn update_after_ac(self, rid: i32) -> Result<(), Error> {
58-
let c = conn(self.0.conf.storage.cache()?);
59+
let c = conn(Config::problems_filepath()?);
5960
let target = problems.filter(id.eq(rid));
6061
diesel::update(target).set(status.eq("ac")).execute(&c)?;
6162
Ok(())
@@ -307,32 +308,16 @@ impl Cache {
307308
json.insert("data_input", test_case);
308309

309310
let url = match run {
310-
Run::Test => conf
311-
.sys
312-
.urls
313-
.get("test")
314-
.ok_or(Error::NoneError)?
315-
.replace("$slug", &p.slug),
311+
Run::Test => conf.sys.urls.test.replace("$slug", &p.slug),
316312
Run::Submit => {
317313
json.insert("judge_type", "large".to_string());
318-
conf.sys
319-
.urls
320-
.get("submit")
321-
.ok_or(Error::NoneError)?
322-
.replace("$slug", &p.slug)
314+
conf.sys.urls.submit.replace("$slug", &p.slug)
323315
}
324316
};
325317

326318
Ok((
327319
json,
328-
[
329-
url,
330-
conf.sys
331-
.urls
332-
.get("problems")
333-
.ok_or(Error::NoneError)?
334-
.replace("$slug", &p.slug),
335-
],
320+
[url, conf.sys.urls.problems.replace("$slug", &p.slug)],
336321
))
337322
}
338323

@@ -395,8 +380,7 @@ impl Cache {
395380

396381
/// New cache
397382
pub fn new() -> Result<Self, Error> {
398-
let conf = cfg::locate()?;
399-
let c = conn(conf.storage.cache()?);
383+
let c = conn(Config::problems_filepath()?);
400384
diesel::sql_query(CREATE_PROBLEMS_IF_NOT_EXISTS).execute(&c)?;
401385
diesel::sql_query(CREATE_TAGS_IF_NOT_EXISTS).execute(&c)?;
402386

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /