Skip to content
Next Next commit
feat(config): add module config instead of cfg.rs
  • Loading branch information
clearloop committed Aug 14, 2022
commit 1918ae435ac0d6221fed23ec97141a20a6b488e8
4 changes: 2 additions & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use self::models::*;
use self::schemas::{problems::dsl::*, tags::dsl::*};
use self::sql::*;
use crate::helper::test_cases_path;
use crate::{cfg, err::Error, plugins::LeetCode};
use crate::{config, err::Error, plugins::LeetCode};
use colored::Colorize;
use diesel::prelude::*;
use reqwest::Response;
Expand Down Expand Up @@ -395,7 +395,7 @@ impl Cache {

/// New cache
pub fn new() -> Result<Self, Error> {
let conf = cfg::locate()?;
let conf = config::locate()?;
let c = conn(conf.storage.cache()?);
diesel::sql_query(CREATE_PROBLEMS_IF_NOT_EXISTS).execute(&c)?;
diesel::sql_query(CREATE_TAGS_IF_NOT_EXISTS).execute(&c)?;
Expand Down
1 change: 1 addition & 0 deletions src/cache/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Problem {
_ => "Unknown",
}
}

pub fn desc_comment(&self, conf: &Config) -> String {
let mut res = String::new();
let comment_leading = &conf.code.comment_leading;
Expand Down
29 changes: 3 additions & 26 deletions src/cfg.rs → src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ categories = [
"shell"
]

langs = [
"bash",
"c",
"cpp",
"csharp",
"golang",
"java",
"javascript",
"kotlin",
"mysql",
"php",
"python",
"python3",
"ruby",
"rust",
"scala",
"swift"
]

[sys.urls]
base = "https://leetcode.com"
graphql = "https://leetcode.com/graphql"
Expand Down Expand Up @@ -74,8 +55,8 @@ session = ""
root = "~/.leetcode"
scripts = "scripts"
code = "code"
# absolutely path for the cache, other use root as parent dir
cache = "~/.cache/leetcode"
# Relative path for the cache, otherwise use root as parent dir
cache = "Problems"
"##;

/// Sync with `~/.leetcode/config.toml`
Expand Down Expand Up @@ -109,7 +90,6 @@ pub struct Cookies {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Sys {
pub categories: Vec<String>,
pub langs: Vec<String>,
pub urls: HashMap<String, String>,
}

Expand All @@ -131,10 +111,7 @@ pub struct Urls {
pub favorite_delete: String,
}

/// default editor and langs
///
/// + support editor: [emacs, vim]
/// + support langs: all in config
/// Code config
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Code {
pub editor: String,
Expand Down
2 changes: 1 addition & 1 deletion src/err.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Errors in leetcode-cli
use crate::cfg::{root, DEFAULT_CONFIG};
use crate::cmds::{Command, DataCommand};
use crate::config::{root, DEFAULT_CONFIG};
use colored::Colorize;
use std::fmt;

Expand Down
6 changes: 3 additions & 3 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod file {

/// Generate test cases path by fid
pub fn test_cases_path(problem: &Problem) -> Result<String, Error> {
let conf = crate::cfg::locate()?;
let conf = crate::config::locate()?;
let mut path = format!("{}/{}.tests.dat", conf.storage.code()?, conf.code.pick);

path = path.replace("${fid}", &problem.fid.to_string());
Expand All @@ -200,7 +200,7 @@ mod file {

/// Generate code path by fid
pub fn code_path(problem: &Problem, l: Option<String>) -> Result<String, Error> {
let conf = crate::cfg::locate()?;
let conf = crate::config::locate()?;
let mut lang = conf.code.lang;
if l.is_some() {
lang = l.ok_or(Error::NoneError)?;
Expand All @@ -223,7 +223,7 @@ mod file {
pub fn load_script(module: &str) -> Result<String, crate::Error> {
use std::fs::File;
use std::io::Read;
let conf = crate::cfg::locate()?;
let conf = crate::config::locate()?;
let mut script = "".to_string();
File::open(format!("{}/{}.py", conf.storage.scripts()?, module))?
.read_to_string(&mut script)?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ extern crate diesel;

// show docs
pub mod cache;
pub mod cfg;
pub mod cli;
pub mod cmds;
pub mod config;
pub mod err;
pub mod flag;
pub mod helper;
Expand All @@ -247,5 +247,5 @@ pub mod pym;

// re-exports
pub use cache::Cache;
pub use cfg::Config;
pub use config::Config;
pub use err::Error;
2 changes: 1 addition & 1 deletion src/plugins/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl std::string::ToString for Ident {

/// Get cookies from chrome storage
pub fn cookies() -> Result<Ident, crate::Error> {
let ccfg = crate::cfg::locate()?.cookies;
let ccfg = crate::config::locate()?.cookies;
if !ccfg.csrf.is_empty() && !ccfg.session.is_empty() {
return Ok(Ident {
csrf: ccfg.csrf,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/leetcode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use self::req::{Json, Mode, Req};
use crate::{
cfg::{self, Config},
config::{self, Config},
err::Error,
plugins::chrome,
};
Expand Down Expand Up @@ -36,7 +36,7 @@ impl LeetCode {

/// New LeetCode client
pub fn new() -> Result<LeetCode, crate::Error> {
let conf = cfg::locate()?;
let conf = config::locate()?;
let cookies = chrome::cookies()?;
let default_headers = LeetCode::headers(
HeaderMap::new(),
Expand Down