Hey all,
So with the things I've learned so far regarding .unwrap() and env::current_dir(), I've been attempting to rewrite one of my functions. You can see in the comment at the bottom of the function what my old code was.
fn write_arti_bin(){
let arti_bin = include_bytes!("C:\\Code\\spiderweb\\src\\bins\\arti.exe");
let exe_file = NamedTempFile::with_suffix_in("exe", "./");
if exe_file.is_ok(){
let arti_bin_result = fs::write(exe_file.unwrap().path(), arti_bin);
if arti_bin_result.is_ok(){
println!("Arti Binary was written sucessfully.");
println!("Path: {:?}",exe_file.unwrap().path());
}
else {
println!("Arti binary failed to write. Error: {:#?}", arti_bin_result.err());
}
}else {
println!("Failed to create temprorary file. Error: {:#?}", exe_file.err())
}
/*
let filename= "arti.exe";
let cur_dir = env::current_dir().unwrap();
let full_path = PathBuf::from(cur_dir).join(filename);
fs::write(full_path, arti_bin).expect("Unable to write file.");
*/
}
So I have a few questions:
- Is it appropriate to use
.unwrap()in this line? My thought is that we've already gotten through declaring the temp file by checking it in theifstatement right? Is there a better way of accessing theNamedTempFilepart of theResult<NamedTempFile,Error>withoutunwrap()?
[...]
if exe_file.is_ok(){
let arti_bin_result = fs::write(exe_file.unwrap().path(), arti_bin);
[...]
- I'm getting an error attempting to write out the path of the tempfile with
println!().
println!("Path: {:?}",exe_file.unwrap().path());
This is erroring with: Use of moved value: exe_file. Is that because I already "used" the exe_file object in the if statement? I tried making the println a reference to exe_file but I still received the moved value error.
- Lastly, is there a better way of handling the errors instead of using a bunch of
ifstatements? I think the answer to this one might be to have this function return aResult<(),Error>to the calling line inmain.rsand use?to bubble up the errors but I'm not sure. Actually, now that I think about it, I'll need to return the NamedTempFile too right?
I have another function that happens after this write function that then builds a command to execute this binary...so I would need to return my temp file somehow so that function can act on it right?