chessbaseLinux/apps/desktop/src/viewed.rs
vrubelroman 3e69a5d57c Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.

The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.

What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).

apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00

87 lines
2.4 KiB
Rust

//! Games the user has opened, remembered by their content (so a mark follows the game
//! when a file is re-sorted or other games are added) in
//! `$XDG_DATA_HOME/chesspenguin/viewed.json`.
use std::collections::HashSet;
use std::path::PathBuf;
#[derive(Default)]
pub struct Viewed {
games: HashSet<u64>,
}
impl Viewed {
fn file() -> Option<PathBuf> {
crate::platform::data_dir().map(|d| d.join("viewed.json"))
}
pub fn load() -> Viewed {
let games = Self::file()
.and_then(|p| crate::platform::fs::read(&p).ok())
.and_then(|b| serde_json::from_slice::<Vec<u64>>(&b).ok())
.unwrap_or_default();
Viewed {
games: games.into_iter().collect(),
}
}
fn save(&self) {
let Some(path) = Self::file() else { return };
let mut list: Vec<u64> = self.games.iter().copied().collect();
list.sort_unstable();
match serde_json::to_vec(&list) {
Ok(bytes) => {
if let Err(e) = crate::settings::write_atomic(&path, &bytes) {
eprintln!("viewed games: {}: {e}", path.display());
}
}
Err(e) => eprintln!("viewed games: {e}"),
}
}
pub fn contains(&self, key: u64) -> bool {
self.games.contains(&key)
}
pub fn set(&mut self, key: u64, viewed: bool) {
let changed = if viewed {
self.games.insert(key)
} else {
self.games.remove(&key)
};
if changed {
self.save();
}
}
}
/// Identity of a game: FNV-1a 64 of its PGN text without surrounding whitespace.
pub fn key(raw: &[u8]) -> u64 {
let start = raw
.iter()
.position(|b| !b.is_ascii_whitespace())
.unwrap_or(raw.len());
let end = raw
.iter()
.rposition(|b| !b.is_ascii_whitespace())
.map_or(start, |i| i + 1);
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in &raw[start..end.max(start)] {
h ^= u64::from(*b);
h = h.wrapping_mul(0x0100_0000_01b3);
}
h
}
#[cfg(test)]
mod tests {
#[test]
fn key_ignores_surrounding_whitespace() {
assert_eq!(
super::key(b"\n[Event \"x\"]\n1. e4 *\n\n"),
super::key(b"[Event \"x\"]\n1. e4 *")
);
assert_ne!(super::key(b"1. e4 *"), super::key(b"1. d4 *"));
assert_eq!(super::key(b" "), super::key(b""));
}
}