leetcode_cli/config/
sys.rs

1//! System section
2//!
3//! This section is a set of constants after #88
4
5use serde::{Deserialize, Serialize};
6
7const CATEGORIES: [&str; 4] = ["algorithms", "concurrency", "database", "shell"];
8
9// TODO: find a better solution.
10fn categories() -> Vec<String> {
11    CATEGORIES.into_iter().map(|s| s.into()).collect()
12}
13
14/// Leetcode API
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct Urls {
17    pub base: String,
18    pub graphql: String,
19    pub login: String,
20    pub problems: String,
21    pub problem: String,
22    pub tag: String,
23    pub test: String,
24    pub session: String,
25    pub submit: String,
26    pub submissions: String,
27    pub submission: String,
28    pub verify: String,
29    pub favorites: String,
30    pub favorite_delete: String,
31}
32
33impl Default for Urls {
34    fn default() -> Self {
35        Self {
36            base: "https://leetcode.com".into(),
37            graphql: "https://leetcode.com/graphql".into(),
38            login: "https://leetcode.com/accounts/login/".into(),
39            problems: "https://leetcode.com/api/problems/$category/".into(),
40            problem: "https://leetcode.com/problems/$slug/description/".into(),
41            tag: "https://leetcode.com/tag/$slug/".into(),
42            test: "https://leetcode.com/problems/$slug/interpret_solution/".into(),
43            session: "https://leetcode.com/session/".into(),
44            submit: "https://leetcode.com/problems/$slug/submit/".into(),
45            submissions: "https://leetcode.com/submissions/detail/$id/".into(),
46            submission: "https://leetcode.com/submissions/detail/$id/".into(),
47            verify: "https://leetcode.com/submissions/detail/$id/check/".into(),
48            favorites: "https://leetcode.com/list/api/questions".into(),
49            favorite_delete: "https://leetcode.com/list/api/questions/$hash/$id".into(),
50        }
51    }
52}
53
54impl Urls {
55    pub fn new_with_leetcode_cn() -> Self {
56        Self {
57            base: "https://leetcode.cn".into(),
58            graphql: "https://leetcode.cn/graphql".into(),
59            login: "https://leetcode.cn/accounts/login/".into(),
60            problems: "https://leetcode.cn/api/problems/$category/".into(),
61            problem: "https://leetcode.cn/problems/$slug/description/".into(),
62            tag: "https://leetcode.cn/tag/$slug/".into(),
63            test: "https://leetcode.cn/problems/$slug/interpret_solution/".into(),
64            session: "https://leetcode.cn/session/".into(),
65            submit: "https://leetcode.cn/problems/$slug/submit/".into(),
66            submissions: "https://leetcode.cn/submissions/detail/$id/".into(),
67            submission: "https://leetcode.cn/submissions/detail/$id/".into(),
68            verify: "https://leetcode.cn/submissions/detail/$id/check/".into(),
69            favorites: "https://leetcode.cn/list/api/questions".into(),
70            favorite_delete: "https://leetcode.cn/list/api/questions/$hash/$id".into(),
71        }
72    }
73
74    /// problem url with specific `$slug`
75    pub fn problem(&self, slug: &str) -> String {
76        self.problem.replace("$slug", slug)
77    }
78
79    /// problems url with specific `$category`
80    pub fn problems(&self, category: &str) -> String {
81        self.problems.replace("$category", category)
82    }
83
84    /// submit url with specific `$slug`
85    pub fn submit(&self, slug: &str) -> String {
86        self.submit.replace("$slug", slug)
87    }
88
89    /// tag url with specific `$slug`
90    pub fn tag(&self, slug: &str) -> String {
91        self.tag.replace("$slug", slug)
92    }
93
94    /// test url with specific `$slug`
95    pub fn test(&self, slug: &str) -> String {
96        self.test.replace("$slug", slug)
97    }
98
99    /// verify url with specific `$id`
100    pub fn verify(&self, id: &str) -> String {
101        self.verify.replace("$id", id)
102    }
103}
104
105/// System settings, for leetcode api mainly
106#[derive(Clone, Debug, Serialize, Deserialize)]
107pub struct Sys {
108    #[serde(default = "categories")]
109    pub categories: Vec<String>,
110    #[serde(default)]
111    pub urls: Urls,
112}
113
114impl Default for Sys {
115    fn default() -> Self {
116        Self {
117            categories: CATEGORIES.into_iter().map(|s| s.into()).collect(),
118            urls: Default::default(),
119        }
120    }
121}