1use std::path::{Path, PathBuf};23use jrsonnet_parser::{CodeLocation, Source};45use crate::{error::Error, LocError, State};678pub enum PathResolver {9 10 FileName,11 12 Absolute,13 14 Relative(PathBuf),15}1617impl PathResolver {18 pub fn resolve(&self, from: &Path) -> String {19 match self {20 Self::FileName => from21 .file_name()22 .expect("file name exists")23 .to_string_lossy()24 .into_owned(),25 Self::Absolute => from.to_string_lossy().into_owned(),26 Self::Relative(base) => {27 if from.is_relative() {28 return from.to_string_lossy().into_owned();29 }30 pathdiff::diff_paths(from, base)31 .expect("base is absolute")32 .to_string_lossy()33 .into_owned()34 }35 }36 }37}383940#[allow(clippy::module_name_repetitions)]41pub trait TraceFormat {42 fn write_trace(43 &self,44 out: &mut dyn std::fmt::Write,45 s: &State,46 error: &LocError,47 ) -> Result<(), std::fmt::Error>;48}4950fn print_code_location(51 out: &mut impl std::fmt::Write,52 start: &CodeLocation,53 end: &CodeLocation,54) -> Result<(), std::fmt::Error> {55 if start.line == end.line {56 if start.column == end.column {57 write!(out, "{}:{}", start.line, end.column.saturating_sub(1))?;58 } else {59 write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;60 }61 } else {62 write!(63 out,64 "{}:{}-{}:{}",65 start.line,66 end.column.saturating_sub(1),67 start.line,68 end.column69 )?;70 }71 Ok(())72}737475pub struct CompactFormat {76 pub resolver: PathResolver,77 pub padding: usize,78}7980impl TraceFormat for CompactFormat {81 fn write_trace(82 &self,83 out: &mut dyn std::fmt::Write,84 _s: &State,85 error: &LocError,86 ) -> Result<(), std::fmt::Error> {87 write!(out, "{}", error.error())?;88 if let Error::ImportSyntaxError { path, error } = error.error() {89 use std::fmt::Write;9091 writeln!(out)?;92 let mut n = match path.path() {93 Some(r) => self.resolver.resolve(r),94 None => path.short_display().to_string(),95 };96 let mut offset = error.location.offset;97 let is_eof = if offset >= path.code().len() {98 offset = path.code().len().saturating_sub(1);99 true100 } else {101 false102 };103 let mut location = path104 .map_source_locations(&[offset as u32])105 .into_iter()106 .next()107 .unwrap();108 if is_eof {109 location.column += 1;110 }111112 write!(n, ":").unwrap();113 print_code_location(&mut n, &location, &location).unwrap();114 write!(out, "{:<p$}{}", "", n, p = self.padding,)?;115 }116 let file_names = error117 .trace()118 .0119 .iter()120 .map(|el| &el.location)121 .map(|location| {122 use std::fmt::Write;123 #[allow(clippy::option_if_let_else)]124 if let Some(location) = location {125 let mut resolved_path = match location.0.path() {126 Some(r) => self.resolver.resolve(r),127 None => location.0.short_display().to_string(),128 };129 130 let location = location.0.map_source_locations(&[location.1, location.2]);131 write!(resolved_path, ":").unwrap();132 print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();133 write!(resolved_path, ":").unwrap();134 Some(resolved_path)135 } else {136 None137 }138 })139 .collect::<Vec<_>>();140 let align = file_names141 .iter()142 .flatten()143 .map(String::len)144 .max()145 .unwrap_or(0);146 for (el, file) in error.trace().0.iter().zip(file_names) {147 writeln!(out)?;148 if let Some(file) = file {149 write!(150 out,151 "{:<p$}{:<w$} {}",152 "",153 file,154 el.desc,155 p = self.padding,156 w = align157 )?;158 } else {159 write!(out, "{:<p$}{}", "", el.desc, p = self.padding,)?;160 }161 }162 Ok(())163 }164}165166pub struct JsFormat;167impl TraceFormat for JsFormat {168 fn write_trace(169 &self,170 out: &mut dyn std::fmt::Write,171 _s: &State,172 error: &LocError,173 ) -> Result<(), std::fmt::Error> {174 write!(out, "{}", error.error())?;175 for item in &error.trace().0 {176 writeln!(out)?;177 let desc = &item.desc;178 if let Some(source) = &item.location {179 let start_end = source.0.map_source_locations(&[source.1, source.2]);180 let resolved_path = match source.0.path() {181 Some(r) => r.display().to_string(),182 None => source.0.short_display().to_string(),183 };184185 write!(186 out,187 " at {} ({}:{}:{})",188 desc, resolved_path, start_end[0].line, start_end[0].column,189 )?;190 } else {191 write!(out, " during {}", desc)?;192 }193 }194 Ok(())195 }196}197198199#[cfg(feature = "explaining-traces")]200pub struct ExplainingFormat {201 pub resolver: PathResolver,202}203#[cfg(feature = "explaining-traces")]204impl TraceFormat for ExplainingFormat {205 fn write_trace(206 &self,207 out: &mut dyn std::fmt::Write,208 _s: &State,209 error: &LocError,210 ) -> Result<(), std::fmt::Error> {211 write!(out, "{}", error.error())?;212 if let Error::ImportSyntaxError { path, error } = error.error() {213 writeln!(out)?;214 let offset = error.location.offset;215 let location = path216 .map_source_locations(&[offset as u32])217 .into_iter()218 .next()219 .unwrap();220 let mut end_location = location.clone();221 end_location.offset += 1;222223 self.print_snippet(224 out,225 path.code(),226 path,227 &location,228 &end_location,229 "syntax error",230 )?;231 }232 let trace = &error.trace();233 for item in &trace.0 {234 writeln!(out)?;235 let desc = &item.desc;236 if let Some(source) = &item.location {237 let start_end = source.0.map_source_locations(&[source.1, source.2]);238 self.print_snippet(239 out,240 &source.0.code(),241 &source.0,242 &start_end[0],243 &start_end[1],244 desc,245 )?;246 } else {247 write!(out, "{}", desc)?;248 }249 }250 Ok(())251 }252}253254impl ExplainingFormat {255 fn print_snippet(256 &self,257 out: &mut dyn std::fmt::Write,258 source: &str,259 origin: &Source,260 start: &CodeLocation,261 end: &CodeLocation,262 desc: &str,263 ) -> Result<(), std::fmt::Error> {264 use annotate_snippets::{265 display_list::{DisplayList, FormatOptions},266 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},267 };268269 let source_fragment: String = source270 .chars()271 .skip(start.line_start_offset)272 .take(end.line_end_offset - end.line_start_offset)273 .collect();274275 let origin = match origin.path() {276 Some(r) => self.resolver.resolve(r),277 None => origin.short_display().to_string(),278 };279 let snippet = Snippet {280 opt: FormatOptions {281 color: true,282 ..FormatOptions::default()283 },284 title: None,285 footer: vec![],286 slices: vec![Slice {287 source: &source_fragment,288 line_start: start.line,289 origin: Some(&origin),290 fold: false,291 annotations: vec![SourceAnnotation {292 label: desc,293 annotation_type: AnnotationType::Error,294 range: (295 start.offset - start.line_start_offset,296 (end.offset - start.line_start_offset).min(source_fragment.len()),297 ),298 }],299 }],300 };301302 let dl = DisplayList::from(snippet);303 write!(out, "{}", dl)?;304305 Ok(())306 }307}