Skip to content

Commit d9b0b20

Browse files
Avoid manually synthesizing JSON
1 parent bd195b6 commit d9b0b20

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ edition = "2018"
88
chrono = "0.4.6"
99
anyhow = "1"
1010
handlebars = "1.0.3"
11-
itertools = "0.8.0"
1211
env_logger = "0.7"
1312
log = { version = "0.4.5", features = ["serde"] }
1413
rustup-available-packages = { path = "../library/" }
1514
serde = { version = "1", features = [ "derive" ] }
1615
serde_yaml = "0.8"
16+
serde_json = "1"
1717
structopt = "0.2.10"

web/src/main.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod tiers_table;
44
use anyhow::Context;
55
use chrono::{NaiveDate, Utc};
66
use handlebars::{handlebars_helper, Handlebars};
7-
use itertools::{Itertools, Position};
87
use opts::Config;
98
use rustup_available_packages::{cache::FsCache, table::Table, AvailabilityData, Downloader};
109
use serde::Serialize;
10+
use std::collections::HashMap;
1111
use std::{
1212
fmt::Display,
1313
fs::{create_dir_all, File},
@@ -107,15 +107,13 @@ fn packages_json(
107107
pkgs: impl IntoIterator<Item = impl Display>,
108108
path: impl AsRef<Path>,
109109
) -> io::Result<()> {
110-
let mut f = File::create(path)?;
111-
writeln!(f, "[")?;
112-
pkgs.into_iter()
113-
.with_position()
114-
.try_for_each(|pkg| match pkg {
115-
Position::Only(pkg) | Position::Last(pkg) => writeln!(f, "\"{}\"", pkg),
116-
Position::First(pkg) | Position::Middle(pkg) => writeln!(f, "\"{}\",", pkg),
117-
})?;
118-
writeln!(f, "]")
110+
let contents = serde_json::to_vec(
111+
&pkgs
112+
.into_iter()
113+
.map(|p| p.to_string())
114+
.collect::<Vec<String>>(),
115+
)?;
116+
std::fs::write(path, contents)
119117
}
120118

121119
fn generate_fs_tree(
@@ -148,24 +146,30 @@ fn generate_fs_tree(
148146
// or output corrupt data.
149147
if dates.len() == row.availability_list.len() {
150148
let path = target_path.join(&format!("{}.json", pkg));
151-
let mut f = File::create(&path)
152-
.with_context(|| format!("Can't create file {}", path.display()))?;
153-
write!(f, "{{")?;
154-
for (date, avail) in dates.iter().zip(row.availability_list.iter()) {
155-
write!(f, "\"{}\":{},", date.format("%Y-%m-%d"), avail)?;
156-
}
157-
if let Some(date) = row.last_available {
158-
write!(f, "\"last_available\":\"{}\"", date.format("%Y-%m-%d"))?;
159-
} else {
160-
write!(f, "\"last_available\":null")?;
161-
}
162-
write!(f, "}}")?;
149+
150+
let contents = serde_json::to_vec_pretty(&TargetPkg {
151+
availability: dates
152+
.iter()
153+
.zip(row.availability_list.iter())
154+
.map(|(date, avail)| (date.format("%Y-%m-%d").to_string(), *avail))
155+
.collect(),
156+
last_available: row.last_available.map(|d| d.format("%Y-%m-%d").to_string()),
157+
})?;
158+
std::fs::write(&path, contents)
159+
.with_context(|| format!("Can't write file {}", path.display()))?;
163160
}
164161
}
165162
}
166163
Ok(())
167164
}
168165

166+
#[derive(serde::Serialize)]
167+
struct TargetPkg {
168+
#[serde(flatten)]
169+
availability: HashMap<String, bool>,
170+
last_available: Option<String>,
171+
}
172+
169173
fn main() -> anyhow::Result<()> {
170174
let cmd_opts = CmdOpts::from_args();
171175
let config = match cmd_opts {

0 commit comments

Comments
 (0)