Skip to content

Commit e5b042b

Browse files
committed
rust: transform module error clean up
1 parent cace846 commit e5b042b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

rust/bear/src/semantic/transformation.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ pub trait Transformation: Send {
1717

1818
#[derive(Debug, Error)]
1919
pub enum Error {
20-
#[error("Transformation error: {0}")]
21-
Transformation(String),
22-
#[error("IO error: {0}")]
23-
IO(#[from] io::Error),
24-
#[error("Path manipulation error: {0}")]
25-
Path(#[from] path::StripPrefixError),
20+
#[error("Path canonicalize failed: {0}")]
21+
PathCanonicalize(#[from] io::Error),
22+
#[error("Path {0} can't be relative to {1}")]
23+
PathsCannotBeRelative(path::PathBuf, path::PathBuf),
2624
#[error("Configuration instructed to filter out")]
2725
FilteredOut,
2826
}
2927

3028
#[derive(Debug, Error)]
3129
pub enum ConfigurationError {
32-
#[error("Initialisation error: {0}")]
33-
Initialisation(#[from] io::Error),
3430
#[error("'Never' or 'Conditional' can't be used after 'Always' for path {0:?}")]
3531
AfterAlways(path::PathBuf),
3632
#[error("'Never' can't be used after 'Conditional' for path {0:?}")]
@@ -51,6 +47,8 @@ pub enum ConfigurationError {
5147
NeverWithArguments(path::PathBuf),
5248
#[error("Only relative paths for 'file' and 'output' when 'directory' is relative.")]
5349
OnlyRelativePaths,
50+
#[error("Getting current directory failed: {0}")]
51+
CurrentWorkingDirectory(#[from] io::Error),
5452
}
5553

5654
/// FilterAndFormat is a transformation that filters and formats the compiler calls.
@@ -197,12 +195,26 @@ mod formatter {
197195
// Count remaining components in the root to determine how many `..` are needed
198196
let mut result = PathBuf::new();
199197
for _ in remaining_root_components {
200-
result.push("..");
198+
result.push(path::Component::ParentDir);
201199
}
202200

203201
// Add the remaining components of the path
204202
for comp in remaining_path_components {
205-
result.push(comp);
203+
// if comp is a Prefix or RootDir, signal error
204+
match comp {
205+
path::Component::Normal(_) | path::Component::ParentDir => {
206+
result.push(comp);
207+
}
208+
path::Component::CurDir => {
209+
// Ignore this (should not happen since we are working with absolute paths)
210+
}
211+
_ => {
212+
return Err(Error::PathsCannotBeRelative(
213+
path.to_path_buf(),
214+
root.to_path_buf(),
215+
));
216+
}
217+
}
206218
}
207219

208220
Ok(result)

0 commit comments

Comments
 (0)