libnotcurses_sys/
error.rs1#[cfg(not(feature = "std"))]
4use alloc::string::{String, ToString};
5
6pub type NcResult<T> = Result<T, NcError>;
8
9#[derive(Debug, Clone, Default)]
11pub struct NcError {
12 pub int: c_api::NcResult_i32,
13 pub msg: String,
14}
15
16impl NcError {
18 pub fn new() -> Self {
21 Self { int: c_api::NCRESULT_ERR, ..Default::default() }
22 }
23
24 pub fn new_err(int: c_api::NcResult_i32) -> Self {
26 Self { int, ..Default::default() }
27 }
28
29 pub fn new_msg(msg: &str) -> Self {
32 Self { int: c_api::NCRESULT_ERR, msg: msg.to_string() }
33 }
34
35 pub fn with_msg(int: c_api::NcResult_i32, msg: &str) -> Self {
37 Self { int, msg: msg.to_string() }
38 }
39}
40
41mod core_impls {
42 use super::NcError;
43 use core::fmt;
44
45 impl fmt::Display for NcError {
46 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
47 write!(f, "NcError {}: {}", self.int, self.msg)
48 }
49 }
50}
51
52#[cfg(feature = "std")]
53mod std_impls {
54 use super::NcError;
55 use std::error::Error;
56
57 impl Error for NcError {
58 fn description(&self) -> &str {
59 &self.msg
60 }
61 }
62}
63
64pub(crate) mod c_api {
65 pub type NcResult_i32 = i32;
76
77 pub const NCRESULT_OK: i32 = 0;
79
80 pub const NCRESULT_ERR: i32 = -1;
82
83 pub const NCRESULT_MAX: i32 = i32::MAX;
85}