ltk/
vfs.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Reading themes, fonts, keyboard layouts and an app's own files: from the file system, or in a browser, which has none, from the files the page loaded into memory with `ltk::web::load_resources` or `ltk::web::load_tree`. An app that also builds for the browser reads its files through it.

use std::io;
use std::path::{ Path, PathBuf };

pub fn read_to_string( path: impl AsRef<Path> ) -> io::Result<String>
{
	String::from_utf8( read( path )? ).map_err( |e| io::Error::new( io::ErrorKind::InvalidData, e ) )
}

#[ cfg( not( target_arch = "wasm32" ) ) ]
pub fn read( path: impl AsRef<Path> ) -> io::Result<Vec<u8>>
{
	std::fs::read( path )
}

/// The paths of the entries in `dir`, skipping any that cannot be read.
#[ cfg( not( target_arch = "wasm32" ) ) ]
pub fn read_dir( dir: impl AsRef<Path> ) -> io::Result<Vec<PathBuf>>
{
	Ok( std::fs::read_dir( dir )?.filter_map( Result::ok ).map( |entry| entry.path() ).collect() )
}

#[ cfg( not( target_arch = "wasm32" ) ) ]
pub fn is_file( path: impl AsRef<Path> ) -> bool
{
	path.as_ref().is_file()
}

#[ cfg( not( target_arch = "wasm32" ) ) ]
pub fn is_dir( path: impl AsRef<Path> ) -> bool
{
	path.as_ref().is_dir()
}

#[ cfg( target_arch = "wasm32" ) ]
static FILES: std::sync::Mutex<std::collections::BTreeMap<PathBuf, std::sync::Arc<[u8]>>> = std::sync::Mutex::new( std::collections::BTreeMap::new() );

#[ cfg( target_arch = "wasm32" ) ]
fn files() -> std::sync::MutexGuard<'static, std::collections::BTreeMap<PathBuf, std::sync::Arc<[u8]>>>
{
	FILES.lock().unwrap_or_else( std::sync::PoisonError::into_inner )
}

/// Make `bytes` the contents of the file at `path`.
#[ cfg( target_arch = "wasm32" ) ]
pub( crate ) fn insert( path: impl Into<PathBuf>, bytes: Vec<u8> )
{
	files().insert( path.into(), bytes.into() );
}

#[ cfg( target_arch = "wasm32" ) ]
pub fn read( path: impl AsRef<Path> ) -> io::Result<Vec<u8>>
{
	files().get( path.as_ref() ).map( |bytes| bytes.to_vec() ).ok_or_else( || io::ErrorKind::NotFound.into() )
}

/// The paths of the files and directories directly in `dir`.
#[ cfg( target_arch = "wasm32" ) ]
pub fn read_dir( dir: impl AsRef<Path> ) -> io::Result<Vec<PathBuf>>
{
	let dir = dir.as_ref();
	let mut entries: Vec<PathBuf> = files().keys()
		.filter_map( |path| path.strip_prefix( dir ).ok()?.components().next() )
		.map( |first| dir.join( first ) )
		.collect();
	entries.dedup();
	if entries.is_empty() { return Err( io::ErrorKind::NotFound.into() ); }
	Ok( entries )
}

#[ cfg( target_arch = "wasm32" ) ]
pub fn is_file( path: impl AsRef<Path> ) -> bool
{
	files().contains_key( path.as_ref() )
}

#[ cfg( target_arch = "wasm32" ) ]
pub fn is_dir( path: impl AsRef<Path> ) -> bool
{
	let path = path.as_ref();
	files().keys().any( |file| file != path && file.starts_with( path ) )
}