Skip to content

Commit 4e501cd

Browse files
committed
rename error to tmcerror
1 parent 430b31f commit 4e501cd

File tree

21 files changed

+111
-104
lines changed

21 files changed

+111
-104
lines changed

tmc-langs-core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub enum CoreError {
5050
CacheDir,
5151

5252
#[error(transparent)]
53-
TmcLangs(#[from] tmc_langs_util::Error),
53+
TmcLangs(#[from] tmc_langs_util::TmcError),
5454
#[error(transparent)]
5555
Response(#[from] response::ResponseError),
5656
#[error(transparent)]

tmc-langs-framework/src/domain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
pub mod meta_syntax;
44

5-
use super::{Error, Result};
5+
use super::{Result, TmcError};
66
use log::debug;
77
use serde::{Deserialize, Serialize};
88
use std::collections::{HashMap, HashSet};
@@ -156,7 +156,7 @@ impl TmcProjectYml {
156156
return Ok(Self::default());
157157
}
158158
debug!("reading .tmcproject.yml from {}", config_path.display());
159-
let file = File::open(&config_path).map_err(|e| Error::OpenFile(config_path, e))?;
159+
let file = File::open(&config_path).map_err(|e| TmcError::OpenFile(config_path, e))?;
160160
Ok(serde_yaml::from_reader(file)?)
161161
}
162162
}

tmc-langs-framework/src/domain/meta_syntax.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains utilities for parsing text files, separating lines into
22
//! strings, stubs and solutions so that they can be more easily filtered accordingly
33
4-
use crate::{Error, Result};
4+
use crate::{Result, TmcError};
55
use lazy_static::lazy_static;
66
use log::debug;
77
use regex::{Captures, Regex};
@@ -183,7 +183,7 @@ impl<B: BufRead> Iterator for MetaSyntaxParser<B> {
183183
Some(Ok(MetaString::String(s)))
184184
}
185185
}
186-
Err(err) => Some(Err(Error::ReadLine(err))),
186+
Err(err) => Some(Err(TmcError::ReadLine(err))),
187187
}
188188
}
189189
}

tmc-langs-framework/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Duration;
55
use thiserror::Error;
66

