Improved paths, and added rust specific errors

This commit is contained in:
2026-05-19 04:35:12 +02:00
parent e30722dde3
commit 95ae7b8d52
3 changed files with 41 additions and 21 deletions
+36 -17
View File
@@ -1,10 +1,11 @@
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fs, env, fs,
path::PathBuf,
process::{Command, Stdio}, process::{Command, Stdio},
}; };
use cargo_metadata::Message; use cargo_metadata::{CompilerMessage, Message};
use mist_parser::rev_mapper::{MistMap, RustMap, find_mapping, get_mapping}; use mist_parser::rev_mapper::{MistMap, RustMap, find_mapping, get_mapping};
use crate::transpiler::Config; use crate::transpiler::Config;
@@ -12,7 +13,8 @@ use crate::transpiler::Config;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MistDiagnosticMessage { pub struct MistDiagnosticMessage {
pub message: String, pub message: String,
pub src_path: String, pub file_path: String,
pub file_name: String,
pub line: usize, pub line: usize,
pub column: usize, pub column: usize,
} }
@@ -21,15 +23,19 @@ pub struct MistDiagnosticMessage {
pub enum MistDiagnostic { pub enum MistDiagnostic {
Error(MistDiagnosticMessage), Error(MistDiagnosticMessage),
Warning(MistDiagnosticMessage), Warning(MistDiagnosticMessage),
Rust(CompilerMessage),
} }
pub fn build( pub fn build(
mut args: Vec<String>, mut args: Vec<String>,
config: Config, config: Config,
root: PathBuf,
) -> Result<Vec<MistDiagnostic>, Vec<MistDiagnostic>> { ) -> Result<Vec<MistDiagnostic>, Vec<MistDiagnostic>> {
args.remove(0); args.remove(0);
args.insert(1, "--message-format=json".to_string()); args.insert(1, "--message-format=json".to_string());
let is_root = root == env::current_dir().unwrap();
let mut command = Command::new("cargo") let mut command = Command::new("cargo")
.args(args) .args(args)
.stdout(Stdio::piped()) .stdout(Stdio::piped())
@@ -45,25 +51,37 @@ pub fn build(
for message in cargo_metadata::Message::parse_stream(reader) { for message in cargo_metadata::Message::parse_stream(reader) {
match message { match message {
Ok(Message::CompilerMessage(msg)) => { Ok(Message::CompilerMessage(msg)) => {
let file_name = msg.target.src_path.as_str().to_string().clone(); for span in &msg.message.spans {
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 {
if span.is_primary { if span.is_primary {
let mist_span = let file_name = span.file_name.clone();
find_mapping(&map, &RustMap(span.line_end, span.column_start)).unwrap();
let mist_file = span let mist_file = span
.file_name .file_name
.replacen(&config.output, &config.src, 1) .replacen(&config.output, &config.src, 1)
.replace(".rs", ".mist"); .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 { let mist_msg = MistDiagnosticMessage {
message: span.label.unwrap_or(msg.message.message.clone()), message: span.label.clone().unwrap_or(msg.message.message.clone()),
src_path: mist_file, 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, line: mist_span.1.0,
column: mist_span.1.1, column: mist_span.1.1,
}; };
@@ -107,7 +125,7 @@ pub fn print_diagnostics(diagnostics: Vec<MistDiagnostic>) {
println!( println!(
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}",
msg.src_path, msg.file_name,
msg.line + 1, msg.line + 1,
msg.column, msg.column,
msg.message, msg.message,
@@ -120,13 +138,14 @@ pub fn print_diagnostics(diagnostics: Vec<MistDiagnostic>) {
println!( println!(
"\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}", "\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}",
msg.src_path, msg.file_name,
msg.line + 1, msg.line + 1,
msg.column, msg.column,
msg.message, msg.message,
line.unwrap_or_default(), line.unwrap_or_default(),
) )
} }
MistDiagnostic::Rust(rs) => println!("{rs}"),
} }
} }
} }
@@ -135,7 +154,7 @@ pub fn get_line(
files: &mut HashMap<String, Vec<String>>, files: &mut HashMap<String, Vec<String>>,
msg: &MistDiagnosticMessage, msg: &MistDiagnosticMessage,
) -> Option<String> { ) -> Option<String> {
let src_path = msg.src_path.clone(); let src_path = msg.file_path.clone();
let lines = files.entry(src_path.clone()).or_insert_with(|| { let lines = files.entry(src_path.clone()).or_insert_with(|| {
fs::read_to_string(src_path) fs::read_to_string(src_path)
+3 -2
View File
@@ -14,8 +14,9 @@ fn main() {
match args[1].as_str() { match args[1].as_str() {
"run" | "build" | "r" | "b" => { "run" | "build" | "r" | "b" => {
let config = transpiler::build(); let (config, root) = transpiler::build();
match builder::build(args, config) {
match builder::build(args, config, root) {
Ok(diagnostics) => builder::print_diagnostics(diagnostics), Ok(diagnostics) => builder::print_diagnostics(diagnostics),
Err(diagnostics) => builder::print_diagnostics(diagnostics), Err(diagnostics) => builder::print_diagnostics(diagnostics),
} }
+2 -2
View File
@@ -14,7 +14,7 @@ pub struct Config {
pub output: String, pub output: String,
} }
pub fn build() -> Config { pub fn build() -> (Config, PathBuf) {
let start = Instant::now(); let start = Instant::now();
// 1. find project root // 1. find project root
@@ -36,7 +36,7 @@ pub fn build() -> Config {
println!("build finished in {:.2?}", elapsed); println!("build finished in {:.2?}", elapsed);
config (config, root)
} }
fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {