From eb84103ed635fb7a17694b2bcab38565fb4f68b5 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 19 May 2026 03:24:12 +0200 Subject: [PATCH] Improved diagnostic conversion --- mist-test/src/main.mist | 2 +- src/builder.rs | 53 +++++++++++++++++++++++++++++++++++++++-- src/main.rs | 9 +++++-- src/transpiler.rs | 10 ++++---- 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 853e686..118b6bc 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -11,5 +11,5 @@ void main() { // statistics::register(&mut app_instance); app_instance.boot(); - app_instance.run(); + app_instance.rund(); } \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index 247385d..bde2883 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -7,6 +7,8 @@ use std::{ use cargo_metadata::Message; use mist_parser::rev_mapper::{MistMap, RustMap, find_mapping, get_mapping}; +use crate::transpiler::Config; + #[derive(Debug, Clone)] pub struct MistDiagnosticMessage { pub message: String, @@ -20,7 +22,10 @@ pub enum MistDiagnostic { Error(MistDiagnosticMessage), } -pub fn build(mut args: Vec) -> Result, Vec> { +pub fn build( + mut args: Vec, + config: Config, +) -> Result, Vec> { args.remove(0); args.insert(1, "--message-format=json".to_string()); @@ -49,9 +54,14 @@ pub fn build(mut args: Vec) -> Result, Vec) -> Result, Vec) { + let mut files = HashMap::new(); + + for diag in diagnostics { + match diag { + MistDiagnostic::Error(msg) => { + let line = get_line(&mut files, &msg); + + println!( + "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", + msg.src_path, + msg.line + 1, + msg.column, + msg.message, + line.unwrap_or_default(), + ) + } + } + } +} + +pub fn get_line( + files: &mut HashMap>, + msg: &MistDiagnosticMessage, +) -> Option { + let src_path = msg.src_path.clone(); + + let lines = files.entry(src_path.clone()).or_insert_with(|| { + fs::read_to_string(src_path) + .unwrap() + .lines() + .into_iter() + .map(String::from) + .collect() + }); + + lines.get(msg.line).map(|v| v.trim().to_string()) +} diff --git a/src/main.rs b/src/main.rs index db15837..b8a7188 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,15 @@ fn main() { match args[1].as_str() { "run" | "build" | "r" | "b" => { + let config = transpiler::build(); + match builder::build(args, config) { + Ok(diagnostics) => builder::print_diagnostics(diagnostics), + Err(diagnostics) => builder::print_diagnostics(diagnostics), + } + } + "transpile" | "t" => { transpiler::build(); - builder::build(args).unwrap(); } - "transpile" | "t" => transpiler::build(), "version" | "--version" | "-v" => { println!("mist {}", env!("CARGO_PKG_VERSION")); } diff --git a/src/transpiler.rs b/src/transpiler.rs index e1dd733..c53419c 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -9,12 +9,12 @@ use mist_parser::error::ParseError; use serde::Deserialize; #[derive(Deserialize)] -struct Config { - src: String, - output: String, +pub struct Config { + pub src: String, + pub output: String, } -pub fn build() { +pub fn build() -> Config { let start = Instant::now(); // 1. find project root @@ -35,6 +35,8 @@ pub fn build() { let elapsed = start.elapsed(); println!("build finished in {:.2?}", elapsed); + + config } fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {