difftreelog
feat move trace mapping api to evaluator
in: master
4 files changed
cmds/jrsonnet/src/location.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/location.rs
+++ /dev/null
@@ -1,99 +0,0 @@
-#[derive(Clone, PartialEq, Debug)]
-pub struct CodeLocation {
- pub line: usize,
- pub column: usize,
-
- pub line_start_offset: usize,
- pub line_end_offset: usize,
-}
-
-pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
- if offsets.is_empty() {
- return vec![];
- }
- let mut line = 1;
- let mut column = 0;
- let max_offset = *offsets.iter().max().unwrap();
-
- let mut offset_map = offsets
- .iter()
- .enumerate()
- .map(|(pos, offset)| (*offset, pos))
- .collect::<Vec<_>>();
- offset_map.sort_by_key(|v| v.0);
- offset_map.reverse();
-
- let mut out = vec![
- CodeLocation {
- column: 0,
- line: 0,
- line_start_offset: 0,
- line_end_offset: 0
- };
- offsets.len()
- ];
- let mut with_no_known_line_ending = vec![];
- let mut this_line_offset = 0;
- for (pos, ch) in file.chars().enumerate() {
- column += 1;
- match offset_map.last() {
- Some(x) if x.0 == pos => {
- let out_idx = x.1;
- with_no_known_line_ending.push(out_idx);
- out[out_idx].line = line;
- out[out_idx].column = column;
- out[out_idx].line_start_offset = this_line_offset + 1;
- offset_map.pop();
- }
- _ => {}
- }
- if ch == '\n' {
- line += 1;
- column = 0;
-
- for idx in with_no_known_line_ending.drain(..) {
- out[idx].line_end_offset = pos;
- }
- this_line_offset = pos;
-
- if pos == max_offset + 1 {
- break;
- }
- }
- }
- let file_end = file.chars().count();
- for idx in with_no_known_line_ending {
- out[idx].line_end_offset = file_end;
- }
-
- out
-}
-
-#[cfg(test)]
-pub mod tests {
- use super::{offset_to_location, CodeLocation};
-
- #[test]
- fn test() {
- assert_eq!(
- offset_to_location(
- "hello world\n_______________________________________________________",
- &[0, 14]
- ),
- vec![
- CodeLocation {
- line: 1,
- column: 1,
- line_start_offset: 1,
- line_end_offset: 11
- },
- CodeLocation {
- line: 2,
- column: 3,
- line_start_offset: 12,
- line_end_offset: 67
- }
- ]
- )
- }
-}
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,9 +1,6 @@
-pub mod location;
-
use clap::Clap;
-use jrsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};
+use jrsonnet_evaluator::{trace::CodeLocation, EvaluationState, LocError, StackTrace, Val};
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};
-use location::{offset_to_location, CodeLocation};
use std::env::current_dir;
use std::{collections::HashMap, path::PathBuf, rc::Rc, str::FromStr};
@@ -252,14 +249,11 @@
for item in trace.0.iter() {
let desc = &item.1;
let source = item.0.clone();
- let code = evaluator.get_source(&source.0);
- if code.is_none() {
- continue;
- }
- let code = code.unwrap();
- let start_end = offset_to_location(&code, &[source.1, source.2]);
+ let start_end = evaluator.map_source_locations(&source.0, &[source.1, source.2]);
if opts.trace_format == TraceFormat::Custom {
- let source_fragment: String = code
+ let source_fragment: String = evaluator
+ .get_source(&source.0)
+ .unwrap()
.chars()
.skip(start_end[0].line_start_offset)
.take(start_end[1].line_end_offset - start_end[0].line_start_offset)
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth16mod map;16mod map;17mod obj;17mod obj;18mod val;18mod val;19pub mod trace;192020pub use ctx::*;21pub use ctx::*;21pub use dynamic::*;22pub use dynamic::*;27pub use obj::*;28pub use obj::*;28use std::{cell::{Ref, RefCell, RefMut}, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};29use std::{cell::{Ref, RefCell, RefMut}, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};29pub use val::*;30pub use val::*;31use trace::{offset_to_location, CodeLocation};303231type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;33type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;32#[derive(Clone)]34#[derive(Clone)]187 let ro_map = &self.data().files;189 let ro_map = &self.data().files;188 ro_map.get(name).map(|value| value.0.clone())190 ro_map.get(name).map(|value| value.0.clone())189 }191 }192 pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {193 offset_to_location(&self.get_source(file).unwrap(), locs)194 }195190 pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {196 pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {191 self.run_in_state(|| {197 self.run_in_state(|| {crates/jrsonnet-evaluator/src/trace.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/trace.rs
@@ -0,0 +1,99 @@
+#[derive(Clone, PartialEq, Debug)]
+pub struct CodeLocation {
+ pub line: usize,
+ pub column: usize,
+
+ pub line_start_offset: usize,
+ pub line_end_offset: usize,
+}
+
+pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
+ if offsets.is_empty() {
+ return vec![];
+ }
+ let mut line = 1;
+ let mut column = 0;
+ let max_offset = *offsets.iter().max().unwrap();
+
+ let mut offset_map = offsets
+ .iter()
+ .enumerate()
+ .map(|(pos, offset)| (*offset, pos))
+ .collect::<Vec<_>>();
+ offset_map.sort_by_key(|v| v.0);
+ offset_map.reverse();
+
+ let mut out = vec![
+ CodeLocation {
+ column: 0,
+ line: 0,
+ line_start_offset: 0,
+ line_end_offset: 0
+ };
+ offsets.len()
+ ];
+ let mut with_no_known_line_ending = vec![];
+ let mut this_line_offset = 0;
+ for (pos, ch) in file.chars().enumerate() {
+ column += 1;
+ match offset_map.last() {
+ Some(x) if x.0 == pos => {
+ let out_idx = x.1;
+ with_no_known_line_ending.push(out_idx);
+ out[out_idx].line = line;
+ out[out_idx].column = column;
+ out[out_idx].line_start_offset = this_line_offset + 1;
+ offset_map.pop();
+ }
+ _ => {}
+ }
+ if ch == '\n' {
+ line += 1;
+ column = 0;
+
+ for idx in with_no_known_line_ending.drain(..) {
+ out[idx].line_end_offset = pos;
+ }
+ this_line_offset = pos;
+
+ if pos == max_offset + 1 {
+ break;
+ }
+ }
+ }
+ let file_end = file.chars().count();
+ for idx in with_no_known_line_ending {
+ out[idx].line_end_offset = file_end;
+ }
+
+ out
+}
+
+#[cfg(test)]
+pub mod tests {
+ use super::{offset_to_location, CodeLocation};
+
+ #[test]
+ fn test() {
+ assert_eq!(
+ offset_to_location(
+ "hello world\n_______________________________________________________",
+ &[0, 14]
+ ),
+ vec![
+ CodeLocation {
+ line: 1,
+ column: 1,
+ line_start_offset: 1,
+ line_end_offset: 11
+ },
+ CodeLocation {
+ line: 2,
+ column: 3,
+ line_start_offset: 12,
+ line_end_offset: 67
+ }
+ ]
+ )
+ }
+}