Skip to content

Commit

Permalink
Change json serialization to use 4 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mpizenberg committed Feb 12, 2021
1 parent 55493f5 commit bd47112
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ nom = "6.1.0"
notify = "4.0.15"
atty = "0.2.14"
anyhow = "1.0.38"
serde = { version = "1.0.123", default-features = false }

[dependencies.clap]
version = "2.33.3"
Expand Down
8 changes: 2 additions & 6 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ pub fn main<P: AsRef<Path>>(elm_home: P, project_root: P, offline: bool) -> anyh
let updated_config = crate::deps::init(elm_home, project_config, offline).context(
"Something went wrong when installing elm-explorations/test to the tests dependencies",
)?;
std::fs::write(
&elm_json_path,
serde_json::to_string_pretty(&updated_config)
.context("Unable to convert the config to a JSON string")?,
)
.context("Unable to write the updated elm.json")?;
crate::utils::json_write(&elm_json_path, &updated_config)
.context("Unable to write the updated elm.json")?;

// Create the tests/Tests.elm template
let init_tests_template = include_template!("Tests.elm");
Expand Down
1 change: 1 addition & 0 deletions src/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ pub fn main_helper(
"Could not create tests dir {}",
tests_root.join("src").display()
))?;
// If it has changed, update the elm.json for the tests.
let tests_config_str = serde_json::to_string(&tests_config)
.context("Failed to convert to JSON the string generated for the tests elm.json")?;
match std::fs::read_to_string(&tests_config_path) {
Expand Down
17 changes: 17 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use anyhow::Context;
use std::error::Error;
use std::io::Write;
use std::path::{Path, PathBuf};

#[macro_export]
Expand Down Expand Up @@ -77,3 +78,19 @@ pub fn http_fetch(url: &str) -> Result<String, Box<dyn Error>> {
.context("Error converting the http response body to a String")?;
Ok(response)
}

// pub fn write_elm_json(project: &Project, matches: &ArgMatches) -> Result<()> {
pub fn json_write<P: AsRef<Path>, T: ?Sized + serde::Serialize>(
path: P,
value: &T,
) -> anyhow::Result<()> {
let path = path.as_ref();
let file = std::fs::File::create(path)?;
let writer = std::io::BufWriter::new(file);
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut serializer = serde_json::Serializer::with_formatter(writer, formatter);
value.serialize(&mut serializer)?;
let mut writer = serializer.into_inner();
writeln!(&mut writer)?;
writer.flush().map_err(|e| e.into())
}

0 comments on commit bd47112

Please sign in to comment.