Skip to content

Commit e150ee1

Browse files
committed
add default return value
1 parent a1971ec commit e150ee1

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/main.rs

+42-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ extern crate serde_json;
55

66
mod problem;
77

8+
use regex::Regex;
89
use std::env;
910
use std::fs;
1011
use std::io;
1112
use std::io::Write;
1213
use std::path::Path;
13-
use regex::Regex;
1414

1515
/// main() helps to generate the submission template .rs
1616
fn main() {
@@ -75,7 +75,10 @@ fn main() {
7575
let source = template
7676
.replace("__PROBLEM_TITLE__", &problem.title)
7777
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
78-
.replace("__PROBLEM_DEFAULT_CODE__", &insert_return_in_code(&problem.return_type, &code.default_code))
78+
.replace(
79+
"__PROBLEM_DEFAULT_CODE__",
80+
&insert_return_in_code(&problem.return_type, &code.default_code),
81+
)
7982
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
8083
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
8184

@@ -149,12 +152,44 @@ fn parse_extra_use(code: &str) -> String {
149152
}
150153

151154
fn insert_return_in_code(return_type: &str, code: &str) -> String {
155+
let re = Regex::new(r"\{[ \n]+}").unwrap();
152156
match return_type {
153-
"ListNode" => {
154-
let re = Regex::new(r"\{[ \n]+}").unwrap();
155-
re.replace(&code, "{\n Some(Box::new(ListNode::new(0)))\n }").to_string()
156-
},
157-
_ => code.to_string()
157+
"ListNode" => re
158+
.replace(&code, "{\n Some(Box::new(ListNode::new(0)))\n }")
159+
.to_string(),
160+
"ListNode[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
161+
"TreeNode" => re
162+
.replace(
163+
&code,
164+
"{\n Some(Rc::new(RefCell::new(TreeNode::new(0))))\n }",
165+
)
166+
.to_string(),
167+
"boolean" => re.replace(&code, "{\n false\n }").to_string(),
168+
"character" => re.replace(&code, "{\n '0'\n }").to_string(),
169+
"character[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
170+
"double" => re.replace(&code, "{\n 0f64\n }").to_string(),
171+
"double[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
172+
"int[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
173+
"integer" => re.replace(&code, "{\n 0\n }").to_string(),
174+
"integer[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
175+
"integer[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
176+
"list<String>" => re.replace(&code, "{\n vec![]\n }").to_string(),
177+
"list<TreeNode>" => re.replace(&code, "{\n vec![]\n }").to_string(),
178+
"list<boolean>" => re.replace(&code, "{\n vec![]\n }").to_string(),
179+
"list<double>" => re.replace(&code, "{\n vec![]\n }").to_string(),
180+
"list<integer>" => re.replace(&code, "{\n vec![]\n }").to_string(),
181+
"list<list<integer>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
182+
"list<list<string>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
183+
"list<string>" => re.replace(&code, "{\n vec![]\n }").to_string(),
184+
"null" => code.to_string(),
185+
"string" => re
186+
.replace(&code, "{\n String::new()\n }")
187+
.to_string(),
188+
"string[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
189+
"void" => code.to_string(),
190+
"NestedInteger" => code.to_string(),
191+
"Node" => code.to_string(),
192+
_ => code.to_string(),
158193
}
159194
}
160195

0 commit comments

Comments
 (0)