-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
60 lines (50 loc) · 1.21 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate handlebars;
#[macro_use]
extern crate serde_json;
use handlebars::Handlebars;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
const TEMPLATE: &str = "/*
* [{{ problem_id }}] {{ problem_title }}
*/
struct Solution;
impl Solution {
fn add(a: i32, b: i32) -> i32 {
a + b
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_case0() {
assert_eq!(Solution::add(2, 3), 5);
}
}
";
fn main() {
let args: Vec<String> = env::args().collect();
println!("{:?}", args);
let problem_id = &args[1];
let problem_title = &args[2];
let filename = format!("src/a{}_{}.rs", problem_id, problem_title.replace("-", "_"));
println!("{}", filename);
let path = Path::new(&filename);
if path.exists() {
panic!("{} is already exists!", path.to_str().unwrap());
}
let rendered = Handlebars::new()
.render_template(
TEMPLATE,
&json!({
"problem_id": problem_id,
"problem_title": problem_title,
}),
)
.unwrap();
let mut file = File::create(path).unwrap();
file.write_all(rendered.as_bytes()).unwrap();
drop(file);
}