Skip to content

Commit

Permalink
Simplify other Serde code
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed May 21, 2024
1 parent 575108c commit 2227019
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 148 deletions.
68 changes: 29 additions & 39 deletions src/cli/report.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};

use itertools::Itertools;

Expand Down Expand Up @@ -65,69 +65,58 @@ impl From<&BackupError> for SaveError {
}

#[derive(Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiFile {
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
failed: bool,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<SaveError>,
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
ignored: bool,
change: ScanChange,
bytes: u64,
#[serde(rename = "originalPath", skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
original_path: Option<String>,
#[serde(rename = "redirectedPath", skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
redirected_path: Option<String>,
#[serde(
rename = "duplicatedBy",
serialize_with = "crate::serialization::ordered_set",
skip_serializing_if = "crate::serialization::is_empty_set"
)]
duplicated_by: HashSet<String>,
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
duplicated_by: BTreeSet<String>,
}

#[derive(Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiRegistry {
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
failed: bool,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<SaveError>,
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
ignored: bool,
change: ScanChange,
#[serde(
rename = "duplicatedBy",
serialize_with = "crate::serialization::ordered_set",
skip_serializing_if = "crate::serialization::is_empty_set"
)]
duplicated_by: HashSet<String>,
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
duplicated_by: BTreeSet<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
values: BTreeMap<String, ApiRegistryValue>,
}

#[derive(Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiRegistryValue {
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
ignored: bool,
change: ScanChange,
#[serde(
rename = "duplicatedBy",
serialize_with = "crate::serialization::ordered_set",
skip_serializing_if = "crate::serialization::is_empty_set"
)]
duplicated_by: HashSet<String>,
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
duplicated_by: BTreeSet<String>,
}

