Fixed module system
This commit is contained in:
@@ -9,27 +9,27 @@ pub class Module {
|
||||
pub PathBuf path;
|
||||
pub Vec<Self> children;
|
||||
|
||||
pub constructor(String name, PathBuf mist_path, PathBuf rust_path) {
|
||||
pub constructor(PathBuf mist_path) {
|
||||
self.children = new Vec();
|
||||
self.name = name;
|
||||
self.name = mist_path.file_stem()
|
||||
.and_then(|var s| s.to_str())
|
||||
.expect("Failed to get module name")
|
||||
.to_string();
|
||||
self.path = mist_path;
|
||||
|
||||
if (self.path.is_dir()) {
|
||||
PathBuf dir = self.path.clone();
|
||||
|
||||
// Behavior 2: It's a directory node. Check for gateway package.mist first.
|
||||
PathBuf pkg_gate = self.path.join("package.mist");
|
||||
if (pkg_gate.exists()) {
|
||||
self.path = pkg_gate;
|
||||
}
|
||||
|
||||
self.scan_directory(&dir, &rust_path);
|
||||
self.scan_directory(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
void scan_directory(self mut*, Path* search_dir, Path* rust_path) {
|
||||
fs::create_dir_all(rust_path).expect("Failed to create dir");
|
||||
|
||||
void scan_directory(self mut*, Path* search_dir) {
|
||||
var entries = match (fs::read_dir(search_dir)) {
|
||||
Ok[entries] => entries;
|
||||
Err[e] => {
|
||||
@@ -53,28 +53,7 @@ pub class Module {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!child_path.is_dir() && child_path.extension().and_then(|var v| v.to_str()) != Some("mist")) {
|
||||
var file_name = child_path.file_name()
|
||||
.and_then(|var s| s.to_str())
|
||||
.expect("Failed to get file name");
|
||||
|
||||
fs::copy(&child_path, rust_path.join(&file_name)).expect("Failed to copy SideFile");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
String child_name = child_path.file_stem()
|
||||
.and_then(|var s| s.to_str())
|
||||
.expect("Failed to get module name")
|
||||
.to_string();
|
||||
|
||||
PathBuf child_output = rust_path.join(&child_name).with_extension("rs");
|
||||
|
||||
if (should_skip(&child_path, &child_output)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
self.children.push(new Module(child_name, child_path, child_output));
|
||||
self.children.push(new Module(child_path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,23 +74,13 @@ pub class Module {
|
||||
}
|
||||
}
|
||||
|
||||
pub Module master_package(PathBuf mist_path, PathBuf rust_path) {
|
||||
String name = mist_path.file_stem()
|
||||
.and_then(|var s| s.to_str())
|
||||
.expect("Failed to get module name")
|
||||
.to_string();
|
||||
pub Module master_package(PathBuf mist_path) {
|
||||
var mut root_mod = new Module(mist_path.clone());
|
||||
|
||||
var mut root_mod = new Module(name, mist_path.clone(), rust_path.clone());
|
||||
|
||||
if (!mist_path.is_dir()) {
|
||||
Path* parent_dir = mist_path.parent()
|
||||
.expect("Failed to get parent directory of root file");
|
||||
|
||||
Path* rust_path = rust_path.parent()
|
||||
.expect("Failed to get parent directory of rust path");
|
||||
|
||||
root_mod.scan_directory(parent_dir, rust_path);
|
||||
}
|
||||
root_mod.scan_directory(parent_dir);
|
||||
|
||||
root_mod
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ pub PathBuf transpile() {
|
||||
PathBuf src = root.join("src");
|
||||
PathBuf out = root.join(".mist/src");
|
||||
|
||||
var master = modules::master_package(src.join("main.mist"), out.join("main.rs"));
|
||||
var master = modules::master_package(src.join("main.mist"));
|
||||
|
||||
master.transpile(&out);
|
||||
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
use <super::task>;
|
||||
use <super::logger>;
|
||||
use <super::event>;
|
||||
|
||||
pub class Application {
|
||||
String name;
|
||||
|
||||
Vec<task::Task> tasks;
|
||||
Vec<String> plugins;
|
||||
|
||||
logger::Logger logger;
|
||||
|
||||
bool running;
|
||||
i32 ticks;
|
||||
|
||||
pub constructor(str* name) {
|
||||
self.name = name.to_string();
|
||||
self.tasks = new Vec();
|
||||
self.plugins = new Vec();
|
||||
|
||||
self.logger = new logger::Logger("app");
|
||||
|
||||
self.running = false;
|
||||
self.ticks = 0;
|
||||
}
|
||||
|
||||
pub void boot(self mut*) {
|
||||
self.logger.info("booting application");
|
||||
|
||||
self.running = true;
|
||||
|
||||
self.emit(event::Event::Startup);
|
||||
}
|
||||
|
||||
pub void run(self mut*) {
|
||||
while(self.running) {
|
||||
self.tick();
|
||||
|
||||
if(self.ticks >= 5) {
|
||||
self.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tick(self mut*) {
|
||||
self.logger.info("tick");
|
||||
|
||||
self.ticks++;
|
||||
|
||||
var task = task::create_task("background-job");
|
||||
|
||||
self.tasks.push(task);
|
||||
|
||||
println!("tasks: {}", self.tasks.len());
|
||||
}
|
||||
|
||||
pub void shutdown(self mut*) {
|
||||
self.emit(event::Event::Shutdown);
|
||||
|
||||
self.logger.warning("shutdown");
|
||||
|
||||
self.running = false;
|
||||
}
|
||||
|
||||
pub void register_plugin(self mut*, String name) {
|
||||
self.plugins.push(name.clone());
|
||||
|
||||
self.emit(event::Event::PluginLoaded(name));
|
||||
}
|
||||
|
||||
void emit(self*, event::Event event) {
|
||||
match(event) {
|
||||
event::Event::Startup => {
|
||||
self.logger.info("startup event");
|
||||
}
|
||||
|
||||
event::Event::Shutdown => {
|
||||
self.logger.info("shutdown event");
|
||||
}
|
||||
|
||||
event::Event::PluginLoaded[name] => {
|
||||
println!("plugin loaded: {}", name);
|
||||
}
|
||||
|
||||
event::Event::TaskExecuted[name] => {
|
||||
println!("task executed: {}", name);
|
||||
}
|
||||
|
||||
event::Event::Error[message] => {
|
||||
self.logger.error(&message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i32 test_stmt() {
|
||||
if (0 == 0) {
|
||||
0
|
||||
}
|
||||
else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
void loops_test() {
|
||||
for (x : 0..10) {
|
||||
println!("{x}");
|
||||
}
|
||||
|
||||
for (var mut i = 0; i < 10; i++) {
|
||||
println!("{i}");
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while (i == 1) {
|
||||
println!("hi");
|
||||
}
|
||||
|
||||
loop {
|
||||
println!("loop!");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
pub enum Event {
|
||||
Startup,
|
||||
Shutdown,
|
||||
TaskExecuted[String],
|
||||
PluginLoaded[String],
|
||||
Error[String],
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
use <std::fmt::Formatter>;
|
||||
use <std::fmt>;
|
||||
|
||||
pub trait TestLogging<T: fmt::Display>: fmt::Display {
|
||||
String get(self*);
|
||||
}
|
||||
|
||||
pub enum LogLevel {
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
||||
match (self) {
|
||||
LogLevel::Info => write!(f, "[INFO]");
|
||||
|
||||
LogLevel::Warning => write!(f, "[WARN]");
|
||||
|
||||
LogLevel::Error => write!(f, "[ERROR]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TestLogging<LogLevel> for LogLevel {
|
||||
String get(self*) {
|
||||
return format!("{self}");
|
||||
}
|
||||
}
|
||||
|
||||
pub class Logger {
|
||||
String prefix;
|
||||
|
||||
pub constructor(str* prefix) {
|
||||
self.prefix = prefix.to_string();
|
||||
}
|
||||
|
||||
pub void info(self*, str* message) {
|
||||
self.log(LogLevel::Info, message);
|
||||
}
|
||||
|
||||
pub void warning(self*, str* message) {
|
||||
self.log(LogLevel::Warning, message);
|
||||
}
|
||||
|
||||
pub void error(self*, str* message) {
|
||||
self.log(LogLevel::Error, message);
|
||||
}
|
||||
|
||||
void log(self*, LogLevel level, str* message) {
|
||||
match(level) {
|
||||
LogLevel::Info => {
|
||||
println!("{level} {} {}", self.prefix, message);
|
||||
}
|
||||
|
||||
LogLevel::Warning => {
|
||||
println!("{} {} {}", level.get(), self.prefix, message);
|
||||
}
|
||||
|
||||
LogLevel::Error => {
|
||||
println!("{level} {} {}", self.prefix, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display {
|
||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
||||
write!(f, "logger ({})", self.prefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-20
@@ -1,22 +1,3 @@
|
||||
void main() {
|
||||
var mut app_instance = new app::Application("mist-app");
|
||||
|
||||
app_instance.boot();
|
||||
app_instance.run();
|
||||
}
|
||||
|
||||
void do_stuff() {
|
||||
vec!(1, 2, 3).into_iter().map(Option<i32> |var v| {Some(v)}).collect::<Vec<_>>();
|
||||
|
||||
if (43==69) testing1();
|
||||
}
|
||||
|
||||
i32 testing1() {
|
||||
|
||||
i32 v = if (0 == 0) {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
v
|
||||
test_package::hello();
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
pub struct PluginInfo {
|
||||
pub String name,
|
||||
pub String version,
|
||||
}
|
||||
|
||||
pub class PluginRegistry<T> {
|
||||
T plugins;
|
||||
|
||||
pub constructor(T plugins) {
|
||||
self.plugins = plugins;
|
||||
}
|
||||
|
||||
pub T plugins(self) {
|
||||
return self.plugins;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
pub enum TaskState {
|
||||
Pending,
|
||||
Running,
|
||||
Finished,
|
||||
Failed,
|
||||
}
|
||||
|
||||
pub struct Task {
|
||||
pub String name,
|
||||
pub TaskState state,
|
||||
pub i32 executions,
|
||||
}
|
||||
|
||||
pub Task create_task(str* name) {
|
||||
Task {
|
||||
name: name.to_string(),
|
||||
state: TaskState::Pending,
|
||||
executions: 0,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub void hello() {
|
||||
println!("Hello")
|
||||
}
|
||||
Reference in New Issue
Block a user