Improved diagnostic conversion
This commit is contained in:
@@ -11,5 +11,5 @@ void main() {
|
|||||||
// statistics::register(&mut app_instance);
|
// statistics::register(&mut app_instance);
|
||||||
|
|
||||||
app_instance.boot();
|
app_instance.boot();
|
||||||
app_instance.run();
|
app_instance.rund();
|
||||||
}
|
}
|
||||||
+51
-2
@@ -7,6 +7,8 @@ use std::{
|
|||||||
use cargo_metadata::Message;
|
use cargo_metadata::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;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MistDiagnosticMessage {
|
pub struct MistDiagnosticMessage {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
@@ -20,7 +22,10 @@ pub enum MistDiagnostic {
|
|||||||
Error(MistDiagnosticMessage),
|
Error(MistDiagnosticMessage),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(mut args: Vec<String>) -> Result<Vec<MistDiagnostic>, Vec<MistDiagnostic>> {
|
pub fn build(
|
||||||
|
mut args: Vec<String>,
|
||||||
|
config: Config,
|
||||||
|
) -> 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());
|
||||||
|
|
||||||
@@ -49,9 +54,14 @@ pub fn build(mut args: Vec<String>) -> Result<Vec<MistDiagnostic>, Vec<MistDiagn
|
|||||||
let mist_span =
|
let mist_span =
|
||||||
find_mapping(&map, &RustMap(span.line_end, span.column_start)).unwrap();
|
find_mapping(&map, &RustMap(span.line_end, span.column_start)).unwrap();
|
||||||
|
|
||||||
|
let mist_file = span
|
||||||
|
.file_name
|
||||||
|
.replacen(&config.output, &config.src, 1)
|
||||||
|
.replace(".rs", ".mist");
|
||||||
|
|
||||||
diagnostics.push(MistDiagnostic::Error(MistDiagnosticMessage {
|
diagnostics.push(MistDiagnostic::Error(MistDiagnosticMessage {
|
||||||
message: span.label.unwrap_or(msg.message.message.clone()),
|
message: span.label.unwrap_or(msg.message.message.clone()),
|
||||||
src_path: span.file_name,
|
src_path: mist_file,
|
||||||
line: mist_span.1.0,
|
line: mist_span.1.0,
|
||||||
column: mist_span.1.1,
|
column: mist_span.1.1,
|
||||||
}));
|
}));
|
||||||
@@ -71,3 +81,42 @@ pub fn build(mut args: Vec<String>) -> Result<Vec<MistDiagnostic>, Vec<MistDiagn
|
|||||||
|
|
||||||
Ok(diagnostics)
|
Ok(diagnostics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn print_diagnostics(diagnostics: Vec<MistDiagnostic>) {
|
||||||
|
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<String, Vec<String>>,
|
||||||
|
msg: &MistDiagnosticMessage,
|
||||||
|
) -> Option<String> {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|||||||
+7
-2
@@ -14,10 +14,15 @@ fn main() {
|
|||||||
|
|
||||||
match args[1].as_str() {
|
match args[1].as_str() {
|
||||||
"run" | "build" | "r" | "b" => {
|
"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();
|
transpiler::build();
|
||||||
builder::build(args).unwrap();
|
|
||||||
}
|
}
|
||||||
"transpile" | "t" => transpiler::build(),
|
|
||||||
"version" | "--version" | "-v" => {
|
"version" | "--version" | "-v" => {
|
||||||
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -9,12 +9,12 @@ use mist_parser::error::ParseError;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
pub struct Config {
|
||||||
src: String,
|
pub src: String,
|
||||||
output: String,
|
pub output: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build() {
|
pub fn build() -> Config {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// 1. find project root
|
// 1. find project root
|
||||||
@@ -35,6 +35,8 @@ pub fn build() {
|
|||||||
let elapsed = start.elapsed();
|
let elapsed = start.elapsed();
|
||||||
|
|
||||||
println!("build finished in {:.2?}", elapsed);
|
println!("build finished in {:.2?}", elapsed);
|
||||||
|
|
||||||
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user