Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

patches for #60 and #56 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 20, 2022
11 changes: 10 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ impl Cache {
.json()
.await?;
debug!("{:#?}", &json);
parser::desc(&mut rdesc, json).ok_or(Error::NoneError)?;
match parser::desc(&mut rdesc, json) {
None => return Err(Error::NoneError),
Some(false) => return
if self.is_session_bad().await {
Err(Error::CookieError)
} else {
Err(Error::PremiumError)
},
Some(true) => ()
}

// update the question
let sdesc = serde_json::to_string(&rdesc)?;
Expand Down
12 changes: 10 additions & 2 deletions src/cache/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {
}

/// desc parser
pub fn desc(q: &mut Question, v: Value) -> Option<()> {
pub fn desc(q: &mut Question, v: Value) -> Option<bool> {
/* None - parsing failed
* Some(false) - content was null (premium?)
* Some(true) - content was parsed
*/
let o = &v
.as_object()?
.get("data")?
.as_object()?
.get("question")?
.as_object()?;

if *o.get("content")? == Value::Null {
return Some(false);
}

*q = Question {
content: o.get("content")?.as_str().unwrap_or("").to_string(),
stats: serde_json::from_str(o.get("stats")?.as_str()?).ok()?,
Expand All @@ -55,7 +63,7 @@ pub fn desc(q: &mut Question, v: Value) -> Option<()> {
.to_string(),
};

Some(())
Some(true)
}

/// tag parser
Expand Down
8 changes: 8 additions & 0 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum Error {
FeatureError(String),
ScriptError(String),
CookieError,
PremiumError,
DecryptError,
SilentError,
NoneError,
Expand All @@ -37,6 +38,13 @@ impl std::fmt::Debug for Error {
.yellow()
.bold(),
),
Error::PremiumError => write!(
f,
"{} \
Your leetcode account lacks a premium subscription, which the given problem requires.\n \
If this looks like a mistake, please open a new issue at: {}",
e,
"https://github.com/clearloop/leetcode-cli/".underline()),
Error::DownloadError(s) => write!(f, "{} Download {} failed, please try again", e, s),
Error::NetworkError(s) => write!(f, "{} {}, please try again", e, s),
Error::ParseError(s) => write!(f, "{} {}", e, s),
Expand Down
6 changes: 5 additions & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ mod html {
impl HTML for String {
fn ser(&self) -> Vec<Token> {
// empty tags
let tks = self.to_string();
let tks = {
let mut s = self.clone();
// some problems (e.g. 1653) have ZWSPs.
s.retain(|x| x != '\u{200B}');
s };
let res: Vec<Token>;
// styled
{
Expand Down