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>
105 lines
2.7 KiB
Rust
105 lines
2.7 KiB
Rust
//! Files of the program and the files the user opened: the disk on a computer; in a
|
|
//! browser the page's files ([`super::web::vfs`]): the program's data kept in the
|
|
//! browser's storage, the user's files in memory for the session.
|
|
|
|
use std::io;
|
|
use std::path::Path;
|
|
|
|
/// What is known of a file.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Meta {
|
|
pub len: u64,
|
|
/// Last change, ms since the Unix epoch (0: not known).
|
|
pub modified_ms: u64,
|
|
pub readonly: bool,
|
|
}
|
|
|
|
pub fn read(path: &Path) -> io::Result<Vec<u8>> {
|
|
#[cfg(not(web))]
|
|
{
|
|
std::fs::read(path)
|
|
}
|
|
#[cfg(web)]
|
|
{
|
|
super::web::vfs::read(path)
|
|
}
|
|
}
|
|
|
|
pub fn read_to_string(path: &Path) -> io::Result<String> {
|
|
String::from_utf8(read(path)?).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
|
|
}
|
|
|
|
/// The first `len` bytes of the file (fewer when it is shorter).
|
|
pub fn read_head(path: &Path, len: usize) -> io::Result<Vec<u8>> {
|
|
#[cfg(not(web))]
|
|
{
|
|
use std::io::Read;
|
|
let mut head = Vec::with_capacity(len);
|
|
std::fs::File::open(path)?
|
|
.take(len as u64)
|
|
.read_to_end(&mut head)?;
|
|
Ok(head)
|
|
}
|
|
#[cfg(web)]
|
|
{
|
|
let mut all = super::web::vfs::read(path)?;
|
|
all.truncate(len);
|
|
Ok(all)
|
|
}
|
|
}
|
|
|
|
/// Writes the file whole: a reader sees the old text or the new one, never a part
|
|
/// (on a computer through a temporary file and a rename; its folder is made).
|
|
pub fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
|
|
#[cfg(not(web))]
|
|
{
|
|
if let Some(dir) = path.parent() {
|
|
std::fs::create_dir_all(dir)?;
|
|
}
|
|
let tmp = path.with_extension(format!("tmp{}", std::process::id()));
|
|
std::fs::write(&tmp, bytes)?;
|
|
std::fs::rename(&tmp, path).inspect_err(|_| {
|
|
let _ = std::fs::remove_file(&tmp);
|
|
})
|
|
}
|
|
#[cfg(web)]
|
|
{
|
|
super::web::vfs::write(path, bytes.to_vec())
|
|
}
|
|
}
|
|
|
|
pub fn exists(path: &Path) -> bool {
|
|
meta(path).is_ok()
|
|
}
|
|
|
|
pub fn meta(path: &Path) -> io::Result<Meta> {
|
|
#[cfg(not(web))]
|
|
{
|
|
let m = std::fs::metadata(path)?;
|
|
let modified_ms = m
|
|
.modified()
|
|
.ok()
|
|
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
|
|
.map_or(0, |d| d.as_millis() as u64);
|
|
Ok(Meta {
|
|
len: m.len(),
|
|
modified_ms,
|
|
readonly: m.permissions().readonly(),
|
|
})
|
|
}
|
|
#[cfg(web)]
|
|
{
|
|
super::web::vfs::meta(path)
|
|
}
|
|
}
|
|
|
|
pub fn remove_file(path: &Path) -> io::Result<()> {
|
|
#[cfg(not(web))]
|
|
{
|
|
std::fs::remove_file(path)
|
|
}
|
|
#[cfg(web)]
|
|
{
|
|
super::web::vfs::remove(path)
|
|
}
|
|
}
|