Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support the wasm32-wasi target #84

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ use std::fmt::{Debug, Formatter};
use std::io::{BufReader, BufWriter, Cursor, Read, Seek, SeekFrom, Write};
use std::path::Path;

// Workaround the absence of File::try_clone with WASM/WASI without penalizing the other platforms
#[cfg(target_family = "wasm")]
type SharedFile = std::sync::Arc<std::fs::File>;
#[cfg(not(target_family = "wasm"))]
type SharedFile = std::fs::File;

pub struct BufReadWriteFile {
input: BufReader<std::fs::File>,
output: BufWriter<std::fs::File>,
input: BufReader<SharedFile>,
output: BufWriter<SharedFile>,
}

impl BufReadWriteFile {
fn new(file: std::fs::File) -> std::io::Result<Self> {
let input = BufReader::new(file.try_clone()?);
let output = BufWriter::new(file);
fn new(file: SharedFile) -> std::io::Result<Self> {
#[cfg(target_family = "wasm")]
let file_ = file.clone();
#[cfg(not(target_family = "wasm"))]
let file_ = file.try_clone()?;

let input = BufReader::new(file_);
let output = BufWriter::new(file);
Ok(Self { input, output })
}
}
Expand Down Expand Up @@ -706,14 +716,15 @@ impl File<BufReadWriteFile> {
let file = options
.open(path)
.map_err(|error| Error::io_error(error, 0))?;
File::open(BufReadWriteFile::new(file).unwrap())
let source = BufReadWriteFile::new(file.into()).unwrap();
File::open(source)
}

/// Opens an existing dBase file in read only mode
pub fn open_read_only<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let file = std::fs::File::open(path.as_ref()).map_err(|error| Error::io_error(error, 0))?;

let mut file = File::open(BufReadWriteFile::new(file).unwrap())?;
let mut file = File::open(BufReadWriteFile::new(file.into()).unwrap())?;
if file.fields_info.at_least_one_field_is_memo() {
let p = path.as_ref();
let memo_type = file.header.file_type.supported_memo_type();
Expand All @@ -726,7 +737,7 @@ impl File<BufReadWriteFile> {
kind: ErrorKind::ErrorOpeningMemoFile(error),
})?;

let memo_reader = BufReadWriteFile::new(memo_file)
let memo_reader = BufReadWriteFile::new(memo_file.into())
.and_then(|memo_file| MemoReader::new(mt, memo_file))
.map_err(|error| Error::io_error(error, 0))?;

Expand Down Expand Up @@ -760,7 +771,7 @@ impl File<BufReadWriteFile> {
pub fn create<P: AsRef<Path>>(path: P, table_info: TableInfo) -> Result<Self, Error> {
let file = std::fs::File::create(path).map_err(|error| Error::io_error(error, 0))?;

File::create_new(BufReadWriteFile::new(file).unwrap(), table_info)
File::create_new(BufReadWriteFile::new(file.into()).unwrap(), table_info)
}
}

Expand Down
Loading