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

fix: remove duplication of code to load stdlib files #2868

Merged
merged 2 commits into from
Sep 27, 2023
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
59 changes: 28 additions & 31 deletions compiler/fm/src/file_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,30 @@
path.starts_with("std/")
}

fn get_stdlib_asset(path: &Path) -> std::io::Result<String> {
if !is_stdlib_asset(path) {
return Err(Error::new(ErrorKind::InvalidInput, "requested a non-stdlib asset"));
}

match StdLibAssets::get(path.to_str().unwrap()) {
Some(std_lib_asset) => {
Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string())
}

None => Err(Error::new(ErrorKind::NotFound, "invalid stdlib path")),
}
}

pub(crate) fn read_file_to_string(path_to_file: &Path) -> std::io::Result<String> {
if is_stdlib_asset(path_to_file) {
get_stdlib_asset(path_to_file)
} else {
get_non_stdlib_asset(path_to_file)
}
}

cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] {

Check warning on line 47 in compiler/fm/src/file_reader.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (wasi)
use wasm_bindgen::{prelude::*, JsValue};

#[wasm_bindgen(module = "@noir-lang/source-resolver")]
Expand All @@ -33,42 +55,17 @@

}

pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result<String, Error> {
fn get_non_stdlib_asset(path_to_file: &Path) -> std::io::Result<String> {
let path_str = path_to_file.to_str().unwrap();
match StdLibAssets::get(path_str) {

Some(std_lib_asset) => {
Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string())
},

None if is_stdlib_asset(path_to_file) => {
Err(Error::new(ErrorKind::NotFound, "invalid stdlib path"))
}

None => match read_file(path_str) {
Ok(buffer) => Ok(buffer),
Err(_) => Err(Error::new(ErrorKind::Other, "could not read file using wasm")),
}

match read_file(path_str) {
Ok(buffer) => Ok(buffer),
Err(_) => Err(Error::new(ErrorKind::Other, "could not read file using wasm")),
}
}
} else {

pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result<String, Error> {

match StdLibAssets::get(path_to_file.to_str().unwrap()) {

Some(std_lib_asset) => {
Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string())
},

None if is_stdlib_asset(path_to_file) => {
Err(Error::new(ErrorKind::NotFound, "invalid stdlib path"))
}

None => std::fs::read_to_string(path_to_file)

}
fn get_non_stdlib_asset(path_to_file: &Path) -> std::io::Result<String> {
std::fs::read_to_string(path_to_file)
}
}
}