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
use crate::err::Error;
use super::models::*;
use serde_json::Value;
pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Result<(), Error> {
let pairs = v.get("stat_status_pairs")?.as_array()?;
for p in pairs {
let stat = p.get("stat")?.as_object()?;
let total_acs = stat.get("total_acs")?.as_f64()? as f32;
let total_submitted = stat.get("total_submitted")?.as_f64()? as f32;
problems.push(Problem{
category: v.get("category_slug")?.as_str()?.to_string(),
fid: stat.get("frontend_question_id")?.as_i64()? as i32,
id: stat.get("question_id")?.as_i64()? as i32,
level: p.get("difficulty")?.as_object()?.get("level")?.as_i64()? as i32,
locked: p.get("paid_only")?.as_bool()?,
name: stat.get("question__title")?.as_str()?.to_string(),
percent: total_acs / total_submitted * 100.0,
slug: stat.get("question__title_slug")?.as_str()?.to_string(),
starred: p.get("is_favor")?.as_bool()?,
status: p.get("status")?.as_str().unwrap_or("Null").to_string(),
desc: String::new(),
});
}
return Ok(());
}
pub fn desc(q: &mut Question, v: Value) -> Result<(), Error> {
let o = &v.as_object()?.get("data")?.as_object()?.get("question")?.as_object()?;
*q = Question {
content: o.get("content")?.as_str().unwrap_or("").to_string(),
stats: serde_json::from_str(o.get("stats")?.as_str()?)?,
defs: serde_json::from_str(o.get("codeDefinition")?.as_str()?)?,
case: o.get("sampleTestCase")?.as_str()?.to_string(),
metadata: serde_json::from_str(o.get("metaData")?.as_str()?)?,
test: o.get("enableRunCode")?.as_bool()?,
t_content: o.get("translatedContent")?.as_str().unwrap_or("").to_string(),
};
Ok(())
}
pub use ss::ssr;
mod ss {
use std::fmt;
use std::marker::PhantomData;
use serde::{de, Deserialize, Deserializer};
pub fn ssr<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where D: Deserializer<'de>
{
struct StringOrVec(PhantomData<Vec<String>>);
impl<'de> de::Visitor<'de> for StringOrVec {
type Value = Vec<String>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("string or list of strings")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where E: de::Error
{
Ok(vec![value.to_owned()])
}
fn visit_seq<S>(self, visitor: S) -> Result<Self::Value, S::Error>
where S: de::SeqAccess<'de>
{
Deserialize::deserialize(de::value::SeqAccessDeserializer::new(visitor))
}
}
deserializer.deserialize_any(StringOrVec(PhantomData))
}
}