#[derive(Debug, serde::Serialize)]
#[serde(untagged)]
#[serde(untagged, rename_all = "camelCase")]
enum ApiGame {
Operative {
decision: OperationStepDecision,
change: ScanChange,
#[serde(serialize_with = "crate::serialization::ordered_map")]
files: HashMap<String, ApiFile>,
#[serde(serialize_with = "crate::serialization::ordered_map")]
registry: HashMap<String, ApiRegistry>,
files: BTreeMap<String, ApiFile>,
registry: BTreeMap<String, ApiRegistry>,
},
Stored {
backups: Vec<ApiBackup>,
Expand All @@ -136,6 +125,7 @@ enum ApiGame {
}

#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiBackup {
name: String,
when: chrono::DateTime<chrono::Utc>,
Expand All @@ -147,13 +137,13 @@ struct ApiBackup {
}

#[derive(Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonOutput {
#[serde(skip_serializing_if = "Option::is_none")]
errors: Option<ApiErrors>,
#[serde(skip_serializing_if = "Option::is_none")]
overall: Option<OperationStatus>,
#[serde(serialize_with = "crate::serialization::ordered_map")]
games: HashMap<String, ApiGame>,
games: BTreeMap<String, ApiGame>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -334,8 +324,8 @@ impl Reporter {
}
Self::Json { output } => {
let decision = decision.clone();
let mut files = HashMap::new();
let mut registry = HashMap::new();
let mut files = BTreeMap::new();
let mut registry = BTreeMap::new();

for entry in itertools::sorted(&scan_info.found_files) {
let mut api_file = ApiFile {
Expand All @@ -347,7 +337,7 @@ impl Reporter {
..Default::default()
};
if !duplicate_detector.is_file_duplicated(entry).resolved() {
let mut duplicated_by: HashSet<_> = duplicate_detector.file(entry).into_keys().collect();
let mut duplicated_by: BTreeSet<_> = duplicate_detector.file(entry).into_keys().collect();
duplicated_by.remove(&scan_info.game_name);
api_file.duplicated_by = duplicated_by;
}
Expand Down Expand Up @@ -385,14 +375,14 @@ impl Reporter {
.is_registry_value_duplicated(&entry.path, k)
.resolved()
{
let mut duplicated_by: HashSet<_> = duplicate_detector
let mut duplicated_by: BTreeSet<_> = duplicate_detector
.registry_value(&entry.path, k)
.into_keys()
.collect();
duplicated_by.remove(&scan_info.game_name);
duplicated_by
} else {
HashSet::new()
BTreeSet::new()
}
},
},
Expand All @@ -402,7 +392,7 @@ impl Reporter {
..Default::default()
};
if !duplicate_detector.is_registry_duplicated(&entry.path).resolved() {
let mut duplicated_by: HashSet<_> =
let mut duplicated_by: BTreeSet<_> =
duplicate_detector.registry(&entry.path).into_keys().collect();
duplicated_by.remove(&scan_info.game_name);
api_registry.duplicated_by = duplicated_by;
Expand Down
5 changes: 2 additions & 3 deletions src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ impl RcloneProcess {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename = "camelCase")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RemoteChoice {
None,
Custom,
Expand Down Expand Up @@ -253,7 +252,7 @@ impl ToString for RemoteChoice {
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename = "camelCase")]
#[serde(rename = "camelCase")] // Legacy: Should have been `rename_all`
pub enum Remote {
Custom {
id: String,
Expand Down
32 changes: 15 additions & 17 deletions src/resource/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
num::NonZeroUsize,
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -706,8 +706,7 @@ impl ZstdCompression {
#[serde(default, rename_all = "camelCase")]
pub struct BackupConfig {
pub path: StrictPath,
#[serde(serialize_with = "crate::serialization::ordered_set")]
pub ignored_games: HashSet<String>,
pub ignored_games: BTreeSet<String>,
pub filter: BackupFilter,
pub toggled_paths: ToggledPaths,
pub toggled_registry: ToggledRegistry,
Expand All @@ -720,8 +719,7 @@ pub struct BackupConfig {
#[serde(default, rename_all = "camelCase")]
pub struct RestoreConfig {
pub path: StrictPath,
#[serde(serialize_with = "crate::serialization::ordered_set")]
pub ignored_games: HashSet<String>,
pub ignored_games: BTreeSet<String>,
pub toggled_paths: ToggledPaths,
pub toggled_registry: ToggledRegistry,
pub sort: Sort,
Expand Down Expand Up @@ -801,7 +799,7 @@ impl App {
#[serde(default, rename_all = "camelCase")]
pub struct CustomGame {
pub name: String,
#[serde(skip_serializing_if = "crate::serialization::is_false")]
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub ignore: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,
Expand Down Expand Up @@ -861,7 +859,7 @@ impl Default for BackupConfig {
fn default() -> Self {
Self {
path: default_backup_dir(),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
filter: BackupFilter::default(),
toggled_paths: Default::default(),
toggled_registry: Default::default(),
Expand All @@ -876,7 +874,7 @@ impl Default for RestoreConfig {
fn default() -> Self {
Self {
path: default_backup_dir(),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
toggled_paths: Default::default(),
toggled_registry: Default::default(),
sort: Default::default(),
Expand Down Expand Up @@ -1517,7 +1515,7 @@ impl ToggledRegistry {
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use velcro::{btree_map, hash_set};
use velcro::{btree_map, btree_set};

use super::*;
use crate::testing::s;
Expand Down Expand Up @@ -1554,7 +1552,7 @@ mod tests {
redirects: vec![],
backup: BackupConfig {
path: StrictPath::new(s("~/backup")),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
filter: BackupFilter {
exclude_store_screenshots: false,
..Default::default()
Expand All @@ -1567,7 +1565,7 @@ mod tests {
},
restore: RestoreConfig {
path: StrictPath::new(s("~/restore")),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
toggled_paths: Default::default(),
toggled_registry: Default::default(),
sort: Default::default(),
Expand Down Expand Up @@ -1675,7 +1673,7 @@ mod tests {
}],
backup: BackupConfig {
path: StrictPath::new(s("~/backup")),
ignored_games: hash_set! {
ignored_games: btree_set! {
s("Backup Game 1"),
s("Backup Game 2"),
},
Expand All @@ -1691,7 +1689,7 @@ mod tests {
},
restore: RestoreConfig {
path: StrictPath::new(s("~/restore")),
ignored_games: hash_set! {
ignored_games: btree_set! {
s("Restore Game 1"),
s("Restore Game 2"),
},
Expand Down Expand Up @@ -1780,7 +1778,7 @@ mod tests {
redirects: vec![],
backup: BackupConfig {
path: StrictPath::new(s("~/backup")),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
filter: BackupFilter {
exclude_store_screenshots: false,
..Default::default()
Expand All @@ -1793,7 +1791,7 @@ mod tests {
},
restore: RestoreConfig {
path: StrictPath::new(s("~/restore")),
ignored_games: HashSet::new(),
ignored_games: BTreeSet::new(),
toggled_paths: Default::default(),
toggled_registry: Default::default(),
sort: Default::default(),
Expand Down Expand Up @@ -1930,7 +1928,7 @@ customGames:
}],
backup: BackupConfig {
path: StrictPath::new(s("~/backup")),
ignored_games: hash_set! {
ignored_games: btree_set! {
s("Backup Game 3"),
s("Backup Game 1"),
s("Backup Game 2"),
Expand All @@ -1947,7 +1945,7 @@ customGames:
},
restore: RestoreConfig {
path: StrictPath::new(s("~/restore")),
ignored_games: hash_set! {
ignored_games: btree_set! {
s("Restore Game 3"),
s("Restore Game 1"),
s("Restore Game 2"),
Expand Down
6 changes: 1 addition & 5 deletions src/scan/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,12 @@ impl BackupInfo {
}

#[derive(Clone, Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OperationStatus {
#[serde(rename = "totalGames")]
pub total_games: usize,
#[serde(rename = "totalBytes")]
pub total_bytes: u64,
#[serde(rename = "processedGames")]
pub processed_games: usize,
#[serde(rename = "processedBytes")]
pub processed_bytes: u64,
#[serde(rename = "changedGames")]
pub changed_games: ScanChangeCount,
}

Expand Down
3 changes: 1 addition & 2 deletions src/scan/launchers/heroic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ mod games_config {
#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
pub enum Game {
#[serde(rename_all = "camelCase")]
Config {
#[serde(rename = "winePrefix")]
wine_prefix: String,
#[serde(rename = "wineVersion")]
wine_version: Wine,
},
IgnoreOther(serde::de::IgnoredAny),
Expand Down
Loading

0 comments on commit 2227019

Please sign in to comment.