Skip to content

Commit

Permalink
added (but not implemented) safe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jul 10, 2024
1 parent de9e10d commit 88efd81
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample.hexo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
> #eval(#read_file('samples/java_object/input.hex'))
> #eval(#read_file('samples/java_object/input.hexo'))

56 changes: 47 additions & 9 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ pub(crate) struct Cli {

#[arg(short, long, default_value_t = LogLevel::Info)]
log_level: LogLevel,

#[arg(short, long, default_value_t = false)]
safe: bool,
}

#[derive(Copy, Clone, Debug)]
struct CliCompilerArguments {
safe_mode: bool,
}

impl Cli {
Expand All @@ -57,16 +65,35 @@ impl Cli {

let cli = Cli::parse();

logger::HexoLogger::set_level(cli.log_level);
logger::HexoLogger::set_level(&cli.log_level);

cli.log_debug_interface_arguments();

let compiler_arguments = cli.cli_compiler_arguments();
let cli_result: Result<_, Error> = match cli.command {
None => Err(Error::UnknownCommand),
Some(Commands::Watch { source, output }) => Self::watch(source, output),
Some(Commands::Build { source, output }) => Self::build(source, output),
Some(Commands::Watch { source, output }) => Self::watch(source, output, compiler_arguments),
Some(Commands::Build { source, output }) => Self::build(source, output, compiler_arguments),
};

Self::handle_cli_error_if_required(cli_result, build_started);
}

fn cli_compiler_arguments(&self) -> CliCompilerArguments {
CliCompilerArguments {
safe_mode: self.safe
}
}

fn log_debug_interface_arguments(&self) {
logger::debug!("initialized cli interface with arguments:\
\n --log-level = {}\
\n --safe = {}",
&self.log_level,
&self.safe
);
}

fn handle_cli_error_if_required(
cli_result: Result<(), Error>,
build_started: Instant,
Expand All @@ -88,12 +115,12 @@ impl Cli {
logger::error!("{error}");
}

fn watch(source: String, output: Option<String>) -> Result<(), Error> {
fn watch(source: String, output: Option<String>, compiler_arguments: CliCompilerArguments) -> Result<(), Error> {
let source_path_clone = source.clone();
let source_path = source_path_clone.as_ref();

let mut watcher = notify::recommended_watcher(move |event: Result<Event, _>| {
Self::watch_loop(source.clone(), output.clone(), event)
Self::watch_loop(source.clone(), output.clone(), compiler_arguments, event)
})
.map_err(Error::FileWatcher)?;

Expand All @@ -108,12 +135,17 @@ impl Cli {
Ok(())
}

fn watch_loop(source: String, output: Option<String>, event: Result<Event, notify::Error>) {
fn watch_loop(
source: String,
output: Option<String>,
compiler_arguments: CliCompilerArguments,
event: Result<Event, notify::Error>,
) {
match event {
Ok(e) => {
if let Modify(ModifyKind::Data(_)) = e.kind {
logger::debug!("rebuilding...");
let _ = catch_unwind(|| Self::build(source.clone(), output.clone()));
let _ = catch_unwind(|| Self::build(source.clone(), output.clone(), compiler_arguments));
logger::debug!(" done!");
}
}
Expand All @@ -123,11 +155,17 @@ impl Cli {
}
}

pub(crate) fn build(source: String, output: Option<String>) -> Result<(), Error> {
pub(crate) fn build(
source: String,
output: Option<String>,
compiler_arguments: CliCompilerArguments
) -> Result<(), Error> {
defer!(logger::debug!("BUILDING, done"));
logger::debug!("BUILDING, source: {}, output: {:?}", source, output);

let context = HexoCompilerContext::new();
let context = HexoCompilerContext::new(
compiler_arguments.safe_mode
);
let compiler = HexoCompiler::new(context);

let source_path = Path::new(&source);
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/compiler_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pub(crate) struct HexoCompilerContext {}
pub(crate) struct HexoCompilerContext {
safe_mode: bool
}

impl HexoCompilerContext {
pub(crate) fn new() -> Self {
HexoCompilerContext {}
pub(crate) fn new(safe_mode: bool) -> Self {
HexoCompilerContext {
safe_mode
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/hexo_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod benchmarks {
#[bench]
fn compilation(b: &mut Bencher) {
let compiler = HexoCompiler::new(
HexoCompilerContext::new()
HexoCompilerContext::new(false)
);

let source = EagerCompilerSource::new(
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod test {
Cli::build(
input_file_path,
Some(actual_file_path.to_string_lossy().to_string()),
,
).unwrap();

let mut expected_file = File::open(expected_file_path).unwrap();
Expand Down
48 changes: 18 additions & 30 deletions src/util/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ impl HexoLogger {
&self.level
}

pub(crate) fn set_level(level: LogLevel) {
INSTANCE.lock().unwrap().level = level;
pub(crate) fn set_level(level: &LogLevel) {
INSTANCE.lock().unwrap().level = level.clone();
}

pub(crate) fn error(&self, message: &str) {
eprintln!("{}", style(message).red());
pub(crate) fn error(&self, location: &str, message: &str) {
eprintln!("{} {}", style(location).blue(), style(message).red());
}

pub(crate) fn info(&self, message: &str) {
eprintln!("{message}");
pub(crate) fn info(&self, location: &str, message: &str) {
eprintln!("{} {}", style(location).blue(), style(message).blue());
}

pub(crate) fn debug(&self, message: &str) {
println!("{}", style(message).dim());
pub(crate) fn debug(&self, location: &str, message: &str) {
println!("{} {}", style(location).blue().dim(), style(message).dim());
}

pub(crate) fn warn(&self, message: &str) {
println!("{}", style(message).yellow());
pub(crate) fn warn(&self, location: &str, message: &str) {
println!("{} {}", style(location).blue(), style(message).yellow());
}

pub(crate) fn output(&self, message: &str) {
Expand All @@ -84,11 +84,8 @@ macro_rules! debug {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Debug {
instance.debug(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
module_path!(),
format!($($arg)*).as_str()
);
}
}
Expand All @@ -101,11 +98,8 @@ macro_rules! error {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Error {
instance.error(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
module_path!(),
format!($($arg)*).as_str()
);
}
}
Expand All @@ -118,11 +112,8 @@ macro_rules! info {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Info {
instance.info(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
module_path!(),
format!($($arg)*).as_str()
);
}
}
Expand All @@ -135,11 +126,8 @@ macro_rules! warning {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Warn {
instance.warn(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
module_path!(),
format!($($arg)*).as_str()
);
}
}
Expand Down

0 comments on commit 88efd81

Please sign in to comment.