practicode 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,205 @@
1
+ use super::*;
2
+
3
+ pub fn ensure_submission(root: &Path, problem: &Problem, settings: &Settings) -> Result<PathBuf> {
4
+ let language = normalize_language(&settings.language);
5
+ let path = root
6
+ .join("submissions")
7
+ .join(&problem.id)
8
+ .join(format!("solution.{}", ext_for(&language)));
9
+ if !path.exists() {
10
+ if let Some(parent) = path.parent() {
11
+ fs::create_dir_all(parent)?;
12
+ }
13
+ fs::write(&path, template_for(&language))?;
14
+ }
15
+ Ok(path)
16
+ }
17
+ pub fn judge(root: &Path, problem: &Problem, settings: &Settings) -> JudgeResult {
18
+ if problem.cases.is_empty() {
19
+ return JudgeResult {
20
+ passed: false,
21
+ passed_cases: 0,
22
+ total_cases: 0,
23
+ output: "problem has no judge cases".to_string(),
24
+ };
25
+ }
26
+ let path = match ensure_submission(root, problem, settings) {
27
+ Ok(path) => path,
28
+ Err(error) => {
29
+ return JudgeResult {
30
+ passed: false,
31
+ passed_cases: 0,
32
+ total_cases: problem.cases.len(),
33
+ output: error.to_string(),
34
+ };
35
+ }
36
+ };
37
+ let language = normalize_language(&settings.language);
38
+ let command = match command_for(root, &path, &language) {
39
+ Ok(Some(command)) => command,
40
+ Ok(None) => {
41
+ return JudgeResult {
42
+ passed: false,
43
+ passed_cases: 0,
44
+ total_cases: problem.cases.len(),
45
+ output: format!("Missing runtime for {}", settings.language),
46
+ };
47
+ }
48
+ Err(error) => {
49
+ return JudgeResult {
50
+ passed: false,
51
+ passed_cases: 0,
52
+ total_cases: problem.cases.len(),
53
+ output: format!("compile failed\n{error}"),
54
+ };
55
+ }
56
+ };
57
+ let run_dir = root.join(".practicode/build").join(&problem.id).join("run");
58
+ if let Err(error) = fs::create_dir_all(&run_dir) {
59
+ return JudgeResult {
60
+ passed: false,
61
+ passed_cases: 0,
62
+ total_cases: problem.cases.len(),
63
+ output: error.to_string(),
64
+ };
65
+ }
66
+
67
+ let mut passed = 0;
68
+ let mut lines = Vec::new();
69
+ for (index, case) in problem.cases.iter().enumerate() {
70
+ let mut process = Command::new(&command.program);
71
+ process.args(&command.args).current_dir(&run_dir);
72
+ let run = match run_capture(&mut process, &case.input, Duration::from_secs(5)) {
73
+ Ok(run) => run,
74
+ Err(error) => {
75
+ lines.push(format!("Case {}: FAIL", index + 1));
76
+ push_labeled_block(&mut lines, "Error", &error.to_string());
77
+ break;
78
+ }
79
+ };
80
+ let got = run.stdout.trim();
81
+ let expected = case.output.trim();
82
+ if !run.timed_out && run.code == Some(0) && got == expected {
83
+ passed += 1;
84
+ lines.push(format!("Case {}: PASS", index + 1));
85
+ if !run.stderr.trim().is_empty() {
86
+ push_labeled_block(&mut lines, "Stderr", run.stderr.trim_end());
87
+ }
88
+ } else {
89
+ lines.push(format!("Case {}: FAIL", index + 1));
90
+ if run.timed_out {
91
+ push_labeled_block(&mut lines, "Error", "timeout: 5s");
92
+ }
93
+ push_labeled_block(&mut lines, "Input", case.input.trim_end());
94
+ push_labeled_block(&mut lines, "Expected", expected);
95
+ push_labeled_block(&mut lines, "Got", run.stdout.trim_end());
96
+ if !run.stderr.trim().is_empty() {
97
+ push_labeled_block(&mut lines, "Stderr", run.stderr.trim_end());
98
+ }
99
+ break;
100
+ }
101
+ }
102
+
103
+ JudgeResult {
104
+ passed: passed == problem.cases.len(),
105
+ passed_cases: passed,
106
+ total_cases: problem.cases.len(),
107
+ output: lines.join("\n"),
108
+ }
109
+ }
110
+
111
+ fn push_labeled_block(lines: &mut Vec<String>, label: &str, body: &str) {
112
+ lines.push(String::new());
113
+ lines.push(label.to_string());
114
+ if body.is_empty() {
115
+ lines.push(" <empty>".to_string());
116
+ } else {
117
+ lines.extend(body.lines().map(|line| format!(" {line}")));
118
+ }
119
+ }
120
+
121
+ pub fn command_for(root: &Path, path: &Path, language: &str) -> Result<Option<CommandSpec>> {
122
+ match language {
123
+ "python" => Ok(which("python3")
124
+ .or_else(|| which("python"))
125
+ .map(|program| CommandSpec {
126
+ program,
127
+ args: vec![path.display().to_string()],
128
+ })),
129
+ "ts" => Ok(which("node").map(|program| CommandSpec {
130
+ program,
131
+ args: vec![
132
+ "--experimental-strip-types".to_string(),
133
+ path.display().to_string(),
134
+ ],
135
+ })),
136
+ "java" => compile_java(root, path),
137
+ "rust" => compile_rust(root, path),
138
+ _ => Ok(None),
139
+ }
140
+ }
141
+
142
+ fn compile_java(root: &Path, path: &Path) -> Result<Option<CommandSpec>> {
143
+ let Some(javac) = which("javac") else {
144
+ return Ok(None);
145
+ };
146
+ let Some(java) = which("java") else {
147
+ return Ok(None);
148
+ };
149
+ let build = root
150
+ .join(".practicode/build")
151
+ .join(path.parent().and_then(Path::file_name).unwrap_or_default())
152
+ .join("java");
153
+ fs::create_dir_all(&build)?;
154
+ let mut compile = Command::new(javac);
155
+ compile
156
+ .args([
157
+ "-d",
158
+ &build.display().to_string(),
159
+ &path.display().to_string(),
160
+ ])
161
+ .current_dir(root);
162
+ let output = run_capture(&mut compile, "", Duration::from_secs(30))?;
163
+ if output.code != Some(0) {
164
+ return Err(anyhow!(output.stderr.trim().to_string()));
165
+ }
166
+ Ok(Some(CommandSpec {
167
+ program: java,
168
+ args: vec![
169
+ "-cp".to_string(),
170
+ build.display().to_string(),
171
+ "Solution".to_string(),
172
+ ],
173
+ }))
174
+ }
175
+
176
+ fn compile_rust(root: &Path, path: &Path) -> Result<Option<CommandSpec>> {
177
+ let Some(rustc) = which("rustc") else {
178
+ return Ok(None);
179
+ };
180
+ let build = root
181
+ .join(".practicode/build")
182
+ .join(path.parent().and_then(Path::file_name).unwrap_or_default());
183
+ fs::create_dir_all(&build)?;
184
+ let exe = build.join(if cfg!(windows) {
185
+ "solution.exe"
186
+ } else {
187
+ "solution"
188
+ });
189
+ let mut compile = Command::new(rustc);
190
+ compile
191
+ .args([
192
+ path.display().to_string(),
193
+ "-o".to_string(),
194
+ exe.display().to_string(),
195
+ ])
196
+ .current_dir(root);
197
+ let output = run_capture(&mut compile, "", Duration::from_secs(30))?;
198
+ if output.code != Some(0) {
199
+ return Err(anyhow!(output.stderr.trim().to_string()));
200
+ }
201
+ Ok(Some(CommandSpec {
202
+ program: exe,
203
+ args: Vec::new(),
204
+ }))
205
+ }
@@ -0,0 +1,110 @@
1
+ use super::*;
2
+
3
+ pub fn ext_for(language: &str) -> &'static str {
4
+ match normalize_language(language).as_str() {
5
+ "python" => "py",
6
+ "ts" => "ts",
7
+ "java" => "java",
8
+ "rust" => "rs",
9
+ _ => "py",
10
+ }
11
+ }
12
+ pub fn problem_by_id<'a>(bank: &'a [Problem], problem_id: &str) -> Option<&'a Problem> {
13
+ bank.iter().find(|problem| problem.id == problem_id)
14
+ }
15
+
16
+ pub fn normalize_language(language: &str) -> String {
17
+ let language = language.trim().to_lowercase();
18
+ if LANGUAGES.contains(&language.as_str()) {
19
+ language
20
+ } else {
21
+ "python".to_string()
22
+ }
23
+ }
24
+
25
+ pub fn parse_language_list(value: &str) -> Vec<String> {
26
+ let mut languages = Vec::new();
27
+ for language in value.split(',') {
28
+ let language = language.trim().to_lowercase();
29
+ if language == "all" {
30
+ return Vec::new();
31
+ }
32
+ if LANGUAGES.contains(&language.as_str()) && !languages.contains(&language) {
33
+ languages.push(language);
34
+ }
35
+ }
36
+ languages
37
+ }
38
+
39
+ pub fn normalize_language_list(languages: &[String]) -> Vec<String> {
40
+ parse_language_list(&languages.join(","))
41
+ }
42
+
43
+ pub fn parse_ui_language_list(value: &str) -> Vec<String> {
44
+ let mut languages = Vec::new();
45
+ for language in value.split(',') {
46
+ let lower = language.trim().to_lowercase();
47
+ if lower == "all" {
48
+ return Vec::new();
49
+ }
50
+ let language = lower
51
+ .split(['-', '_'])
52
+ .next()
53
+ .filter(|value| UI_LANGUAGES.contains(value))
54
+ .unwrap_or("");
55
+ if language == "all" {
56
+ return Vec::new();
57
+ }
58
+ if !language.is_empty() && !languages.iter().any(|value| value == language) {
59
+ languages.push(language.to_string());
60
+ }
61
+ }
62
+ languages
63
+ }
64
+
65
+ pub fn normalize_ui_language_list(languages: &[String]) -> Vec<String> {
66
+ parse_ui_language_list(&languages.join(","))
67
+ }
68
+
69
+ pub fn normalize_next_source(source: &str) -> String {
70
+ if source == "ai" {
71
+ "ai".to_string()
72
+ } else {
73
+ "bank".to_string()
74
+ }
75
+ }
76
+
77
+ pub fn normalize_ai_provider(provider: &str) -> String {
78
+ if provider == "claude" {
79
+ "claude".to_string()
80
+ } else {
81
+ "codex".to_string()
82
+ }
83
+ }
84
+
85
+ pub fn normalize_ai_effort(provider: &str, effort: &str) -> String {
86
+ let effort = effort.trim().to_lowercase();
87
+ let provider = normalize_ai_provider(provider);
88
+ let allowed = if provider == "claude" {
89
+ CLAUDE_AI_EFFORTS
90
+ } else {
91
+ CODEX_AI_EFFORTS
92
+ };
93
+ if allowed.contains(&effort.as_str()) {
94
+ effort
95
+ } else if provider == "codex" && effort == "max" {
96
+ "xhigh".to_string()
97
+ } else {
98
+ "auto".to_string()
99
+ }
100
+ }
101
+
102
+ pub fn template_for(language: &str) -> String {
103
+ match normalize_language(language).as_str() {
104
+ "python" => "# Read from stdin and print to stdout.\nimport sys\n\n\n".to_string(),
105
+ "ts" => "const fs = require('fs');\nconst input = fs.readFileSync(0, 'utf8');\n\n".to_string(),
106
+ "java" => "import java.io.*;\n\nclass Solution {\n public static void main(String[] args) throws Exception {\n }\n}\n".to_string(),
107
+ "rust" => "fn main() {\n}\n".to_string(),
108
+ _ => String::new(),
109
+ }
110
+ }
@@ -0,0 +1,171 @@
1
+ use super::*;
2
+
3
+ pub const LANGUAGES: &[&str] = &["python", "ts", "java", "rust"];
4
+ pub const THEMES: &[&str] = &["dark", "light"];
5
+ pub const AI_PROVIDERS: &[&str] = &["codex", "claude"];
6
+ pub const CODEX_AI_EFFORTS: &[&str] = &["auto", "low", "medium", "high", "xhigh"];
7
+ pub const CLAUDE_AI_EFFORTS: &[&str] = &["auto", "low", "medium", "high", "xhigh", "max"];
8
+ pub const BANK_PATH: &str = ".practicode/problem_bank.json";
9
+ pub const STATE_PATH: &str = ".practicode/problem-state.json";
10
+ pub const PROBLEM_NOTES_PATH: &str = ".practicode/problem_notes.md";
11
+
12
+ #[derive(Clone, Debug, Serialize, Deserialize)]
13
+ pub struct Settings {
14
+ #[serde(default = "default_language")]
15
+ pub language: String,
16
+ #[serde(default = "default_ui_language")]
17
+ pub ui_language: String,
18
+ #[serde(default = "default_theme")]
19
+ pub theme: String,
20
+ #[serde(default = "default_difficulty")]
21
+ pub difficulty: String,
22
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
23
+ pub topics: Vec<String>,
24
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
25
+ pub avoid_topics: Vec<String>,
26
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
27
+ pub generate_languages: Vec<String>,
28
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
29
+ pub generate_ui_languages: Vec<String>,
30
+ #[serde(default = "default_editor")]
31
+ pub editor: String,
32
+ #[serde(default = "default_next_source")]
33
+ pub next_source: String,
34
+ #[serde(default = "default_ai_provider")]
35
+ pub ai_provider: String,
36
+ #[serde(default = "default_ai_model")]
37
+ pub ai_model: String,
38
+ #[serde(default = "default_ai_effort")]
39
+ pub ai_effort: String,
40
+ #[serde(default, skip_serializing_if = "String::is_empty")]
41
+ pub ai_next_command: String,
42
+ }
43
+
44
+ impl Default for Settings {
45
+ fn default() -> Self {
46
+ Self {
47
+ language: default_language(),
48
+ ui_language: default_ui_language(),
49
+ theme: default_theme(),
50
+ difficulty: default_difficulty(),
51
+ topics: Vec::new(),
52
+ avoid_topics: Vec::new(),
53
+ generate_languages: Vec::new(),
54
+ generate_ui_languages: Vec::new(),
55
+ editor: default_editor(),
56
+ next_source: default_next_source(),
57
+ ai_provider: default_ai_provider(),
58
+ ai_model: default_ai_model(),
59
+ ai_effort: default_ai_effort(),
60
+ ai_next_command: String::new(),
61
+ }
62
+ }
63
+ }
64
+
65
+ impl Settings {
66
+ pub fn next_ai_command(&self) -> &str {
67
+ &self.ai_next_command
68
+ }
69
+
70
+ pub fn model_arg(&self) -> Option<&str> {
71
+ let model = self.ai_model.trim();
72
+ if model.is_empty() || model == "auto" {
73
+ None
74
+ } else {
75
+ Some(model)
76
+ }
77
+ }
78
+
79
+ pub fn effort_arg(&self) -> Option<&str> {
80
+ let effort = self.ai_effort.trim();
81
+ if effort.is_empty() || effort == "auto" {
82
+ None
83
+ } else {
84
+ Some(effort)
85
+ }
86
+ }
87
+ }
88
+
89
+ #[derive(Clone, Debug, Serialize, Deserialize)]
90
+ pub struct HistoryItem {
91
+ pub id: String,
92
+ pub status: String,
93
+ }
94
+
95
+ #[derive(Clone, Debug, Serialize, Deserialize)]
96
+ pub struct AppState {
97
+ pub current_problem: String,
98
+ #[serde(default)]
99
+ pub settings: Settings,
100
+ #[serde(default)]
101
+ pub solved: Vec<String>,
102
+ #[serde(default)]
103
+ pub history: Vec<HistoryItem>,
104
+ #[serde(default = "default_suggested_difficulty")]
105
+ pub suggested_next_difficulty: String,
106
+ }
107
+
108
+ #[derive(Clone, Debug, Serialize, Deserialize)]
109
+ pub struct Problem {
110
+ pub id: String,
111
+ pub slug: String,
112
+ pub difficulty: String,
113
+ pub topics: Vec<String>,
114
+ pub title: HashMap<String, String>,
115
+ pub statement: HashMap<String, String>,
116
+ pub input: HashMap<String, String>,
117
+ pub output: HashMap<String, String>,
118
+ pub examples: Vec<IoCase>,
119
+ pub cases: Vec<IoCase>,
120
+ pub answers: HashMap<String, String>,
121
+ }
122
+
123
+ #[derive(Clone, Debug, Serialize, Deserialize)]
124
+ pub struct IoCase {
125
+ pub input: String,
126
+ pub output: String,
127
+ }
128
+
129
+ #[derive(Clone, Debug)]
130
+ pub struct JudgeResult {
131
+ pub passed: bool,
132
+ pub passed_cases: usize,
133
+ pub total_cases: usize,
134
+ pub output: String,
135
+ }
136
+
137
+ pub fn default_language() -> String {
138
+ "python".to_string()
139
+ }
140
+
141
+ pub fn default_ui_language() -> String {
142
+ "en".to_string()
143
+ }
144
+
145
+ pub fn default_theme() -> String {
146
+ "dark".to_string()
147
+ }
148
+
149
+ pub fn default_editor() -> String {
150
+ "vim".to_string()
151
+ }
152
+
153
+ pub fn default_next_source() -> String {
154
+ "bank".to_string()
155
+ }
156
+
157
+ pub fn default_ai_provider() -> String {
158
+ "codex".to_string()
159
+ }
160
+
161
+ pub fn default_ai_model() -> String {
162
+ "auto".to_string()
163
+ }
164
+
165
+ pub fn default_ai_effort() -> String {
166
+ "auto".to_string()
167
+ }
168
+
169
+ pub fn default_suggested_difficulty() -> String {
170
+ "easy".to_string()
171
+ }
@@ -0,0 +1,92 @@
1
+ use super::*;
2
+
3
+ pub fn ensure_problem_files(root: &Path, problem: &Problem) -> Result<()> {
4
+ let problem_dir = root.join("problems").join(&problem.id);
5
+ fs::create_dir_all(&problem_dir)?;
6
+ let readme = problem_dir.join("README.md");
7
+ if readme.exists() {
8
+ return Ok(());
9
+ }
10
+ let examples = problem
11
+ .examples
12
+ .iter()
13
+ .map(|case| format!("input:\n{}output:\n{}", case.input, case.output))
14
+ .collect::<Vec<_>>()
15
+ .join("\n");
16
+ fs::write(
17
+ readme,
18
+ format!(
19
+ "# {}. {}\n\n난이도: {}\n\n{}\n\n## 입력\n\n{}\n\n## 출력\n\n{}\n\n## 예시\n\n```text\n{}\n```\n",
20
+ problem.id,
21
+ localized(&problem.title, "ko"),
22
+ problem.difficulty,
23
+ localized(&problem.statement, "ko"),
24
+ localized(&problem.input, "ko"),
25
+ localized(&problem.output, "ko"),
26
+ examples
27
+ ),
28
+ )?;
29
+ Ok(())
30
+ }
31
+
32
+ pub fn upsert_problem_index(root: &Path, problem: &Problem, status: &str) -> Result<()> {
33
+ let index = root.join("problems/INDEX.md");
34
+ if let Some(parent) = index.parent() {
35
+ fs::create_dir_all(parent)?;
36
+ }
37
+ let mut rows: HashMap<String, (String, String, String, String)> = HashMap::new();
38
+ if index.exists() {
39
+ for line in fs::read_to_string(&index)?.lines() {
40
+ let parts = line
41
+ .trim()
42
+ .trim_matches('|')
43
+ .split('|')
44
+ .map(str::trim)
45
+ .collect::<Vec<_>>();
46
+ if parts.len() == 5 && parts[0].chars().all(|c| c.is_ascii_digit()) {
47
+ rows.insert(
48
+ parts[0].to_string(),
49
+ (
50
+ parts[1].to_string(),
51
+ parts[2].to_string(),
52
+ parts[3].to_string(),
53
+ parts[4].to_string(),
54
+ ),
55
+ );
56
+ }
57
+ }
58
+ }
59
+ let number = problem
60
+ .id
61
+ .split_once('-')
62
+ .map(|(number, _)| number)
63
+ .unwrap_or(&problem.id);
64
+ rows.insert(
65
+ number.to_string(),
66
+ (
67
+ problem.slug.clone(),
68
+ problem.difficulty.clone(),
69
+ problem.topics.join(", "),
70
+ status.to_string(),
71
+ ),
72
+ );
73
+ let mut numbers = rows.keys().cloned().collect::<Vec<_>>();
74
+ numbers.sort();
75
+ let body = numbers
76
+ .into_iter()
77
+ .filter_map(|number| {
78
+ rows.get(&number)
79
+ .map(|(slug, difficulty, topics, row_status)| {
80
+ format!("| {number} | {slug} | {difficulty} | {topics} | {row_status} |")
81
+ })
82
+ })
83
+ .collect::<Vec<_>>()
84
+ .join("\n");
85
+ fs::write(
86
+ index,
87
+ format!(
88
+ "# Problem Index\n\n| # | Slug | Difficulty | Topics | Status |\n|---|------|------------|--------|--------|\n{body}\n"
89
+ ),
90
+ )?;
91
+ Ok(())
92
+ }
@@ -0,0 +1,97 @@
1
+ use super::*;
2
+
3
+ pub fn give_up(root: &Path, problem: &Problem, state: &mut AppState) -> Result<String> {
4
+ let language = normalize_language(&state.settings.language);
5
+ let answer = problem
6
+ .answers
7
+ .get(&language)
8
+ .cloned()
9
+ .unwrap_or_else(|| problem.answers.values().next().cloned().unwrap_or_default());
10
+ mark_history(state, &problem.id, "gave_up");
11
+ upsert_problem_index(root, problem, "gave_up")?;
12
+ save_state(root, state)?;
13
+ Ok(answer)
14
+ }
15
+
16
+ pub fn next_problem(
17
+ root: &Path,
18
+ bank: &[Problem],
19
+ state: &mut AppState,
20
+ ) -> Result<Option<Problem>> {
21
+ let seen = state
22
+ .history
23
+ .iter()
24
+ .map(|item| item.id.as_str())
25
+ .collect::<Vec<_>>();
26
+ let preferred = if state.settings.difficulty == "auto" {
27
+ &state.suggested_next_difficulty
28
+ } else {
29
+ &state.settings.difficulty
30
+ };
31
+ let problem = bank
32
+ .iter()
33
+ .find(|item| !seen.contains(&item.id.as_str()) && &item.difficulty == preferred)
34
+ .or_else(|| bank.iter().find(|item| !seen.contains(&item.id.as_str())));
35
+ let Some(problem) = problem.cloned() else {
36
+ return Ok(None);
37
+ };
38
+ state.current_problem = problem.id.clone();
39
+ mark_history(state, &problem.id, "assigned");
40
+ save_state(root, state)?;
41
+ ensure_problem_files(root, &problem)?;
42
+ upsert_problem_index(root, &problem, "assigned")?;
43
+ Ok(Some(problem))
44
+ }
45
+
46
+ pub fn previous_problem(root: &Path, bank: &[Problem], state: &mut AppState) -> Result<Problem> {
47
+ let known_ids = bank
48
+ .iter()
49
+ .map(|problem| problem.id.as_str())
50
+ .collect::<Vec<_>>();
51
+ let history = state
52
+ .history
53
+ .iter()
54
+ .filter(|item| known_ids.contains(&item.id.as_str()))
55
+ .map(|item| item.id.clone())
56
+ .collect::<Vec<_>>();
57
+ let Some(index) = history.iter().position(|id| id == &state.current_problem) else {
58
+ return problem_by_id(bank, &state.current_problem)
59
+ .cloned()
60
+ .ok_or_else(|| anyhow!("current problem missing"));
61
+ };
62
+ if index == 0 {
63
+ return problem_by_id(bank, &state.current_problem)
64
+ .cloned()
65
+ .ok_or_else(|| anyhow!("current problem missing"));
66
+ }
67
+ state.current_problem = history[index - 1].clone();
68
+ save_state(root, state)?;
69
+ problem_by_id(bank, &state.current_problem)
70
+ .cloned()
71
+ .ok_or_else(|| anyhow!("current problem missing"))
72
+ }
73
+
74
+ pub fn record_pass(root: &Path, problem: &Problem, state: &mut AppState) -> Result<()> {
75
+ if !state.solved.contains(&problem.id) {
76
+ state.solved.push(problem.id.clone());
77
+ }
78
+ mark_history(state, &problem.id, "solved");
79
+ upsert_problem_index(root, problem, "solved")?;
80
+ state.suggested_next_difficulty = if state.solved.len() >= 2 {
81
+ "medium".to_string()
82
+ } else {
83
+ "easy".to_string()
84
+ };
85
+ save_state(root, state)
86
+ }
87
+
88
+ pub fn mark_history(state: &mut AppState, problem_id: &str, status: &str) {
89
+ if let Some(item) = state.history.iter_mut().find(|item| item.id == problem_id) {
90
+ item.status = status.to_string();
91
+ } else {
92
+ state.history.push(HistoryItem {
93
+ id: problem_id.to_string(),
94
+ status: status.to_string(),
95
+ });
96
+ }
97
+ }