use opfs::persistent::{DirectoryHandle, FileHandle, WritableFileStream, app_specific_dir};
use opfs::{GetFileHandleOptions, CreateWritableOptions};
use opfs::{DirectoryHandle as _, FileHandle as _, WritableFileStream as _};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir: DirectoryHandle = app_specific_dir().await?;
let options = GetFileHandleOptions { create: true };
let mut file: FileHandle = dir.get_file_handle_with_options("hello.txt", &options).await?;
let write_options = CreateWritableOptions { keep_existing_data: false };
let mut writer: WritableFileStream = file.create_writable_with_options(&write_options).await?;
let message = b"Hello from OPFS! This works on both native and web platforms.";
writer.write_at_cursor_pos(message.to_vec()).await?;
writer.close().await?;
let data = file.read().await?;
let content = String::from_utf8(data)?;
println!("File contents: {}", content);
println!("File size: {} bytes", file.size().await?);
Ok(())
}