From 95ae7b8d5220d9507c1d091ed8847c82c4363658 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 19 May 2026 04:35:12 +0200 Subject: [PATCH] Improved paths, and added rust specific errors --- src/builder.rs | 53 ++++++++++++++++++++++++++++++++--------------- src/main.rs | 5 +++-- src/transpiler.rs | 4 ++-- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 787c212..06a8b22 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,10 +1,11 @@ use std::{ collections::{HashMap, HashSet}, - fs, + env, fs, + path::PathBuf, process::{Command, Stdio}, }; -use cargo_metadata::Message; +use cargo_metadata::{CompilerMessage, Message}; use mist_parser::rev_mapper::{MistMap, RustMap, find_mapping, get_mapping}; use crate::transpiler::Config; @@ -12,7 +13,8 @@ use crate::transpiler::Config; #[derive(Debug, Clone)] pub struct MistDiagnosticMessage { pub message: String, - pub src_path: String, + pub file_path: String, + pub file_name: String, pub line: usize, pub column: usize, } @@ -21,15 +23,19 @@ pub struct MistDiagnosticMessage { pub enum MistDiagnostic { Error(MistDiagnosticMessage), Warning(MistDiagnosticMessage), + Rust(CompilerMessage), } pub fn build( mut args: Vec, config: Config, + root: PathBuf, ) -> Result, Vec> { args.remove(0); args.insert(1, "--message-format=json".to_string()); + let is_root = root == env::current_dir().unwrap(); + let mut command = Command::new("cargo") .args(args) .stdout(Stdio::piped()) @@ -45,25 +51,37 @@ pub fn build( for message in cargo_metadata::Message::parse_stream(reader) { match message { Ok(Message::CompilerMessage(msg)) => { - let file_name = msg.target.src_path.as_str().to_string().clone(); - - let map = mapping - .entry(file_name.clone()) - .or_insert_with(|| get_mapping(&fs::read_to_string(file_name).unwrap())); - - for span in msg.message.spans { + for span in &msg.message.spans { if span.is_primary { - let mist_span = - find_mapping(&map, &RustMap(span.line_end, span.column_start)).unwrap(); + let file_name = span.file_name.clone(); let mist_file = span .file_name .replacen(&config.output, &config.src, 1) .replace(".rs", ".mist"); + let mist_path = root.join(&mist_file); + + if !fs::exists(&mist_path).unwrap() { + diagnostics.push(MistDiagnostic::Rust(msg)); + break; + } + + let map = mapping.entry(file_name.clone()).or_insert_with(|| { + get_mapping(&fs::read_to_string(file_name).unwrap()) + }); + + let mist_span = + find_mapping(&map, &RustMap(span.line_end, span.column_start)).unwrap(); + let mist_msg = MistDiagnosticMessage { - message: span.label.unwrap_or(msg.message.message.clone()), - src_path: mist_file, + message: span.label.clone().unwrap_or(msg.message.message.clone()), + file_path: mist_path.to_string_lossy().to_string(), + file_name: if is_root { + mist_file + } else { + mist_path.to_string_lossy().to_string() + }, line: mist_span.1.0, column: mist_span.1.1, }; @@ -107,7 +125,7 @@ pub fn print_diagnostics(diagnostics: Vec) { println!( "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", - msg.src_path, + msg.file_name, msg.line + 1, msg.column, msg.message, @@ -120,13 +138,14 @@ pub fn print_diagnostics(diagnostics: Vec) { println!( "\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}", - msg.src_path, + msg.file_name, msg.line + 1, msg.column, msg.message, line.unwrap_or_default(), ) } + MistDiagnostic::Rust(rs) => println!("{rs}"), } } } @@ -135,7 +154,7 @@ pub fn get_line( files: &mut HashMap>, msg: &MistDiagnosticMessage, ) -> Option { - let src_path = msg.src_path.clone(); + let src_path = msg.file_path.clone(); let lines = files.entry(src_path.clone()).or_insert_with(|| { fs::read_to_string(src_path) diff --git a/src/main.rs b/src/main.rs index b8a7188..383abe4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,9 @@ fn main() { match args[1].as_str() { "run" | "build" | "r" | "b" => { - let config = transpiler::build(); - match builder::build(args, config) { + let (config, root) = transpiler::build(); + + match builder::build(args, config, root) { Ok(diagnostics) => builder::print_diagnostics(diagnostics), Err(diagnostics) => builder::print_diagnostics(diagnostics), } diff --git a/src/transpiler.rs b/src/transpiler.rs index c53419c..fbb935d 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -14,7 +14,7 @@ pub struct Config { pub output: String, } -pub fn build() -> Config { +pub fn build() -> (Config, PathBuf) { let start = Instant::now(); // 1. find project root @@ -36,7 +36,7 @@ pub fn build() -> Config { println!("build finished in {:.2?}", elapsed); - config + (config, root) } fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {