-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement load_abi util function
- Loading branch information
TAMARA LIPOWSKI
committed
Oct 17, 2024
1 parent
5b2bdc0
commit c4ea3a0
Showing
5 changed files
with
143 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// TODO: remove skip for clippy dead_code check | ||
#![allow(dead_code)] | ||
|
||
use serde_json::Value; | ||
use std::{ | ||
fs::File, | ||
io::Read, | ||
path::{Path, PathBuf}, | ||
}; | ||
use walkdir::WalkDir; | ||
|
||
fn assets_folder() -> PathBuf { | ||
// Get the directory of the current file (similar to Python's __file__) | ||
let current_file = Path::new(file!()); | ||
|
||
// Go one level up (parent directory) and then add the "assets" folder | ||
let assets_folder = current_file | ||
.parent() | ||
.unwrap() | ||
.join("assets"); | ||
|
||
assets_folder | ||
} | ||
|
||
pub fn load_abi(name_or_path: &str) -> Result<Value, std::io::Error> { | ||
let assets_folder = assets_folder(); | ||
|
||
let path = if Path::new(name_or_path).exists() { | ||
PathBuf::from(name_or_path) | ||
} else { | ||
PathBuf::from(assets_folder.clone()).join(format!("{}.abi", name_or_path)) | ||
}; | ||
|
||
match File::open(&path) { | ||
Ok(mut file) => { | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
Ok(serde_json::from_str(&contents)?) | ||
} | ||
Err(_) => { | ||
let available_files: Vec<String> = WalkDir::new(&assets_folder) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
.filter(|e| { | ||
e.path() | ||
.extension() | ||
.map_or(false, |ext| ext == "abi") | ||
}) | ||
.filter_map(|e| { | ||
e.path() | ||
.strip_prefix(&assets_folder) | ||
.ok() | ||
.map(|p| p.to_owned()) | ||
}) | ||
.filter_map(|p| { | ||
p.to_str() | ||
.map(|s| s.replace(".abi", "")) | ||
}) | ||
.collect(); | ||
|
||
Err(std::io::Error::new( | ||
std::io::ErrorKind::NotFound, | ||
format!( | ||
"File {} not found. Did you mean one of these? {}", | ||
name_or_path, | ||
available_files.join(", ") | ||
), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tempfile::NamedTempFile; | ||
|
||
#[test] | ||
fn test_load_existing_abi() { | ||
let assets_folder = assets_folder(); | ||
|
||
// Create a temporary file in the assets folder | ||
let temp_file = NamedTempFile::new_in(&assets_folder).unwrap(); | ||
let test_file_path = temp_file.path().to_path_buf(); | ||
|
||
// Create a test ABI file | ||
let test_abi = r#"{"test": "abi"}"#; | ||
std::fs::write(&test_file_path, test_abi).unwrap(); | ||
|
||
// Test loading an existing file | ||
let result = load_abi(test_file_path.to_str().unwrap()); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), serde_json::json!({"test": "abi"})); | ||
} | ||
|
||
#[test] | ||
fn test_load_non_existent_abi() { | ||
// Write two abi files, neither of which is the one we will try to load | ||
let assets_folder = assets_folder(); | ||
let test_a_file_path = assets_folder.clone().join("test_a.abi"); | ||
let test_abi = r#"{"test": "abi"}"#; | ||
std::fs::write(&test_a_file_path, test_abi).unwrap(); | ||
|
||
let test_b_file_path = assets_folder.join("test_b.abi"); | ||
std::fs::write(&test_b_file_path, test_abi).unwrap(); | ||
|
||
// Test loading a non-existent file | ||
let result = load_abi("non_existent"); | ||
assert!(result.is_err()); | ||
assert!(result | ||
.unwrap_err() | ||
.to_string() | ||
.contains(&format!("Did you mean one of these? test_a, test_b"))); | ||
|
||
std::fs::remove_file(&test_a_file_path).unwrap(); | ||
std::fs::remove_file(&test_b_file_path).unwrap(); | ||
} | ||
} |