use anyhow::anyhow;
use colored::Colorize;
#[cfg(debug_assertions)]
const CONFIG: &str = "~/.leetcode/leetcode.tmp.toml";
#[cfg(not(debug_assertions))]
const CONFIG: &str = "~/.leetcode/leetcode_tmp.toml";
pub type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Nothing matched")]
MatchError,
#[error("Download {0} failed, please try again")]
DownloadError(String),
#[error(transparent)]
Reqwest(#[from] wreq::Error),
#[error(transparent)]
HeaderName(#[from] wreq::header::InvalidHeaderName),
#[error(transparent)]
HeaderValue(#[from] wreq::header::InvalidHeaderValue),
#[error(
"Your leetcode cookies seems expired, \
{} \
Either you can handwrite your `LEETCODE_SESSION` and `csrf` into `leetcode.toml`, \
more info please checkout this: \
https://github.com/siansiansu/leetctl/blob/main/docs/cookies.md",
"please make sure you have logined in leetcode.com with chrome. ".yellow().bold()
)]
CookieError,
#[error(
"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: {}",
"https://github.com/siansiansu/leetctl/".underline()
)]
PremiumError,
#[error(transparent)]
Utf8(#[from] std::string::FromUtf8Error),
#[error(
"json from response parse failed, please open a new issue at: {}.",
"https://github.com/siansiansu/leetctl/".underline()
)]
NoneError,
#[error(
"Parse config file failed, \
leetctl has just generated a new leetcode.toml at {}, \
the current one at {} seems missing some keys, Please compare \
the new file and add the missing keys.\n",
CONFIG,
"~/.leetcode/leetcode.toml".yellow().bold().underline(),
)]
Config(#[from] toml::de::Error),
#[error("Maybe you not login on the Chrome, you can login and retry")]
ChromeNotLogin,
#[error("Could not open the problem cache at {path}: {source}")]
CacheOpen {
path: String,
source: diesel::ConnectionError,
},
#[error("Unknown problem set `{0}`. Run `leetctl sets` to see what is available.")]
UnknownSet(String),
#[error(
"Bundled problem set `{slug}` failed to parse. This is a packaging bug in leetctl, \
not a problem with your config — please report it at {}\n{source}",
"https://github.com/siansiansu/leetctl/issues/new".underline()
)]
SetData {
slug: String,
source: toml::de::Error,
},
#[error(
"No problem matches {0}.\n\
Relax the filters, or run `leetctl data -u` to refresh the problem cache."
)]
NoProblemsMatch(String),
#[error(
"LeetCode is rate-limiting this machine: it answered HTTP 429 instead of a result. \
Wait a minute before retrying."
)]
Throttled,
#[error(
"Cloudflare answered with a bot challenge instead of a result. This is not a rate \
limit — the request itself scored as automated. Open {} in Chrome, let the challenge \
clear, and retry: leetctl forwards Chrome's `cf_clearance` cookie, and it is only \
good for as long as Chrome's own is.",
"https://leetcode.com".underline()
)]
CloudflareChallenge,
#[error(
"The judge never reported a result for submission {rid}. It may still be running — \
check it at {}",
"https://leetcode.com/submissions/".underline()
)]
VerifyTimeout { rid: String },
#[error(transparent)]
ParseInt(#[from] std::num::ParseIntError),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Toml(#[from] toml::ser::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Anyhow(#[from] anyhow::Error),
#[cfg(feature = "pym")]
#[error(transparent)]
Pyo3(#[from] pyo3::PyErr),
}
impl std::convert::From<diesel::result::Error> for Error {
fn from(err: diesel::result::Error) -> Self {
match err {
diesel::result::Error::NotFound => Error::Anyhow(anyhow!(
"NotFound, you may update cache with `leetctl data -u`, and try it again\r\n"
)),
_ => Error::Anyhow(anyhow!("{err}")),
}
}
}