1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 18:57:39 +02:00

Errors with '?'

This commit is contained in:
Anthony Scemama 2023-10-12 09:51:25 +02:00
parent 6aeecd709b
commit af9bc4360d
3 changed files with 53 additions and 25 deletions

View File

@ -93,7 +93,7 @@ impl ExitCode {
} }
/// Conversion to a C value /// Conversion to a C value
pub fn to_c(self) -> c::trexio_exit_code { pub fn to_c(&self) -> c::trexio_exit_code {
match self { match self {
Self::Failure => c::TREXIO_FAILURE, Self::Failure => c::TREXIO_FAILURE,
Self::Success => c::TREXIO_SUCCESS, Self::Success => c::TREXIO_SUCCESS,
@ -136,4 +136,29 @@ impl ExitCode {
Self::PhaseChange => c::TREXIO_PHASE_CHANGE, Self::PhaseChange => c::TREXIO_PHASE_CHANGE,
} }
} }
pub fn to_str(&self) -> Result<&'static str, Utf8Error> {
let c_error = self.to_c();
let c_buf: *const c_char = unsafe { c::trexio_string_of_error( c_error ) };
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
c_str.to_str()
}
}
use std::fmt;
use std::error::Error;
use std::ffi::CStr;
use std::ffi::c_char;
use std::str::Utf8Error;
impl fmt::Display for ExitCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str().unwrap())
}
}
impl Error for ExitCode {
fn description(&self) -> &str {
self.to_str().unwrap()
}
} }

View File

@ -1,6 +1,8 @@
use std::error::Error;
mod test; mod test;
fn main() { fn main() -> Result<(), Box<dyn Error>> {
println!("============================================"); println!("============================================");
println!(" TREXIO VERSION : {}", trexio::PACKAGE_VERSION ); println!(" TREXIO VERSION : {}", trexio::PACKAGE_VERSION );
println!("============================================"); println!("============================================");
@ -8,6 +10,7 @@ fn main() {
let file_name = "test_write_rust.dir"; let file_name = "test_write_rust.dir";
let back_end = trexio::BackEnd::Text; let back_end = trexio::BackEnd::Text;
test::test_write(file_name, back_end); test::test_write(file_name, back_end)?;
Ok(())
} }

View File

@ -1,6 +1,6 @@
use trexio::back_end::BackEnd; use trexio::back_end::BackEnd;
pub fn test_write(file_name: &str, back_end: BackEnd) { pub fn test_write(file_name: &str, back_end: BackEnd) -> Result<(), trexio::ExitCode> {
// Prepare data to be written // Prepare data to be written
let n_buffers = 5; let n_buffers = 5;
@ -56,32 +56,32 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
println!("{}", file_name); println!("{}", file_name);
assert!( ! trexio::inquire(file_name).unwrap() ); assert!( ! trexio::inquire(file_name)? );
let trex_file = trexio::open(file_name, 'w', back_end).unwrap(); let trex_file = trexio::open(file_name, 'w', back_end)?;
assert!( ! trexio::has_nucleus(trex_file).unwrap() ); assert!( ! trexio::has_nucleus(trex_file)? );
assert!( ! trexio::has_nucleus_num(trex_file).unwrap() ); assert!( ! trexio::has_nucleus_num(trex_file)? );
assert!( ! trexio::has_nucleus_charge(trex_file).unwrap() ); assert!( ! trexio::has_nucleus_charge(trex_file)? );
assert!( ! trexio::has_ao_2e_int(trex_file).unwrap() ); assert!( ! trexio::has_ao_2e_int(trex_file)? );
assert!( ! trexio::has_ao_2e_int_eri(trex_file).unwrap() ); assert!( ! trexio::has_ao_2e_int_eri(trex_file)? );
assert!( ! trexio::has_determinant_list(trex_file).unwrap() ); assert!( ! trexio::has_determinant_list(trex_file)? );
trexio::write_nucleus_num(trex_file, nucleus_num).unwrap(); trexio::write_nucleus_num(trex_file, nucleus_num)?;
trexio::write_nucleus_charge(trex_file, charge).unwrap(); trexio::write_nucleus_charge(trex_file, charge)?;
trexio::write_nucleus_point_group(trex_file, sym_str).unwrap(); trexio::write_nucleus_point_group(trex_file, sym_str)?;
trexio::write_nucleus_coord(trex_file, coord).unwrap(); trexio::write_nucleus_coord(trex_file, coord)?;
trexio::write_nucleus_label(trex_file, label).unwrap(); trexio::write_nucleus_label(trex_file, label)?;
trexio::write_basis_shell_num(trex_file, basis_shell_num).unwrap(); trexio::write_basis_shell_num(trex_file, basis_shell_num)?;
trexio::write_basis_nucleus_index(trex_file, basis_nucleus_index).unwrap(); trexio::write_basis_nucleus_index(trex_file, basis_nucleus_index)?;
trexio::write_state_id(trex_file, state_id).unwrap(); trexio::write_state_id(trex_file, state_id)?;
if ! trexio::has_ao_num(trex_file).unwrap() { if ! trexio::has_ao_num(trex_file)? {
trexio::write_ao_num(trex_file, ao_num).unwrap(); trexio::write_ao_num(trex_file, ao_num)?;
} }
if ! trexio::has_mo_num(trex_file).unwrap() { if ! trexio::has_mo_num(trex_file)? {
trexio::write_mo_num(trex_file, mo_num).unwrap(); trexio::write_mo_num(trex_file, mo_num)?;
} }
let mut energy = Vec::with_capacity(mo_num); let mut energy = Vec::with_capacity(mo_num);
@ -91,6 +91,6 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
} }
println!("{:#?}", energy); println!("{:#?}", energy);
trexio::close(trex_file).expect("Unable to close File"); trexio::close(trex_file)
} }