77
#[derive(Error, Debug)]
8-
pub enum Error {
8+
pub enum TmcError {
99
// IO
1010
#[error("Failed to open file at {0}")]
1111
OpenFile(PathBuf, #[source] std::io::Error),

tmc-langs-framework/src/io/submission_processing.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functions for processing submissions.
22
33
use crate::policy::StudentFilePolicy;
4-
use crate::{Error, Result};
4+
use crate::{Result, TmcError};
55

66
use crate::domain::meta_syntax::{MetaString, MetaSyntaxParser};
77
use lazy_static::lazy_static;
@@ -42,10 +42,10 @@ pub fn move_files<P: StudentFilePolicy>(
4242
let target_path = target.join(&relative);
4343
if let Some(parent) = target_path.parent() {
4444
fs::create_dir_all(parent)
45-
.map_err(|e| Error::CreateDir(parent.to_path_buf(), e))?;
45+
.map_err(|e| TmcError::CreateDir(parent.to_path_buf(), e))?;
4646
}
4747
fs::rename(entry.path(), &target_path).map_err(|e| {
48-
Error::Rename(entry.path().to_path_buf(), target_path.to_path_buf(), e)
48+
TmcError::Rename(entry.path().to_path_buf(), target_path.to_path_buf(), e)
4949
})?;
5050
}
5151
}
@@ -113,7 +113,7 @@ fn copy_file<F: Fn(&MetaString) -> bool>(
113113
.unwrap_or_else(|_| Path::new(""));
114114
let dest_path = dest_root.join(&relative_path);
115115
if let Some(parent) = dest_path.parent() {
116-
fs::create_dir_all(parent).map_err(|e| Error::CreateDir(parent.to_path_buf(), e))?;
116+
fs::create_dir_all(parent).map_err(|e| TmcError::CreateDir(parent.to_path_buf(), e))?;
117117
}
118118
let extension = entry.path().extension().and_then(|e| e.to_str());
119119
let is_binary = extension
@@ -127,7 +127,7 @@ fn copy_file<F: Fn(&MetaString) -> bool>(
127127
dest_path
128128
);
129129
fs::copy(entry.path(), &dest_path)
130-
.map_err(|e| Error::FileCopy(entry.path().to_path_buf(), dest_path, e))?;
130+
.map_err(|e| TmcError::FileCopy(entry.path().to_path_buf(), dest_path, e))?;
131131
} else {
132132
// filter text files
133133
debug!(
@@ -136,11 +136,11 @@ fn copy_file<F: Fn(&MetaString) -> bool>(
136136
dest_path
137137
);
138138

139-
let source_file =
140-
File::open(entry.path()).map_err(|e| Error::OpenFile(entry.path().to_path_buf(), e))?;
139+
let source_file = File::open(entry.path())
140+
.map_err(|e| TmcError::OpenFile(entry.path().to_path_buf(), e))?;
141141

142142
let mut target_file = File::create(&dest_path)
143-
.map_err(|e| Error::CreateFile(entry.path().to_path_buf(), e))?;
143+
.map_err(|e| TmcError::CreateFile(entry.path().to_path_buf(), e))?;
144144

145145
let parser = MetaSyntaxParser::new(source_file, extension.unwrap_or_default());
146146

@@ -160,7 +160,7 @@ fn copy_file<F: Fn(&MetaString) -> bool>(
160160
// writes all lines
161161
target_file
162162
.write_all(&write_lines)
163-
.map_err(|e| Error::Write(dest_path, e))?;
163+
.map_err(|e| TmcError::Write(dest_path, e))?;
164164
}
165165
Ok(())
166166
}

tmc-langs-framework/src/io/zip.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains functions for zipping and unzipping projects.
22
33
use crate::policy::StudentFilePolicy;
4-
use crate::{Error, Result};
4+
use crate::{Result, TmcError};
55
use std::collections::HashSet;
66
use std::fs::{self, File};
77
use std::io::{Cursor, Read, Seek, Write};
@@ -32,15 +32,15 @@ pub fn zip(policy: Box<dyn StudentFilePolicy>, root_directory: &Path) -> Result<
3232
writer.add_directory_from_path(path, FileOptions::default())?;
3333
} else {
3434
let mut file = File::open(entry.path())
35-
.map_err(|e| Error::FileOpen(entry.path().to_path_buf(), e))?;
35+
.map_err(|e| TmcError::FileOpen(entry.path().to_path_buf(), e))?;
3636
let mut bytes = vec![];
3737
file.read_to_end(&mut bytes)
38-
.map_err(|e| Error::FileRead(entry.path().to_path_buf(), e))?;
38+
.map_err(|e| TmcError::FileRead(entry.path().to_path_buf(), e))?;
3939
log::trace!("writing file {}", path.display());
4040
writer.start_file_from_path(path, FileOptions::default())?;
4141
writer
4242
.write_all(&bytes)
43-
.map_err(|e| Error::Write(path.to_path_buf(), e))?;
43+
.map_err(|e| TmcError::Write(path.to_path_buf(), e))?;
4444
}
4545
}
4646
}
@@ -52,7 +52,7 @@ pub fn zip(policy: Box<dyn StudentFilePolicy>, root_directory: &Path) -> Result<
5252
pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Result<()> {
5353
log::debug!("Unzipping {} to {}", zip.display(), target.display());
5454

55-
let file = File::open(zip).map_err(|e| Error::OpenFile(zip.to_path_buf(), e))?;
55+
let file = File::open(zip).map_err(|e| TmcError::OpenFile(zip.to_path_buf(), e))?;
5656
let mut zip_archive = ZipArchive::new(file)?;
5757

5858
let project_dir = find_project_dir(&mut zip_archive)?;
@@ -76,17 +76,17 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
7676
if file.is_dir() {
7777
log::trace!("creating {:?}", path_in_target);
7878
fs::create_dir_all(&path_in_target)
79-
.map_err(|e| Error::CreateDir(path_in_target.clone(), e))?;
79+
.map_err(|e| TmcError::CreateDir(path_in_target.clone(), e))?;
8080
unzipped_paths.insert(
8181
path_in_target
8282
.canonicalize()
83-
.map_err(|e| Error::Canonicalize(path_in_target, e))?,
83+
.map_err(|e| TmcError::Canonicalize(path_in_target, e))?,
8484
);
8585
} else {
8686
let mut write = true;
8787
let mut file_contents = vec![];
8888
file.read_to_end(&mut file_contents)
89-
.map_err(|e| Error::FileRead(file_path, e))?;
89+
.map_err(|e| TmcError::FileRead(file_path, e))?;
9090
// always overwrite .tmcproject.yml
9191
if path_in_target.exists()
9292
&& !path_in_target
@@ -95,11 +95,11 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
9595
.unwrap_or_default()
9696
{
9797
let mut target_file = File::open(&path_in_target)
98-
.map_err(|e| Error::OpenFile(path_in_target.clone(), e))?;
98+
.map_err(|e| TmcError::OpenFile(path_in_target.clone(), e))?;
9999
let mut target_file_contents = vec![];
100100
target_file
101101
.read_to_end(&mut target_file_contents)
102-
.map_err(|e| Error::FileRead(path_in_target.clone(), e))?;
102+
.map_err(|e| TmcError::FileRead(path_in_target.clone(), e))?;
103103
if file_contents == target_file_contents
104104
|| (policy.is_student_file(&path_in_target, &target, &tmc_project_yml)?
105105
&& !policy.is_updating_forced(&path_in_target, &tmc_project_yml)?)
@@ -110,17 +110,18 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
110110
if write {
111111
log::trace!("writing to {}", path_in_target.display());
112112
if let Some(res) = path_in_target.parent() {
113-
fs::create_dir_all(res).map_err(|e| Error::CreateDir(res.to_path_buf(), e))?;
113+
fs::create_dir_all(res)
114+
.map_err(|e| TmcError::CreateDir(res.to_path_buf(), e))?;
114115
}
115116
let mut overwrite_target = File::create(&path_in_target)
116-
.map_err(|e| Error::CreateFile(path_in_target.clone(), e))?;
117+
.map_err(|e| TmcError::CreateFile(path_in_target.clone(), e))?;
117118
overwrite_target
118119
.write_all(&file_contents)
119-
.map_err(|e| Error::Write(path_in_target.clone(), e))?;
120+
.map_err(|e| TmcError::Write(path_in_target.clone(), e))?;
120121
unzipped_paths.insert(
121122
path_in_target
122123
.canonicalize()
123-
.map_err(|e| Error::Canonicalize(path_in_target, e))?,
124+
.map_err(|e| TmcError::Canonicalize(path_in_target, e))?,
124125
);
125126
}
126127
}
@@ -133,7 +134,7 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
133134
&entry
134135
.path()
135136
.canonicalize()
136-
.map_err(|e| Error::Canonicalize(entry.path().to_path_buf(), e))?,
137+
.map_err(|e| TmcError::Canonicalize(entry.path().to_path_buf(), e))?,
137138
) && (policy.is_updating_forced(entry.path(), &tmc_project_yml)?
138139
|| !policy.is_student_file(entry.path(), &project_dir, &tmc_project_yml)?)
139140
{
@@ -142,12 +143,12 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
142143
if WalkDir::new(entry.path()).max_depth(1).into_iter().count() == 1 {
143144
log::debug!("deleting empty directory {}", entry.path().display());
144145
fs::remove_dir(entry.path())
145-
.map_err(|e| Error::RemoveDir(entry.path().to_path_buf(), e))?;
146+
.map_err(|e| TmcError::RemoveDir(entry.path().to_path_buf(), e))?;
146147
}
147148
} else {
148149
log::debug!("removing file {}", entry.path().display());
149150
fs::remove_file(entry.path())
150-
.map_err(|e| Error::RemoveFile(entry.path().to_path_buf(), e))?;
151+
.map_err(|e| TmcError::RemoveFile(entry.path().to_path_buf(), e))?;
151152
}
152153
}
153154
}
@@ -191,7 +192,7 @@ fn find_project_dir<R: Read + Seek>(zip_archive: &mut ZipArchive<R>) -> Result<P
191192
return Ok(parent.to_path_buf());
192193
}
193194
}
194-
Err(Error::NoProjectDirInZip)
195+
Err(TmcError::NoProjectDirInZip)
195196
}
196197

