use std::{num, ffi, convert};
use errno::{Errno, errno};
use shims::constants;
custom_error::custom_error! {
#[derive(PartialEq, Eq)]
pub NCurseswFormError
BadArgument { func: String } = "nform::{func}() : bad argument",
BadState { func: String } = "nform::{func}() : bad state",
Connected { func: String } = "nform::{func}() : connected",
Current { func: String } = "nform::{func}() : current",
InvalidField { func: String } = "nform::{func}() : invalid field",
NotConnected { func: String } = "nform::{func}() : not connected",
NotPosted { func: String } = "nform::{func}() : not posted",
NotSelectable { func: String } = "nform::{func}() : not selectable",
NoMatch { func: String } = "nform::{func}() : no match",
NoRoom { func: String } = "nform::{func}() : no room",
Ok { func: String } = "nform::{func}() : ok",
Posted { func: String } = "nform::{func}() : posted",
RequestDenied { func: String } = "nform::{func}() : request denied",
SystemError { func: String, errno: Errno } = @{ format!("nform::{}() : {} (#{})", func, errno, errno.0) },
UnknownCommand { func: String } = "nform::{func}() : unknown command",
UnknownError { func: String, errno: i32 } = "nform::{func} : error number {errno}",
IntError { source: num::TryFromIntError } = "{source}",
NulError { source: ffi::NulError } = "{source}",
Infallible { source: convert::Infallible } = "{source}"
}
pub fn ncursesw_form_error_from_rc(func: &str, err: i32) -> NCurseswFormError {
let func = func.to_string();
match err {
constants::E_BAD_ARGUMENT => NCurseswFormError::BadArgument { func },
constants::E_BAD_STATE => NCurseswFormError::BadState { func },
constants::E_CONNECTED => NCurseswFormError::Connected { func },
constants::E_CURRENT => NCurseswFormError::Current { func },
constants::E_INVALID_FIELD => NCurseswFormError::InvalidField { func },
constants::E_NOT_CONNECTED => NCurseswFormError::NotConnected { func },
constants::E_NOT_POSTED => NCurseswFormError::NotPosted { func },
constants::E_NOT_SELECTABLE => NCurseswFormError::NotSelectable { func },
constants::E_NO_MATCH => NCurseswFormError::NoMatch { func },
constants::E_NO_ROOM => NCurseswFormError::NoRoom { func },
constants::E_OK => NCurseswFormError::Ok { func },
constants::E_POSTED => NCurseswFormError::Posted { func },
constants::E_REQUEST_DENIED => NCurseswFormError::RequestDenied { func },
constants::E_SYSTEM_ERROR => NCurseswFormError::SystemError { func, errno: errno() },
constants::E_UNKNOWN_COMMAND => NCurseswFormError::UnknownCommand { func },
_ => NCurseswFormError::UnknownError { func, errno: err }
}
}
pub(in crate::form) fn ncursesw_form_error_system_error(func: &str) -> NCurseswFormError {
NCurseswFormError::SystemError { func: func.to_string(), errno: errno() }
}