-
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.
[WIP] feat: implement load_abi util function
- Loading branch information
TAMARA LIPOWSKI
committed
Oct 16, 2024
1 parent
5b2bdc0
commit 3fee6bb
Showing
5 changed files
with
120 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,95 @@ | ||
// 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 | ||
} | ||
|
||
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::TempDir; | ||
|
||
#[test] | ||
fn test_load_abi() { | ||
let assets_folder = assets_folder(); | ||
|
||
// Create a test ABI file | ||
let test_abi = r#"{"test": "abi"}"#; | ||
let test_file_path = assets_folder.join("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 loading a non-existent file | ||
let result = load_abi("non_existent"); | ||
assert!(result.is_err()); | ||
assert!(result | ||
.unwrap_err() | ||
.to_string() | ||
.contains("Did you mean one of these? test")); | ||
} | ||
} |