1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Cookies in config
use serde::{Deserialize, Serialize};
use std::{
    fmt::{self, Display},
    str::FromStr,
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum LeetcodeSite {
    #[serde(rename = "leetcode.com")]
    LeetcodeCom,
    #[serde(rename = "leetcode.cn")]
    LeetcodeCn,
}

impl FromStr for LeetcodeSite {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "leetcode.com" => Ok(LeetcodeSite::LeetcodeCom),
            "leetcode.cn" => Ok(LeetcodeSite::LeetcodeCn),
            _ => Err("Invalid site key".to_string()),
        }
    }
}

impl Display for LeetcodeSite {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            LeetcodeSite::LeetcodeCom => "leetcode.com",
            LeetcodeSite::LeetcodeCn => "leetcode.cn",
        };

        write!(f, "{s}")
    }
}

/// Cookies settings
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Cookies {
    pub csrf: String,
    pub session: String,
    pub site: LeetcodeSite,
}

impl Default for Cookies {
    fn default() -> Self {
        Self {
            csrf: "".to_string(),
            session: "".to_string(),
            site: LeetcodeSite::LeetcodeCom,
        }
    }
}

impl Display for Cookies {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "LEETCODE_SESSION={};csrftoken={};",
            self.session, self.csrf
        )
    }
}