refactor: type game availability state

Game::availability used string labels that were carried through persisted
library data, protocol summaries, and the Tauri-facing game payload. That
allowed invalid states to exist and required legacy summary conversion code to
defensively map strings back into protocol availability values.

Move Availability to lanspread-db and re-export it from lanspread-proto so the
persisted Game type and wire GameSummary type share one serde enum. The JSON
spelling stays Ready, Downloading, or LocalOnly, so the serialized shape does
not change for current library indexes or peer payloads.

Add typed helpers for sentinel-derived download state. Game::set_downloaded
keeps downloaded and Ready/LocalOnly in lockstep and intentionally collapses
non-ready local state, including Downloading, back to LocalOnly. That matches
the current local-summary contract where active operations are suppressed
instead of advertised as Downloading. Game::normalized_availability keeps the
legacy Game-to-summary path from publishing an inconsistent Ready value when
downloaded is false.

Update the follow-up status note so typed availability is no longer listed as
open work.

Test Plan:
- just fmt
- just test
- just clippy
- just build

Refs: none
This commit is contained in:
2026-05-16 11:49:01 +02:00
parent fdad162240
commit be196f9e4b
8 changed files with 128 additions and 93 deletions
+102 -13
View File
@@ -5,10 +5,6 @@ use std::{collections::HashMap, fmt, path::Path};
use serde::{Deserialize, Serialize};
pub const AVAILABILITY_READY: &str = "Ready";
pub const AVAILABILITY_DOWNLOADING: &str = "Downloading";
pub const AVAILABILITY_LOCAL_ONLY: &str = "LocalOnly";
/// Read version from version.ini file
/// # Errors
/// Returns error if file cannot be read or parsed
@@ -34,8 +30,30 @@ pub fn read_version_from_ini(game_dir: &Path) -> eyre::Result<Option<String>> {
}
}
fn default_availability() -> String {
AVAILABILITY_LOCAL_ONLY.to_string()
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Availability {
Ready,
/// Wire-compatible transitional state. Local library summaries currently
/// suppress active operations instead of advertising this value.
Downloading,
#[default]
LocalOnly,
}
impl Availability {
#[must_use]
pub fn from_downloaded(downloaded: bool) -> Self {
if downloaded {
Self::Ready
} else {
Self::LocalOnly
}
}
#[must_use]
pub fn is_downloaded(&self) -> bool {
matches!(self, Self::Ready)
}
}
/// A game
@@ -66,8 +84,8 @@ pub struct Game {
#[serde(default)]
pub installed: bool,
/// Backend-reported availability state for this game's local or peer summary.
#[serde(default = "default_availability")]
pub availability: String,
#[serde(default)]
pub availability: Availability,
/// ETI game version from version.ini (YYYYMMDD format) (server)
pub eti_game_version: Option<String>,
/// Local game version from version.ini (YYYYMMDD format)
@@ -76,6 +94,26 @@ pub struct Game {
pub peer_count: u32,
}
impl Game {
/// Sets sentinel-derived download state and collapses any non-ready
/// availability, including `Downloading`, back to `LocalOnly`.
pub fn set_downloaded(&mut self, downloaded: bool) {
self.downloaded = downloaded;
self.availability = Availability::from_downloaded(downloaded);
}
#[must_use]
pub fn normalized_availability(&self) -> Availability {
if self.downloaded {
Availability::Ready
} else if self.availability.is_downloaded() {
Availability::LocalOnly
} else {
self.availability.clone()
}
}
}
impl fmt::Debug for Game {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
@@ -166,9 +204,8 @@ impl GameDB {
pub fn set_all_uninstalled(&mut self) {
for game in self.games.values_mut() {
game.downloaded = false;
game.set_downloaded(false);
game.installed = false;
game.availability = AVAILABILITY_LOCAL_ONLY.to_string();
game.local_version = None;
}
}
@@ -219,10 +256,30 @@ impl fmt::Debug for GameFileDescription {
mod tests {
use serde_json::json;
use super::{AVAILABILITY_LOCAL_ONLY, Game, GameFileDescription};
use super::{Availability, Game, GameFileDescription};
fn game_fixture() -> Game {
Game {
id: "aoe2".to_string(),
name: "Age of Empires II".to_string(),
description: "desc".to_string(),
release_year: "1999".to_string(),
publisher: "Microsoft".to_string(),
max_players: 8,
version: "1.0".to_string(),
genre: "RTS".to_string(),
size: 123_456,
downloaded: false,
installed: false,
availability: Availability::LocalOnly,
eti_game_version: None,
local_version: None,
peer_count: 0,
}
}
#[test]
fn installed_defaults_to_false_when_missing() {
fn missing_client_state_defaults_to_false_and_local_only() {
let raw = json!({
"id": "aoe2",
"name": "Age of Empires II",
@@ -246,7 +303,39 @@ mod tests {
!game.installed,
"missing installed flag should default to false"
);
assert_eq!(game.availability, AVAILABILITY_LOCAL_ONLY);
assert_eq!(game.availability, Availability::LocalOnly);
}
#[test]
fn download_state_helpers_keep_ready_in_lockstep() {
assert_eq!(Availability::from_downloaded(true), Availability::Ready);
assert_eq!(
Availability::from_downloaded(false),
Availability::LocalOnly
);
let mut game = game_fixture();
game.set_downloaded(true);
assert!(game.downloaded);
assert_eq!(game.availability, Availability::Ready);
game.set_downloaded(false);
assert!(!game.downloaded);
assert_eq!(game.availability, Availability::LocalOnly);
game.availability = Availability::Ready;
assert_eq!(game.normalized_availability(), Availability::LocalOnly);
game.availability = Availability::Downloading;
game.set_downloaded(false);
assert!(!game.downloaded);
assert_eq!(game.availability, Availability::LocalOnly);
game.availability = Availability::Downloading;
assert_eq!(game.normalized_availability(), Availability::Downloading);
game.downloaded = true;
assert_eq!(game.normalized_availability(), Availability::Ready);
}
#[test]