-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
𧩠Implement PhinkEnv enum for environment variables
Refactored environment variable management by introducing the `PhinkEnv` enum, simplifying and centralizing the handling of key environment variables. Additionally, moved the error formatting function to a more appropriate module and streamlined various function calls to use the `PhinkEnv` enum. Signed-off-by: Kevin Valerio <kevin@srlabs.de>
- Loading branch information
1 parent
02ea402
commit 3719dbe
Showing
8 changed files
with
144 additions
and
77 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
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,52 @@ | ||
use std::fmt::Display; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum PhinkEnv { | ||
FuzzingWithConfig, | ||
FromZiggy, | ||
CargoManifestDir, | ||
AflForkServerTimeout, | ||
AflDebug, | ||
AflLLVMAllowList, | ||
} | ||
|
||
impl Display for PhinkEnv { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str(match self { | ||
PhinkEnv::FuzzingWithConfig => "PHINK_START_FUZZING_WITH_CONFIG", | ||
PhinkEnv::FromZiggy => "PHINK_FROM_ZIGGY", | ||
PhinkEnv::CargoManifestDir => "CARGO_MANIFEST_DIR", | ||
PhinkEnv::AflForkServerTimeout => "AFL_FORKSRV_INIT_TMOUT", | ||
PhinkEnv::AflDebug => "AFL_DEBUG", | ||
PhinkEnv::AflLLVMAllowList => "AFL_LLVM_ALLOWLIST", | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_phink_env_display() { | ||
assert_eq!( | ||
PhinkEnv::FuzzingWithConfig.to_string(), | ||
"PHINK_START_FUZZING_WITH_CONFIG" | ||
); | ||
assert_eq!(PhinkEnv::FromZiggy.to_string(), "PHINK_FROM_ZIGGY"); | ||
assert_eq!(PhinkEnv::CargoManifestDir.to_string(), "CARGO_MANIFEST_DIR"); | ||
} | ||
|
||
#[test] | ||
fn test_phink_env_debug() { | ||
assert_eq!( | ||
format!("{:?}", PhinkEnv::FuzzingWithConfig), | ||
"StartFuzzingWithConfig" | ||
); | ||
assert_eq!(format!("{:?}", PhinkEnv::FromZiggy), "FromZiggy"); | ||
assert_eq!( | ||
format!("{:?}", PhinkEnv::CargoManifestDir), | ||
"CargoManifestDir" | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,37 @@ | ||
use colored::Colorize; | ||
use std::backtrace::BacktraceStatus; | ||
|
||
pub mod config; | ||
pub mod env; | ||
pub mod ui; | ||
pub mod ziggy; | ||
|
||
/// Prints `anyhow` errors correctly in the Phink style | ||
/// | ||
/// # Argument | ||
/// * `e`: The `anyhow::Error` to pretty-print | ||
/// | ||
/// returns: `String` | ||
/// | ||
/// # Example | ||
/// `eprintln!("{}", format_error(e));` | ||
pub fn format_error(e: anyhow::Error) -> String { | ||
let mut message = format!("\n{}: {}\n", "Phink got an error...".red().bold(), e); | ||
|
||
if e.backtrace().status() == BacktraceStatus::Captured { | ||
message = format!( | ||
"{}\n{}\n{}\n", | ||
message, | ||
"Backtrace ->".yellow(), | ||
e.backtrace() | ||
) | ||
} | ||
|
||
let mut source = e.source(); | ||
while let Some(cause) = source { | ||
message = format!("{}\n\n{}: {}", message, "Caused by".cyan().bold(), cause); | ||
source = cause.source(); | ||
} | ||
|
||
message | ||
} |
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
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
Oops, something went wrong.