Skip to content

Commit

Permalink
Allow for relative paths in read_json_file (#5)
Browse files Browse the repository at this point in the history
* allow for relative paths in `read_json_file`

* cleaning
  • Loading branch information
rvhonorato authored Jun 12, 2024
1 parent 650f62a commit 3194372
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
25 changes: 24 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
use crate::Interactor;
use std::fs::File;
use std::io::BufReader;
use std::process;

pub fn read_json_file(file_path: &str) -> Result<Vec<Interactor>, Box<dyn std::error::Error>> {
let file = File::open(file_path)?;
let reader = BufReader::new(file);
let data: Vec<Interactor> = serde_json::from_reader(reader)?;
let mut data: Vec<Interactor> = serde_json::from_reader(reader)?;

let wd = std::path::Path::new(file_path).parent().unwrap();

data.iter_mut().for_each(|interactor| {
if !interactor.structure().is_empty() {
let pdb_path = wd.join(interactor.structure());

if pdb_path.exists() {
interactor.set_structure(pdb_path.to_str().unwrap());
} else {
eprintln!("\n### ERROR LOADING STRUCTURE ###");
eprintln!("# The file `{}` does not exist", interactor.structure());
eprintln!(
"# If you are using relative paths, they should be relative to `{}`\n",
file_path
);
process::exit(1);
}

interactor.set_structure(pdb_path.to_str().unwrap());
}
});

Ok(data)
}
8 changes: 0 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn gen_tbl(input_file: &str) {
let mut interactors = input::read_json_file(input_file).unwrap();

let wd = std::path::Path::new(input_file)
.parent()
.unwrap()
.to_str()
.unwrap();

interactors.iter_mut().for_each(|interactor| {
if !interactor.structure().is_empty() {
interactor.set_structure(format!("{}/{}", wd, interactor.structure()).as_str());

if interactor.passive_from_active() {
interactor.set_passive_from_active();
}
Expand Down

0 comments on commit 3194372

Please sign in to comment.