cli/commands/
mod.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use runtime::cache::Cache;
8use runtime::config::{CONFIG, Config, LogLevel};
9
10use crate::{Cli, Command};
11
12mod cache;
13mod eval;
14mod repl;
15mod run;
16
17pub(crate) async fn handle_command(cli: Cli) {
18	match cli.command {
19		Some(Command::Cache { clear }) => {
20			if !clear {
21				cache::cache_statistics();
22			} else if let Some(cache) = Cache::new()
23				&& let Err(err) = cache.clear()
24			{
25				eprintln!("{err}");
26			}
27		}
28
29		Some(Command::Eval { source }) => {
30			CONFIG.set(Config::default().log_level(LogLevel::Debug).script(true)).unwrap();
31			eval::eval_source(&source).await;
32		}
33
34		Some(Command::Run { path, log_level, debug, script }) => {
35			let log_level = if debug {
36				LogLevel::Debug
37			} else {
38				match log_level.to_uppercase().as_str() {
39					"NONE" => LogLevel::None,
40					"INFO" => LogLevel::Info,
41					"WARN" => LogLevel::Warn,
42					"ERROR" => LogLevel::Error,
43					"DEBUG" => LogLevel::Debug,
44					_ => panic!("Invalid Logging Level"),
45				}
46			};
47
48			CONFIG.set(Config::default().log_level(log_level).script(script)).unwrap();
49			run::run(&path).await;
50		}
51
52		Some(Command::Repl) | None => {
53			CONFIG.set(Config::default().log_level(LogLevel::Debug).script(true)).unwrap();
54			repl::start_repl().await;
55		}
56	}
57}