Skip to content

Commit 56c42df

Browse files
author
dianxu
committed
Update configuration and main modules for improved structure
1 parent 713bbc1 commit 56c42df

3 files changed

Lines changed: 35 additions & 46 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct ServerConfig {
1919
pub port: i32,
2020

2121
/// verbose mode
22-
#[arg(short, long, default_value_t = false)]
22+
#[arg(short, long, default_value_t = true)]
2323
pub verbose: bool,
2424

2525
/// log to file

src/main.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ async fn get_path(task: &model::Task) -> Result<String, String> {
4343
contest = contest.replace(*illegal_char, "_");
4444
}
4545
groups.push(contest);
46-
let problem = task.name.clone();
46+
let mut problem = task.name.clone();
47+
for illegal_char in ILLEGAL_CHARS.iter() {
48+
problem = problem.replace(*illegal_char, "_");
49+
}
4750
groups.push(problem);
51+
} else {
52+
log::debug!("Short path enabled, using short path.");
4853
}
4954
log::debug!("Problem groups: {:?}", groups);
5055
let mut path = Path::new(&SERVER_CONFIG.workspace).to_path_buf();
@@ -63,43 +68,32 @@ async fn get_path(task: &model::Task) -> Result<String, String> {
6368

6469
async fn main_post(Json(task): Json<model::Task>) -> StatusCode {
6570
log::debug!("Problem received: {:?}", task);
66-
let path_str = match get_path(&task).await {
67-
Ok(path_str) => path_str,
68-
Err(info) => {
69-
log::error!("{}", info);
70-
return StatusCode::INTERNAL_SERVER_ERROR;
71-
}
72-
};
71+
let path_str = get_path(&task).await.expect("Failed to get path.");
7372
let path = Path::new(&path_str);
7473
if !path.exists() {
75-
match fs::create_dir_all(path).await {
76-
Ok(_) => {}
77-
Err(info) => {
78-
log::error!("Failed to create directory: {}, {}", path_str, info);
79-
return StatusCode::INTERNAL_SERVER_ERROR;
80-
}
81-
}
74+
fs::create_dir_all(path)
75+
.await
76+
.expect(&format!("Failed to create directory: {}", path_str));
8277
}
8378

8479
// create problem data
8580
for idx in 0..task.tests.len() {
8681
let test = &task.tests[idx];
8782
let input_path = path.join(format!("{:02}.i.txt", idx + 1));
8883
let output_path = path.join(format!("{:02}.o.txt", idx + 1));
89-
match fs::write(input_path, &test.input).await {
90-
Ok(_) => {}
91-
Err(err) => {
92-
log::error!("Failed to write input file: {}", err);
93-
return StatusCode::INTERNAL_SERVER_ERROR;
94-
}
95-
}
96-
match fs::write(output_path, &test.output).await {
97-
Ok(_) => {}
98-
Err(err) => {
99-
log::error!("Failed to write output file: {}", err);
100-
return StatusCode::INTERNAL_SERVER_ERROR;
101-
}
102-
}
84+
fs::write(input_path.clone(), &test.input)
85+
.await
86+
.expect(&format!(
87+
"Failed to write input file: {}",
88+
input_path.display()
89+
));
90+
91+
fs::write(output_path.clone(), &test.output)
92+
.await
93+
.expect(&format!(
94+
"Failed to write output file: {}",
95+
output_path.display()
96+
));
10397
}
10498
// create template files
10599
let templates: Vec<&str> = SERVER_CONFIG
@@ -118,22 +112,17 @@ async fn main_post(Json(task): Json<model::Task>) -> StatusCode {
118112
// handle template with line number and column number: src/main.rs:<line>:<column>
119113
let template_file = template.split(":").into_iter().collect::<Vec<&str>>();
120114
let source_path = Path::new(&template_file[0]);
121-
let src_filename = match source_path.file_name() {
122-
Some(filename) => filename,
123-
None => {
124-
log::error!("Failed to get file name: {:?}", source_path);
125-
return StatusCode::INTERNAL_SERVER_ERROR;
126-
}
127-
};
115+
let src_filename = source_path.to_str().expect(&format!(
116+
"Failed to convert source path to string: {:?}",
117+
source_path
118+
));
128119
let destination_pathbuf = path.join(src_filename);
129120
let destination_path = destination_pathbuf.as_path();
130-
let destination = match destination_path.to_str() {
131-
Some(destination) => destination,
132-
None => {
133-
log::error!("Failed to convert path to string: {:?}", destination_path);
134-
return StatusCode::INTERNAL_SERVER_ERROR;
135-
}
136-
};
121+
122+
let destination = destination_path.to_str().expect(&format!(
123+
"Failed to convert destination path to string: {:?}",
124+
destination_path
125+
));
137126
template::handle(source_path, destination_path);
138127
if SERVER_CONFIG.open_by_vscode {
139128
let mut open_path = destination.to_string();

0 commit comments

Comments
 (0)