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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::fmt;
use std::str::FromStr;

use super::content_parser::*;
use super::request::*;

use reqwest::blocking::Response;
use scraper::Html;
use serde::Serialize;
use tinytemplate::TinyTemplate;

const LC_P: &str = "https://leetcode.com/problems/";

#[derive(Debug, Clone)]
pub struct Quiz {
    title: String,
    level: Level,
    source_link: String,
    content: serde_json::Value,
}

#[derive(Debug, Eq, PartialEq, Serialize, Clone)]
pub enum Level {
    Easy,
    Medium,
    Hard,
}

#[derive(Serialize)]
struct FmtTemplate {
    level: Level,
    source: String,
    title: String,
    content: String,

    #[serde(rename(serialize = "code"))]
    code_snippet: String,
}

impl FromStr for Level {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Easy" | "easy" | "e" => Ok(Self::Easy),
            "Medium" | "medium" | "m" => Ok(Self::Medium),
            "Hard" | "hard" | "h" => Ok(Self::Hard),
            _ => Err("Unspport difficulty".to_string()),
        }
    }
}

#[cfg(feature = "emacs")]
impl emacs::FromLisp<'_> for Level {
    fn from_lisp(value: emacs::Value<'_>) -> emacs::Result<Self> {
        Ok(Level::from_str(&String::from_lisp(value).unwrap()).unwrap())
    }
}

impl Quiz {
    pub(super) fn from_resp(resp: Response, source_link: String) -> Result<Self, String> {
        let content = graphql_response_parse(resp)?;
        Ok(Quiz {
            title: find_question_title_from_graphql_req(&content)?,
            level: Level::from_str(&find_question_level_from_graphql_req(&content)?)?,
            source_link,
            content,
        })
    }

    pub fn get_by_name(name: &str) -> Result<Self, String> {
        get_quiz_by_url(&(LC_P.to_string() + name + "/"))
    }

    /// get quiz randomly
    pub fn get_randomly(level: Option<Level>) -> Result<Self, String> {
        get_random_quiz(level)
    }

    /// get quiz id
    pub fn get_by_id(id: u64) -> Result<Self, String> {
        get_quiz_by_id(id)
    }

    pub fn quiz_id(&self) -> Result<String, String> {
        find_question_id_from_graphql_req(&self.content)
    }

    pub fn quiz_source(&self) -> &str {
        &self.source_link
    }

    pub fn quiz_pure_description(&self) -> Result<String, String> {
        let s = find_question_content(&self.content)?;
        // some quizzes haven't start with <p>, have to add by myself
        if s.starts_with("<p>") {
            Ok(s.to_string())
        } else {
            Ok("<p>".to_string() + s + "</p>")
        }
    }

    /// Get markdown description of quiz
    pub fn quiz_description(&self) -> Result<String, String> {
        let fragment = Html::parse_fragment(&self.quiz_pure_description()?);
        Ok(description_markdown(description_in_graphql(&fragment)).join(""))
    }

    pub fn quiz_level(&self) -> &Level {
        &self.level
    }

    /// Get code snippet of special language
    /// if None, give first one
    pub fn code_snippet(&self, lang: &str) -> Option<&str> {
        match find_code_snippet(&self.content, lang) {
            Ok(d) => d,
            Err(e) => {
                println!("{}", e.to_string());
                None
            }
        }
    }

    /// Parse content in template
    pub fn use_fmt_temp(
        &self,
        temp: Option<String>,
        code_lang: &Option<String>,
    ) -> Result<String, String> {
        match temp {
            Some(s) => {
                // make template
                let mut tt = TinyTemplate::new();
                tt.add_template("quiz", &s).map_err(|e| e.to_string())?;

                tt.render(
                    "quiz",
                    &FmtTemplate {
                        level: self.level.clone(),
                        source: self.source_link.clone(),
                        title: self.title.clone(),
                        content: self.quiz_description()?,
                        code_snippet: self
                            .code_snippet(code_lang.as_ref().unwrap_or(&"rust".to_string()))
                            .unwrap()
                            .to_string(),
                    },
                )
                .map_err(|e| e.to_string())
            }
            None => Ok(format!("{}", self)), //default format
        }
    }
}

impl fmt::Display for Quiz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}: {}\n\n{}\n",
            self.quiz_id().unwrap(),
            self.title,
            self.quiz_description().unwrap(),
        )
    }
}

#[cfg(test)]
impl Quiz {
    fn new() -> Self {
        Self {
            title: String::new(),
            level: Level::Easy,
            source_link: String::new(),
            content: serde_json::Value::Null,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fmt_temp() {
        let s = "README {source}, {title}; {content}..{level}ll{code}";

        // make template
        let mut tt = TinyTemplate::new();
        tt.add_template("quiz", s)
            .map_err(|e| e.to_string())
            .unwrap();

        let result = tt
            .render(
                "quiz",
                &FmtTemplate {
                    level: Level::Easy,
                    source: "linklink".to_string(),
                    title: "titititle".to_string(),
                    content: "main content".to_string(),
                    code_snippet: "codecode".to_string(),
                },
            )
            .unwrap();

        assert_eq!(
            result,
            "README linklink, titititle; main content..Easyllcodecode"
        );

        // test \n
        let s = "README {source}\n\n, {title}; {content}..{level}ll{code}";
        tt.add_template("quiz", s)
            .map_err(|e| e.to_string())
            .unwrap();

        let result = tt
            .render(
                "quiz",
                &FmtTemplate {
                    level: Level::Easy,
                    source: "linklink".to_string(),
                    title: "titititle".to_string(),
                    content: "main content".to_string(),
                    code_snippet: "codecode".to_string(),
                },
            )
            .unwrap();

        assert_eq!(
            result,
            "README linklink

, titititle; main content..Easyllcodecode"
        );
        println!("{}", result);
    }
}