-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'assert' module and assertion errors
- Loading branch information
Showing
5 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Assertion functions | ||
|
||
use std::path::Path; | ||
|
||
use crate::error::{ | ||
assert::{AssertionError, AssertionErrorType}, | ||
Error, Throwable, | ||
}; | ||
|
||
/// Asserts that a path is relative | ||
/// # Arguments | ||
/// * `path` - The path to check | ||
/// # Returns | ||
/// The path or an assertion error | ||
#[macro_export] | ||
macro_rules! assert_relative { | ||
($path: expr) => { | ||
$crate::assert::assert_relative_raw($path, || (line!(), file!())) | ||
}; | ||
} | ||
|
||
/// Asserts that a path is relative | ||
/// | ||
/// Consider using the `assert_relative!()` macro | ||
/// # Arguments | ||
/// * `path` - The path to check | ||
/// * `callback` - The callback to provide the following tuple: (line, file) | ||
/// # Returns | ||
/// The path or an assertion error | ||
pub fn assert_relative_raw<'a, F: Fn() -> (u32, &'a str)>( | ||
path: &'a Path, | ||
callback: F, | ||
) -> Result<&'a Path, Error> { | ||
if path.is_relative() { | ||
Ok(path) | ||
} else { | ||
let info = callback(); | ||
let error = AssertionError { | ||
error: AssertionErrorType::RelativePath(path.to_owned()), | ||
line: info.0, | ||
file: info.1.to_string(), | ||
}; | ||
Err(error.throw(format!( | ||
"Asserting '{}' is relative", | ||
path.to_string_lossy() | ||
))) | ||
} | ||
} | ||
|
||
/// Asserts that a path is absolute | ||
/// # Arguments | ||
/// * `path` - The path to check | ||
/// # Returns | ||
/// The path or an assertion error | ||
#[macro_export] | ||
macro_rules! assert_absolute { | ||
($path: expr) => { | ||
$crate::assert::assert_absolute_raw($path, || (line!(), file!())) | ||
}; | ||
} | ||
|
||
/// Asserts that a path is absolute | ||
/// | ||
/// Consider using the `assert_absolute!()` macro | ||
/// # Arguments | ||
/// * `path` - The path to check | ||
/// * `callback` - The callback to provide the following tuple: (line, file) | ||
/// # Returns | ||
/// The path or an assertion error | ||
pub fn assert_absolute_raw<'a, F: Fn() -> (u32, &'a str)>( | ||
path: &'a Path, | ||
callback: F, | ||
) -> Result<&'a Path, Error> { | ||
if path.is_absolute() { | ||
Ok(path) | ||
} else { | ||
let info = callback(); | ||
let error = AssertionError { | ||
error: AssertionErrorType::AbsolutePath(path.to_owned()), | ||
line: info.0, | ||
file: info.1.to_string(), | ||
}; | ||
Err(error.throw(format!( | ||
"Asserting '{}' is absolute", | ||
path.to_string_lossy() | ||
))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Assertion errors | ||
use std::path::PathBuf; | ||
|
||
use crate::GIT_COMMIT_HASH; | ||
|
||
/// An error for an assertion, includes the path to where the assertion has been made | ||
#[derive(Debug)] | ||
pub struct AssertionError { | ||
/// The error | ||
pub error: AssertionErrorType, | ||
/// The line number where the assertion has been made | ||
pub line: u32, | ||
/// The file where the assertion has been made | ||
pub file: String, | ||
} | ||
|
||
/// All the possible assertion errors | ||
#[derive(Debug)] | ||
pub enum AssertionErrorType { | ||
/// A path was expected to be relative | ||
RelativePath(PathBuf), | ||
/// A path was expected to be absolute | ||
AbsolutePath(PathBuf), | ||
} | ||
|
||
impl std::fmt::Display for AssertionError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let msg = match &self.error { | ||
AssertionErrorType::RelativePath(path) => { | ||
format!("Expected '{}' to be relative", path.to_string_lossy()) | ||
} | ||
AssertionErrorType::AbsolutePath(path) => { | ||
format!("Expected '{}' to be absolute", path.to_string_lossy()) | ||
} | ||
}; | ||
|
||
let msg = format!( | ||
"[GIT: {}] {}:{} - {}", | ||
GIT_COMMIT_HASH, self.file, self.line, msg | ||
); | ||
let len = msg.len(); | ||
|
||
writeln!(f, "{}", msg)?; | ||
|
||
writeln!(f, "!!!!{}!!!!", "!".repeat(len))?; | ||
writeln!( | ||
f, | ||
"!! {:^width$} !!", | ||
"ASSERTION ERROR - THIS IS A BUG!", | ||
width = len | ||
)?; | ||
writeln!( | ||
f, | ||
"!! {:^width$} !!", | ||
"Please report this error to", | ||
width = len | ||
)?; | ||
writeln!( | ||
f, | ||
"!! {:^width$} !!", | ||
"https://github.com/AcaciaLinux/tooling/issues", | ||
width = len | ||
)?; | ||
writeln!(f, "!! {:^width$} !!", msg, width = len)?; | ||
write!(f, "!!!!{}!!!!", "!".repeat(len)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters