Skip to content

Commit

Permalink
[move] Enable serialization of source maps into json
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Aug 22, 2024
1 parent bacc76c commit e1c998e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions external-crates/move/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ move-command-line-common.workspace = true
bcs.workspace = true

serde.workspace = true
serde_json.workspace = true

[features]
default = []
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
use crate::source_map::SourceMap;
use anyhow::{format_err, Result};
use move_ir_types::location::Loc;
use std::{fs::File, io::Read, path::Path};
use std::{
fs::File,
io::{Read, Write},
path::Path,
};

pub type Error = (Loc, String);
pub type Errors = Vec<Error>;

pub fn source_map_from_file(file_path: &Path) -> Result<SourceMap> {
if file_path.extension().is_some_and(|ext| ext == "json") {
return deserialize_from_json(file_path);
}
let mut bytes = Vec::new();
File::open(file_path)
.ok()
Expand All @@ -19,3 +26,31 @@ pub fn source_map_from_file(file_path: &Path) -> Result<SourceMap> {
bcs::from_bytes::<SourceMap>(&bytes)
.map_err(|_| format_err!("Error deserializing into source map"))
}

pub fn serialize_to_json(map: &SourceMap) -> Result<Vec<u8>> {
serde_json::to_vec(map).map_err(|e| format_err!("Error serializing to json: {}", e))
}

pub fn serialize_to_json_file(map: &SourceMap, file_path: &Path) -> Result<()> {
let json = serde_json::to_string_pretty(map)
.map_err(|e| format_err!("Error serializing to json: {}", e))?;
let mut f =
std::fs::File::create(file_path).map_err(|e| format_err!("Error creating file: {}", e))?;
f.write_all(json.as_bytes())
.map_err(|e| format_err!("Error writing to file: {}", e))?;
Ok(())
}

pub fn deserialize_from_json(file_path: &Path) -> Result<SourceMap> {
let mut file = File::open(file_path).map_err(|e| format_err!("Error opening file: {}", e))?;
let mut json = String::new();
file.read_to_string(&mut json)
.map_err(|e| format_err!("Error reading file: {}", e))?;
serde_json::from_str(&json).map_err(|e| format_err!("Error deserializing from json: {}", e))
}

pub fn convert_to_json(file_path: &Path) -> Result<()> {
let map = source_map_from_file(file_path)?;
let json_file_path = file_path.with_extension("json");
serialize_to_json_file(&map, &json_file_path)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use anyhow::{ensure, Result};
use colored::Colorize;
use itertools::{Either, Itertools};
use move_binary_format::file_format::CompiledModule;
use move_bytecode_source_map::utils::source_map_from_file;
use move_bytecode_source_map::utils::{serialize_to_json, source_map_from_file};
use move_bytecode_utils::Modules;
use move_command_line_common::files::{
extension_equals, find_filenames, try_exists, MOVE_COMPILED_EXTENSION, MOVE_EXTENSION,
Expand Down Expand Up @@ -321,6 +321,13 @@ impl OnDiskCompiledPackage {
.with_extension(SOURCE_MAP_EXTENSION),
compiled_unit.unit.serialize_source_map().as_slice(),
)?;
self.save_under(
CompiledPackageLayout::SourceMaps
.path()
.join(&file_path)
.with_extension("json"),
&serialize_to_json(&compiled_unit.unit.source_map)?,
)?;
self.save_under(
CompiledPackageLayout::Sources
.path()
Expand Down

0 comments on commit e1c998e

Please sign in to comment.