use super::{Level, Quiz};
use emacs::{defun, Env, Error, IntoLisp, Result, Value};
use lazy_static::*;
use std::sync::Mutex;
emacs::plugin_is_GPL_compatible!();
lazy_static! {
static ref CACHE_QUiZ: Mutex<Option<Quiz>> = Mutex::new(None);
}
#[emacs::module(name = "leetcode-picker-inner")]
fn init(env: &Env) -> Result<Value<'_>> {
env.message("Done loading!")
}
#[defun(mod_in_name = false)]
fn set_token(env: &Env, token: String) -> Result<()> {
Ok(super::set_token(&Some(token)))
}
#[defun(mod_in_name = false)]
fn get_random(env: &Env, format_str: Option<String>) -> Result<Value<'_>> {
let qq = Quiz::get_randomly(None).unwrap();
update_static_quiz(&qq);
qq.use_fmt_temp(&format_str, &None).unwrap().into_lisp(env)
}
#[defun(mod_in_name = false)]
fn get_random_with_level(env: &Env, level: Level, format_str: Option<String>) -> Result<Value<'_>> {
let qq = Quiz::get_randomly(Some(level)).unwrap();
update_static_quiz(&qq);
qq.use_fmt_temp(&format_str, &None).unwrap().into_lisp(env)
}
#[defun(mod_in_name = false)]
fn get_code_snippet(env: &Env, lang: String) -> Result<String> {
match CACHE_QUiZ.lock() {
Ok(ref mut q) => Ok(q.as_ref().unwrap().code_snippet(&lang).unwrap().to_string()),
Err(e) => {
panic!("{}", e);
}
}
}
fn update_static_quiz(qq: &Quiz) {
match CACHE_QUiZ.lock() {
Ok(ref mut q) => {
**q = Some(qq.clone());
}
Err(e) => {
panic!("{}", e);
}
}
}