Expand description
§OPFS - Origin Private File System
A Rust implementation of the Origin Private File System browser API.
§Usage
The main entry point is the persistent module, which provides platform-agnostic
types that automatically resolve to the correct implementation:
use opfs::persistent::{DirectoryHandle, FileHandle, WritableFileStream, app_specific_dir};
use opfs::{GetFileHandleOptions, CreateWritableOptions};
use opfs::persistent;
// you must import the traits to call methods on the types
use opfs::{DirectoryHandle as _, FileHandle as _, WritableFileStream as _};
// This code works on both native and web platforms
async fn example(dir: DirectoryHandle) -> persistent::Result<()> {
let options = GetFileHandleOptions { create: true };
let mut file = dir.get_file_handle_with_options("example.txt", &options).await?;
let write_options = CreateWritableOptions { keep_existing_data: false };
let mut writer = file.create_writable_with_options(&write_options).await?;
writer.write_at_cursor_pos(b"Hello, world!").await?;
writer.close().await?;
let data = file.read().await?;
println!("File contents: {:?}", String::from_utf8(data));
Ok(())
}
async fn use_example() -> persistent::Result<()> {
let directory: DirectoryHandle = app_specific_dir().await?;
example(directory).await?;
Ok(())
}§Platform-Specific Modules
For advanced use cases, you can also access platform-specific implementations directly:
Modules§
- memory
- “in-memory” filesystem for use in tests or when persistence isn’t necessary
- native
- persistent
Structs§
- Create
Writable Options - File
System Remove Options - GetDirectory
Handle Options - GetFile
Handle Options - Write
Params