-
Notifications
You must be signed in to change notification settings - Fork 2
feat(trace): Add trace formatter #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| [workspace] | ||
| resolver = "2" | ||
| members = ["runtime_tracing", "runtime_tracing_cli"] | ||
| members = ["runtime_tracing", "runtime_tracing_cli", "trace_formatter"] | ||
|
|
||
| [workspace.dependencies] | ||
| runtime_tracing = { path = "runtime_tracing/" } | ||
| trace_formatter = { path = "trace_formatter/"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| use clap::Args; | ||
| use serde_json::Value; | ||
| use trace_formatter::{ | ||
| prettify::{correct_path, prettify_value}, | ||
| read_write_json::{save_to_file, serialize_file}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone, Args)] | ||
| pub(crate) struct FmtTraceCommand { | ||
| /// Trace file which we want to format | ||
| source_file: String, | ||
|
|
||
| /// Path where the formatted trace will be saved | ||
| target_file: String, | ||
| } | ||
|
|
||
| pub(crate) fn run(args: FmtTraceCommand) { | ||
| let ser_json: Value = serialize_file(args.source_file); | ||
|
|
||
| let prettified_json: String = prettify_value(ser_json, "", false); | ||
| let final_pretty_json: String = correct_path(&prettified_json); | ||
|
|
||
| save_to_file(args.target_file, final_pretty_json); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "trace_formatter" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| serde = "1.0" | ||
| serde_json = { version = "1.0", features = ["preserve_order"] } | ||
| regex = "1.10.5" | ||
|
|
||
| [lib] | ||
| name = "trace_formatter" | ||
| path = "src/lib.rs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Format-trace-tool | ||
| ## Overview | ||
| This tool is used for formatting json files, especially ones that are generated from the command: | ||
| ```bash | ||
| nargo trace | ||
| ``` | ||
| ## Usage | ||
| You need to provide two arguments, first being the source file containing the json, second is the destination file name. | ||
| Example: | ||
| ```bash | ||
| cargo run src.json des.json | ||
| ``` | ||
| This will generate a file in the current directory named "des.json" containing the output of our program. | ||
| ### Trace formatting example | ||
| Input: | ||
| ```json | ||
| [{"a":1},{"b":"bbb"},{"c":{"f1":3,"f2":"0"}}] | ||
| ``` | ||
| Output: | ||
| ```json | ||
| [ | ||
| { "a": 1 }, | ||
| { "b": "bbb" }, | ||
| { "c": { "f1": 3, "f2": "0" } } | ||
| ] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| pub mod prettify; | ||
| pub mod read_write_json; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::prettify::correct_path; | ||
|
|
||
| use super::*; | ||
| fn generate_pretty_json(input_json: &str) -> String { | ||
| let ser_json = serde_json::from_str(input_json).expect("Failed to parse the json input"); | ||
| let prettified_json: String = prettify::prettify_value(ser_json, "", false); | ||
| let mut final_pretty_json: String = correct_path(&prettified_json); | ||
| final_pretty_json.push('\n'); //this is done automatically when saving the json to a file | ||
| final_pretty_json | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_single_json_object() { | ||
| let input_json = r#"[{"Key":"val"}]"#; | ||
| let expected = r#"[ | ||
| { "Key": "val" } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_non_absolute_path_json() { | ||
| let input_json = r#"[{"Path":"?"},{"Path":"src/dir/main.nr"}]"#; | ||
| let expected = r#"[ | ||
| { "Path": "?" }, | ||
| { "Path": "src/dir/main.nr" } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_absolute_path_json() { | ||
| let input_json = r#"[{"Path":"?"},{"Path":"some/absolute/path/src/dir/main.nr"}]"#; | ||
| let expected = r#"[ | ||
| { "Path": "?" }, | ||
| { "Path": "<relative-to-this>/src/dir/main.nr" } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_basic_nested_array_json() { | ||
| let input_json = r#"[{"arr":[{"nested_arr":[{"key":"val"}]}]}]"#; | ||
| let expected = r#"[ | ||
| { "arr": [ | ||
| { "nested_arr": [ | ||
| { "key": "val" } | ||
| ] } | ||
| ] } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_basic_nested_json_objects() { | ||
| let input_json = r#"[{"key":{"inner_key1":"inner_value1","inner_key2":"inner_value2"}}]"#; | ||
| let expected = r#"[ | ||
| { "key": { "inner_key1": "inner_value1", "inner_key2": "inner_value2" } } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_arrays_nested_objects_full_json() { | ||
| let input_json = r#"[{"a":"111"},{"b":[]},{"c":[{"arr":"arr1", "abb" : 1},"#.to_string() | ||
| + r#"{"arr":"arr2","abb" : 2},{"arr":"arr3","abb" : 3}]},{"long":"a1","along1":"a2"},"# | ||
| + r#"{ "Value": { "variable_id": 0, "value": { "kind": "Int", "i": 4, "type_id": 1 } } }]"#; | ||
|
|
||
| let expected = r#"[ | ||
| { "a": "111" }, | ||
| { "b": [] }, | ||
| { "c": [ | ||
| { "arr": "arr1", "abb": 1 }, | ||
| { "arr": "arr2", "abb": 2 }, | ||
| { "arr": "arr3", "abb": 3 } | ||
| ] }, | ||
| { "long": "a1", "along1": "a2" }, | ||
| { "Value": { "variable_id": 0, "value": { "kind": "Int", "i": 4, "type_id": 1 } } } | ||
| ] | ||
| "#; | ||
| let final_pretty_json = generate_pretty_json(&input_json); | ||
| assert_eq!(final_pretty_json, expected); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| use regex::Regex; | ||
| use serde_json::Value; | ||
|
|
||
| pub fn prettify_value(root_value: Value, indent: &str, is_in_array: bool) -> String { | ||
| let content = match root_value { | ||
| Value::Array(elements) => { | ||
| let new_indent = indent.to_string() + " "; | ||
| let parts: Vec<String> = elements | ||
| .into_iter() | ||
| .map(|el| prettify_value(el, new_indent.as_str(), true)) | ||
| .collect::<Vec<String>>(); | ||
| if parts.is_empty() { | ||
| "[]".to_string() | ||
| } else { | ||
| let lines = parts.join(",\n"); | ||
| format!("[\n{lines}\n{indent}]") | ||
| } | ||
| } | ||
| Value::Object(map) => { | ||
| let parts: Vec<String> = map | ||
| .into_iter() | ||
| .map(|(k, v)| { | ||
| let head = format!("\"{k}\""); | ||
| let rest = prettify_value(v, indent, false); | ||
| format!("{head}: {rest}") | ||
| }) | ||
| .collect(); | ||
| let json_object_string = parts.join(", "); | ||
| format!("{{ {json_object_string} }}") | ||
| } | ||
| _ => root_value.to_string(), | ||
| }; | ||
| let indent = if is_in_array { indent } else { "" }; | ||
| format!("{indent}{content}") | ||
| } | ||
|
|
||
| /// Replaces all absolute paths in the trace with relative paths | ||
| pub fn correct_path(pretty_json: &str) -> String { | ||
| let re = Regex::new(r#" \{ "Path": (?<abs_path>.*)(?<rel_path>/src/.*)"#); | ||
| let result = re.map(|regex| { | ||
| regex.replace_all(pretty_json, |caps: ®ex::Captures| { | ||
| format!(" {{ \"Path\": \"<relative-to-this>{}", &caps["rel_path"]) | ||
| }) | ||
| }); | ||
| result.map(|result_string| result_string.into_owned()).unwrap_or(pretty_json.to_string()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| use serde_json::Value; | ||
| use std::fs; | ||
|
|
||
| pub fn serialize_file(src_filename: String) -> Value { | ||
| let file_content = fs::read_to_string(src_filename).expect("Failed to read the file"); | ||
|
|
||
| serde_json::from_str(&file_content) | ||
| .expect("Failed to parse the json file that was given as a source") | ||
| } | ||
|
|
||
| pub fn save_to_file(dest_filename: String, json_string: String) { | ||
| let mut json_string_copy = json_string.clone(); | ||
| if !json_string_copy.ends_with('\n') { | ||
| json_string_copy.push('\n'); | ||
| } | ||
| fs::write(&dest_filename, json_string_copy).unwrap_or_else(|_| { | ||
| panic!("Unable to write to destination file: {}", dest_filename.as_str()) | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.