use std::{num, ffi, convert};
use errno::{Errno, errno};
use thiserror::Error;
use crate::shims::constants;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum NCurseswFormError {
#[error("nform::{func}() : bad argument")]
BadArgument { func: String },
#[error("nform::{func}() : bad state")]
BadState { func: String },
#[error("nform::{func}() : connected")]
Connected { func: String },
#[error("nform::{func}() : current")]
Current { func: String },
#[error("nform::{func}() : invalid field")]
InvalidField { func: String },
#[error("nform::{func}() : not connected")]
NotConnected { func: String },
#[error("nform::{func}() : not posted")]
NotPosted { func: String },
#[error("nform::{func}() : not selectable")]
NotSelectable { func: String },
#[error("nform::{func}() : no match")]
NoMatch { func: String },
#[error("nform::{func}() : no room")]
NoRoom { func: String },
#[error("nform::{func}() : ok",)]
Ok { func: String },
#[error("nform::{func}() : posted")]
Posted { func: String },
#[error("nform::{func}() : request denied")]
RequestDenied { func: String },
#[error("nform::{}() : {} (#{})", func, errno, errno.0)]
SystemError { func: String, errno: Errno },
#[error("nform::{func}() : unknown command")]
UnknownCommand { func: String },
#[error("nform::{func} : error number {errno}")]
UnknownError { func: String, errno: i32 },
#[error("{source}")]
IntError { #[from] source: num::TryFromIntError },
#[error("{source}")]
NulError { #[from] source: ffi::NulError },
#[error("{source}")]
Infallible { #[from] source: convert::Infallible }
}
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() }
}