Skip to content

Commit

Permalink
feat: check modification date
Browse files Browse the repository at this point in the history
  • Loading branch information
xfoxfu committed Oct 18, 2023
1 parent a73f960 commit be1e542
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 3 additions & 1 deletion checker.cfg.json
Expand Up @@ -18,5 +18,7 @@
"name": "variance",
"regex": "^variance\\\\variance\\.(cpp|c|pas)$"
}
]
],
"start_time": "2023-10-18T15:00:00Z",
"end_time": "2023-10-18T16:00:00Z"
}
9 changes: 9 additions & 0 deletions src/config.rs
@@ -1,3 +1,6 @@
use std::collections::HashMap;

use chrono::{DateTime, Utc};
use regex::Regex;
use serde::{Deserialize, Serialize};

Expand All @@ -9,6 +12,8 @@ pub struct Problem {
pub regex: Regex,
#[serde(default)]
pub existing_files: Vec<String>,
#[serde(default)]
pub existing_files_date: HashMap<String, DateTime<Utc>>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -19,6 +24,10 @@ pub struct Contestant {
pub regex: Regex,
/// 所有题目的配置项
pub problems: Vec<Problem>,
/// 考试开始时间
pub start_time: DateTime<Utc>,
/// 考试结束时间
pub end_time: DateTime<Utc>,
}

mod regex_sd {
Expand Down
29 changes: 25 additions & 4 deletions src/main.rs
Expand Up @@ -110,8 +110,13 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
.regex
.is_match(dir2.path().strip_prefix(&user_directory)?.to_str().unwrap())
{
prob.existing_files
.push(dir2.path().to_str().unwrap().to_string());
let filepath = dir2.path().to_str().unwrap().to_string();
prob.existing_files.push(filepath.clone());
if let Ok(meta) = dir2.metadata() {
if let Ok(modi) = meta.modified() {
prob.existing_files_date.insert(filepath, modi.into());
}
}
}
}
}
Expand All @@ -122,15 +127,31 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
if prob.existing_files.is_empty() {
messages.push((format!(" 未找到源代码文件."), Color::Black));
} else if prob.existing_files.len() == 1 {
let f = Path::new(&prob.existing_files[0]).strip_prefix(&user_directory)?;
let filename = &prob.existing_files[0];
let f = Path::new(filename).strip_prefix(&user_directory)?;
messages.push((
format!(
" 找到文件 {} => 校验码 {}.",
f.display(),
result_into_ok_or_err(try_crc32(&prob.existing_files[0]))
result_into_ok_or_err(try_crc32(filename))
),
Color::Black,
));
if let Some(mod_date) = &prob.existing_files_date.get(filename) {
if mod_date >= &&cfg.start_time && mod_date <= &&cfg.end_time {
messages.push((
format!(" 修改日期有效 {}.", mod_date),
Color::Black,
));
} else {
messages.push((
format!(" 修改日期不在考试时间范围内 {}.", mod_date),
Color::Red,
));
}
} else {
messages.push((format!(" 文件没有修改日期记录."), Color::Yellow));
}
} else {
messages.push((format!(" 找到多个源代码文件:"), Color::Red));
for file in prob.existing_files.iter() {
Expand Down

0 comments on commit be1e542

Please sign in to comment.