Update api bootstrap

This commit is contained in:
2026-06-12 19:17:34 +02:00
parent a31f6633fe
commit 919447875a
4 changed files with 46 additions and 42 deletions
+12 -9
View File
@@ -1,3 +1,5 @@
pub module builder;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::fs; use std::fs;
@@ -47,11 +49,11 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
let mist_src = format!(".mist{MAIN_SEPARATOR}src"); let mist_src = format!(".mist{MAIN_SEPARATOR}src");
for (message : cargo_metadata::Message::parse_stream(&mut reader)) { for message in cargo_metadata::Message::parse_stream(&mut reader) {
match (message) { match message {
Ok[Message::CompilerMessage[msg]] => { Ok[Message::CompilerMessage[msg]] => {
for (span : &msg.message.spans) { for span in &msg.message.spans {
if (span.is_primary) { if span.is_primary {
let rust_path = root.join(&span.file_name); let rust_path = root.join(&span.file_name);
let mist_file = span let mist_file = span
@@ -63,7 +65,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
let mist_path = root.join(&mist_file); let mist_path = root.join(&mist_file);
if (!fs::exists(&mist_path).expect("Unable to check if mist file exists")) { if !fs::exists(&mist_path).expect("Unable to check if mist file exists") {
diagnostics.push(MistDiagnostic::Rust(msg)); diagnostics.push(MistDiagnostic::Rust(msg));
break; break;
} }
@@ -85,7 +87,8 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
msg.message.message, msg.message.message,
span.label.clone().unwrap_or_default() span.label.clone().unwrap_or_default()
), ),
file_name: if (is_root) { file_name: if is_root {
(); // TODO: fix this
mist_file mist_file
} else { } else {
mist_path.to_string_lossy().to_string() mist_path.to_string_lossy().to_string()
@@ -95,7 +98,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
column: column column: column
}; };
match (msg.message.level) { match msg.message.level {
cargo_metadata::diagnostic::DiagnosticLevel::Error => { cargo_metadata::diagnostic::DiagnosticLevel::Error => {
diagnostics.push(MistDiagnostic::Error(mist_msg)) diagnostics.push(MistDiagnostic::Error(mist_msg))
} }
@@ -136,8 +139,8 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
pub fn print_diagnostics(diagnostics *Vec<MistDiagnostic>) { pub fn print_diagnostics(diagnostics *Vec<MistDiagnostic>) {
let mut files = HashMap::new(); let mut files = HashMap::new();
for (diag : diagnostics) { for diag in diagnostics {
match (diag) { match diag {
MistDiagnostic::Error[msg] => { MistDiagnostic::Error[msg] => {
let line = get_line(&mut files, &msg); let line = get_line(&mut files, &msg);
-3
View File
@@ -1,3 +0,0 @@
pub mod builder;
pub mod transpiler;
pub mod modules;
+12 -10
View File
@@ -1,3 +1,5 @@
pub module modules;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::fs; use std::fs;
@@ -9,7 +11,7 @@ use crate::transpiler::MistConfig;
pub class Module { pub class Module {
pub name String; pub name String;
pub path PathBuf; pub path PathBuf;
pub children Vec<Box<Module>>; pub children Vec<Module>;
pub constructor(mist_path PathBuf, config *MistConfig) { pub constructor(mist_path PathBuf, config *MistConfig) {
self.children = Vec::new(); self.children = Vec::new();
@@ -19,16 +21,16 @@ pub class Module {
.to_string(); .to_string();
self.path = mist_path; self.path = mist_path;
if (self.path.is_dir()) { if self.path.is_dir() {
let dir = self.path.clone(); let dir = self.path.clone();
let pkg_gate = self.path.join("package.mist"); let pkg_gate = self.path.join("package.mist");
if (pkg_gate.exists()) { if pkg_gate.exists() {
self.path = pkg_gate; self.path = pkg_gate;
} }
self.scan_directory(&dir, config); self.scan_directory(&dir, config);
} else if (config.should_ignore(&self.path)) { } else if config.should_ignore(&self.path) {
let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf(); let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf();
self.scan_directory(&dir, config); self.scan_directory(&dir, config);
@@ -36,7 +38,7 @@ pub class Module {
} }
fn scan_directory(*mut self, search_dir *Path, config *MistConfig) { fn scan_directory(*mut self, search_dir *Path, config *MistConfig) {
let entries = match (fs::read_dir(search_dir)) { let entries = match fs::read_dir(search_dir) {
Ok[entries] => entries; Ok[entries] => entries;
Err[e] => { Err[e] => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e); eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
@@ -44,8 +46,8 @@ pub class Module {
} }
}; };
for (entry : entries) { for entry in entries {
let entry = match (entry) { let entry = match entry {
Ok[entry] => entry; Ok[entry] => entry;
Err[e] => { Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e); eprintln!("error: failed to read directory entry\n {}", e);
@@ -55,7 +57,7 @@ pub class Module {
let child_path = entry.path(); let child_path = entry.path();
if (child_path == self.path || child_path == config.package) { if child_path == self.path || child_path == config.package {
continue; continue;
} }
@@ -68,7 +70,7 @@ pub class Module {
} }
pub fn output_dir(*self, parent_dir *PathBuf) PathBuf { pub fn output_dir(*self, parent_dir *PathBuf) PathBuf {
if (self.path.is_dir() || self.is_package()) { if self.path.is_dir() || self.is_package() {
parent_dir.join(&self.name) parent_dir.join(&self.name)
} else { } else {
parent_dir.clone() parent_dir.clone()
@@ -76,7 +78,7 @@ pub class Module {
} }
pub fn output_path(*self, parent_dir *PathBuf) PathBuf { pub fn output_path(*self, parent_dir *PathBuf) PathBuf {
if (self.path.is_dir() || self.is_package()) { if self.path.is_dir() || self.is_package() {
parent_dir.join(&self.name).join("mod.rs") parent_dir.join(&self.name).join("mod.rs")
} else { } else {
parent_dir.join(&self.name).with_extension("rs") parent_dir.join(&self.name).with_extension("rs")
+22 -20
View File
@@ -1,3 +1,5 @@
pub module transpiler;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@@ -20,12 +22,12 @@ pub struct MistConfig {
} }
impl MistConfig { impl MistConfig {
pub fn should_ignore(*self, path *Path) bool { pub fn should_ignore(*self, path *Path) bool {
if (path == self.package) { if path == self.package {
return true; return true;
} }
for (pkg : &self.packages) { for pkg in &self.packages {
if (path.ends_with(pkg)) { if path.ends_with(pkg) {
return true; return true;
} }
} }
@@ -58,7 +60,7 @@ pub fn transpile() PathBuf {
let mut config MistConfig = toml::from_str(&toml_content) let mut config MistConfig = toml::from_str(&toml_content)
.unwrap_or_else(fn(_) -> panic!("Failed to parse Mist.toml from {:?}", toml_path)); .unwrap_or_else(fn(_) -> panic!("Failed to parse Mist.toml from {:?}", toml_path));
for (pkg : &mut config.packages) { for pkg in &mut config.packages {
*pkg = src.join(&*pkg); *pkg = src.join(&*pkg);
} }
@@ -83,7 +85,7 @@ pub fn transpile() PathBuf {
impl modules::Module { impl modules::Module {
fn transpile(*self, parent_dir *PathBuf, config *mut MistConfig) String { fn transpile(*self, parent_dir *PathBuf, config *mut MistConfig) String {
if (!self.path.is_dir() && self.path.extension().and_then(fn(v) -> v.to_str()) != Some("mist")) { if !self.path.is_dir() && self.path.extension().and_then(fn(v) -> v.to_str()) != Some("mist") {
let file_name = self.path.file_name() let file_name = self.path.file_name()
.and_then(fn(s) -> s.to_str()) .and_then(fn(s) -> s.to_str())
.expect("Failed to get file name"); .expect("Failed to get file name");
@@ -100,15 +102,15 @@ impl modules::Module {
let mut output = String::new(); let mut output = String::new();
for (child : &self.children) { for child in &self.children {
if (!config.should_ignore(&child.path)) { if !config.should_ignore(&child.path) {
output.push_str(&child.transpile(&dir, config)); output.push_str(&child.transpile(&dir, config));
} }
} }
if (self.path == config.package) { if self.path == config.package {
config.main_mods = output.clone(); config.main_mods = output.clone();
} else if (config.should_ignore(&self.path)) { } else if config.should_ignore(&self.path) {
output.push_str(&config.main_mods) output.push_str(&config.main_mods)
} }
@@ -116,14 +118,14 @@ impl modules::Module {
} }
fn write_transpile(*self, output String, output_file PathBuf) String { fn write_transpile(*self, output String, output_file PathBuf) String {
if (self.path.is_dir()) { if self.path.is_dir() {
if (output.len() == 0) { if output.len() == 0 {
return String::new(); return String::new();
} }
let res = fs::write(&output_file, output); let res = fs::write(&output_file, output);
if (res.is_err()) { if res.is_err() {
eprintln!( eprintln!(
"error: failed to write output {}\n {}", "error: failed to write output {}\n {}",
output_file.display(), output_file.display(),
@@ -141,7 +143,7 @@ impl modules::Module {
} }
fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String { fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String {
let source = match (fs::read_to_string(&path)) { let source = match fs::read_to_string(&path) {
Ok[s] => s; Ok[s] => s;
Err[e] => { Err[e] => {
eprintln!("error: failed to read file {}\n {}", path.display(), e); eprintln!("error: failed to read file {}\n {}", path.display(), e);
@@ -152,10 +154,10 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
let mut gc = mist_codegen::RustCodegen::new(); let mut gc = mist_codegen::RustCodegen::new();
let parsed = match (mist_parser::parse(&source)) { let parsed = match mist_parser::parse(&source) {
Ok[ast] => ast; Ok[ast] => ast;
Err[e] => { Err[e] => {
match (e) { match e {
error::ParseError::Ast[e] => { error::ParseError::Ast[e] => {
let start_pos = e.span.start_pos().line_col(); let start_pos = e.span.start_pos().line_col();
@@ -186,9 +188,9 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
let output = gc.generate(parsed.1); let output = gc.generate(parsed.1);
if (let [Some[[vis, name]], _] = parsed) { if let [Some[[vis, name]], _] = parsed {
if (output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs")) { if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") {
if (let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned())) { if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) {
let mut new_name = std::ffi::OsString::from(&name.0); let mut new_name = std::ffi::OsString::from(&name.0);
new_name.push("."); new_name.push(".");
@@ -202,7 +204,7 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
let res = fs::write(&output_file, format!("{mod_decl}{output}")); let res = fs::write(&output_file, format!("{mod_decl}{output}"));
if (res.is_err()) { if res.is_err() {
eprintln!( eprintln!(
"error: failed to write output {}\n {}", "error: failed to write output {}\n {}",
output_file.display(), output_file.display(),
@@ -216,7 +218,7 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
} else { } else {
let res = fs::write(&output_file, format!("{mod_decl}{output}")); let res = fs::write(&output_file, format!("{mod_decl}{output}"));
if (res.is_err()) { if res.is_err() {
eprintln!( eprintln!(
"error: failed to write output {}\n {}", "error: failed to write output {}\n {}",
output_file.display(), output_file.display(),