197198
fn contains_tmcnosubmit(entry: &DirEntry) -> bool {

tmc-langs-framework/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod io;
66
pub mod plugin;
77
pub mod policy;
88

9-
pub use error::Error;
9+
pub use error::TmcError;
1010
pub use plugin::LanguagePlugin;
1111
pub use policy::StudentFilePolicy;
1212

@@ -15,7 +15,7 @@ use std::process::{Command, Output};
1515
use std::thread;
1616
use std::time::{Duration, Instant};
1717

18-
pub type Result<T> = std::result::Result<T, Error>;
18+
pub type Result<T> = std::result::Result<T, TmcError>;
1919

2020
pub struct CommandWithTimeout<'a>(pub &'a mut Command);
2121

@@ -28,21 +28,24 @@ impl CommandWithTimeout<'_> {
2828
match timeout {
2929
Some(timeout) => {
3030
// spawn process and init timer
31-
let mut child = self.0.spawn().map_err(|e| Error::CommandSpawn(name, e))?;
31+
let mut child = self
32+
.0
33+
.spawn()
34+
.map_err(|e| TmcError::CommandSpawn(name, e))?;
3235
let timer = Instant::now();
3336
loop {
34-
match child.try_wait().map_err(|e| Error::Process(e))? {
37+
match child.try_wait().map_err(|e| TmcError::Process(e))? {
3538
Some(_exit_status) => {
3639
// done, get output
3740
return child
3841
.wait_with_output()
39-
.map_err(|e| Error::CommandFailed(name, e));
42+
.map_err(|e| TmcError::CommandFailed(name, e));
4043
}
4144
None => {
4245
// still running, check timeout
4346
if timer.elapsed() > timeout {
44-
child.kill().map_err(|e| Error::Process(e))?;
45-
return Err(Error::TestTimeout(timer.elapsed()));
47+
child.kill().map_err(|e| TmcError::Process(e))?;
48+
return Err(TmcError::TestTimeout(timer.elapsed()));
4649
}
4750

4851
// TODO: gradually increase sleep duration?
@@ -52,7 +55,10 @@ impl CommandWithTimeout<'_> {
5255
}
5356
}
5457
// no timeout, block forever
55-
None => self.0.output().map_err(|e| Error::CommandFailed(name, e)),
58+
None => self
59+
.0
60+
.output()
61+
.map_err(|e| TmcError::CommandFailed(name, e)),
5662
}
5763
}
5864
}
@@ -67,7 +73,7 @@ mod test {
6773
let mut command = command.arg("1");
6874
let mut out = CommandWithTimeout(&mut command);
6975
let res = out.wait_with_timeout("sleep", Some(Duration::from_millis(100)));
70-
if let Err(Error::TestTimeout(_)) = res {
76+
if let Err(TmcError::TestTimeout(_)) = res {
7177
} else {
7278
panic!("unexpected result: {:?}", res);
7379
}

tmc-langs-framework/src/policy.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains StudentFilePolicy.
22
33
use super::{Result, TmcProjectYml};
4-
use crate::Error;
4+
use crate::TmcError;
55
use std::ffi::OsStr;
66
use std::path::{Path, PathBuf};
77

@@ -49,11 +49,11 @@ pub trait StudentFilePolicy {
4949
fn is_extra_student_file(&self, path: &Path, tmc_project_yml: &TmcProjectYml) -> Result<bool> {
5050
let absolute = path
5151
.canonicalize()
52-
.map_err(|e| Error::Canonicalize(path.to_path_buf(), e))?;
52+
.map_err(|e| TmcError::Canonicalize(path.to_path_buf(), e))?;
5353
for path in &tmc_project_yml.extra_exercise_files {
5454
let path = path
5555
.canonicalize()
56-
.map_err(|e| Error::Canonicalize(path.to_owned(), e))?;
56+
.map_err(|e| TmcError::Canonicalize(path.to_owned(), e))?;
5757

5858
if path.is_dir() && (path == absolute || absolute.starts_with(path)) {
5959
return Ok(true);
@@ -68,11 +68,11 @@ pub trait StudentFilePolicy {
6868
fn is_updating_forced(&self, path: &Path, tmc_project_yml: &TmcProjectYml) -> Result<bool> {
6969
let absolute = path
7070
.canonicalize()
71-
.map_err(|e| Error::Canonicalize(path.to_path_buf(), e))?;
71+
.map_err(|e| TmcError::Canonicalize(path.to_path_buf(), e))?;
7272
for force_update_path in &tmc_project_yml.force_update {
7373
let force_absolute = force_update_path
7474
.canonicalize()
75-
.map_err(|e| Error::Canonicalize(force_update_path.to_owned(), e))?;
75+
.map_err(|e| TmcError::Canonicalize(force_update_path.to_owned(), e))?;
7676
if (absolute == force_absolute || absolute.starts_with(&force_absolute))
7777
&& force_absolute.is_dir()
7878
{

tmc-langs-java/src/ant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::time::Duration;
1515
use tmc_langs_framework::{
1616
domain::{ExerciseDesc, RunResult},
1717
plugin::{Language, LanguagePlugin, ValidationResult},
18-
Error,
18+
TmcError,
1919
};
2020
use walkdir::WalkDir;
2121

@@ -82,7 +82,7 @@ impl LanguagePlugin for AntPlugin {
8282
self.run_checkstyle(&locale, path)
8383
}
8484

85-
fn scan_exercise(&self, path: &Path, exercise_name: String) -> Result<ExerciseDesc, Error> {
85+
fn scan_exercise(&self, path: &Path, exercise_name: String) -> Result<ExerciseDesc, TmcError> {
8686
if !Self::is_exercise_type_correct(path) {
8787
return JavaError::InvalidExercise(path.to_path_buf()).into();
8888
}
@@ -95,7 +95,7 @@ impl LanguagePlugin for AntPlugin {
9595
&self,
9696
project_root_path: &Path,
9797
_timeout: Option<Duration>,
98-
) -> Result<RunResult, Error> {
98+
) -> Result<RunResult, TmcError> {
9999
Ok(self.run_java_tests(project_root_path)?)
100100
}
101101

@@ -108,11 +108,11 @@ impl LanguagePlugin for AntPlugin {
108108
AntStudentFilePolicy::new(project_path.to_path_buf())
109109
}
110110

111-
fn maybe_copy_shared_stuff(&self, dest_path: &Path) -> Result<(), Error> {
111+
fn maybe_copy_shared_stuff(&self, dest_path: &Path) -> Result<(), TmcError> {
112112
Ok(self.copy_tmc_junit_runner(dest_path)?)
113113
}
114114

115-
fn clean(&self, path: &Path) -> Result<(), Error> {
115+
fn clean(&self, path: &Path) -> Result<(), TmcError> {
116116
log::debug!("Cleaning project at {}", path.display());
117117

118118
let stdout_path = path.join("build_log.txt");

0 commit comments

Comments
 (0)