use std::{error::Error, fmt::Display};
use super::preprocessor::PreprocessorError;
#[derive(Debug, Clone)]
pub enum OptLevel {
None,
Speed,
Size,
O1,
O2,
O3,
Custom(String),
}
impl OptLevel {
pub fn as_stanard_opt_char(&self) -> String {
match self {
OptLevel::None => "0",
OptLevel::Speed => "fast",
OptLevel::Size => "z",
OptLevel::O1 => "1",
OptLevel::O2 => "2",
OptLevel::O3 => "3",
OptLevel::Custom(c) => c,
}
.to_string()
}
}
pub fn check_program_installed(program: &str) -> Result<(), CompilationError> {
if which::which(program).is_err() {
Err(CompilationError::ProgramNotInstalled(program.to_string()))
} else {
Ok(())
}
}
#[derive(Debug)]
pub enum CompilationError {
IoError(std::io::Error),
CompilationFailed(String),
ProgramNotInstalled(String),
FeatureNotSupported(String),
PreprocessorError(PreprocessorError),
}
impl From<std::io::Error> for CompilationError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
pub type CompilationResult<T> = Result<T, CompilationError>;
impl Display for CompilationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompilationError::IoError(e) => write!(f, "IO error: {}", e),
CompilationError::CompilationFailed(e) => write!(f, "Compilation failed: {}", e),
CompilationError::ProgramNotInstalled(e) => write!(f, "Program not installed: {}", e),
CompilationError::FeatureNotSupported(e) => write!(f, "Feature not supported: {}", e),
CompilationError::PreprocessorError(e) => write!(f, "Preprocessor error: {:?}", e),
}
}
}
impl Error for CompilationError {}
impl From<PreprocessorError> for CompilationError {
fn from(e: PreprocessorError) -> Self {
Self::PreprocessorError(e)
}
}