1use anyhow::anyhow;
2use std::{fs, io, path::PathBuf};
3
4pub fn copy_recursively(src: &PathBuf, dest: &PathBuf) -> io::Result<()> {
5 log::debug!("copying {:?} to {:?}", src, dest);
6 if fs::metadata(src)?.is_file() {
7 fs::copy(src, dest)?;
8 } else {
9 if !dest.exists() || !fs::metadata(dest)?.is_dir() {
10 log::debug!("creating dir: {:?}", dest);
11 fs::create_dir_all(dest)?;
12 }
13 for entry in fs::read_dir(src)? {
14 let entry = entry?;
15 let src_path = entry.path();
16 let file_name = src_path.file_name().unwrap();
17 let dest_path = dest.join(file_name);
18 copy_recursively(&src_path, &dest_path)?;
19 }
20 }
21
22 Ok(())
23}
24
25use std::fs::File;
26use std::io::{BufRead, BufReader};
27
28use crate::error::JudgeCoreError;
29
30pub fn compare_files(file_path1: &PathBuf, file_path2: &PathBuf) -> bool {
31 let file1 = BufReader::new(File::open(file_path1).unwrap());
32 let file2 = BufReader::new(File::open(file_path2).unwrap());
33
34 file1.lines().zip(file2.lines()).all(|(line1, line2)| {
35 let line1_string = line1.unwrap();
37 let line2_string: String = line2.unwrap();
38 let trimed1 = line1_string.trim_end();
39 let trimed2 = line2_string.trim_end();
40 trimed1 == trimed2
41 })
42}
43
44pub fn get_pathbuf_str(path: &PathBuf) -> Result<String, JudgeCoreError> {
45 match path.to_str() {
46 Some(path_str) => Ok(path_str.to_owned()),
47 None => Err(JudgeCoreError::AnyhowError(anyhow!(
48 "PathBuf to str failed: {:?}",
49 path
50 ))),
51 }
52}