rust: start improving errors reporting.

- refactor error![] macro, switch place for $msg & $ok parameters.
- add more meaningful NcError return values in several functions related with poc-menu example, for starters.
- the poc-menu example now doesn't randomly fail, IDKW but OK I guess.
- minor unrelated corrections.
This commit is contained in:
joseLuís 2020-12-30 19:11:55 +01:00
parent 8e5e60f588
commit 31e7305ce8
7 changed files with 55 additions and 33 deletions

View File

@ -3,7 +3,6 @@ use libnotcurses_sys::*;
fn main() -> NcResult<()> {
let nc = Notcurses::new()?;
let stdplane = nc.stdplane()?;
let p1 = NcPlane::new(nc, 0, 0, 20, 30);
for ch in "Initializing cells...".chars() {
let cell = NcCell::with_char7b(ch);

View File

@ -76,7 +76,7 @@ impl NcCell {
channels: NcChannelPair,
) -> NcResult<u32> {
let bytes = crate::cell_prime(plane, cell, gcluster, style, channels);
error![bytes, bytes as u32]
error![bytes, "", bytes as u32]
}
/// Duplicate this NcCell into another one.

View File

@ -71,7 +71,6 @@ impl NcDirect {
pub fn raster_frame(&mut self, faken: &mut NcPlane, align: NcAlign) -> NcResult<()> {
error![
unsafe { crate::ncdirect_raster_frame(self, faken, align) },
(),
"Rastering frame"
]
}
@ -311,6 +310,7 @@ impl NcDirect {
let (mut y, mut x) = (0, 0);
error![
unsafe { crate::ncdirect_cursor_yx(self, &mut y, &mut x) },
"",
(y as NcDimension, x as NcDimension)
]
}

View File

@ -119,18 +119,18 @@ macro_rules! printf {
/// type `()`, and an empty `&str` `""`, respectively.
#[macro_export]
macro_rules! error {
($res:expr, $ok:expr, $msg:expr) => {
($res:expr, $msg:expr, $ok:expr) => {
if $res >= crate::NCRESULT_OK {
return Ok($ok);
} else {
return Err(crate::NcError::with_msg($res, $msg));
}
};
($res:expr, $ok:expr) => {
error![$res, $ok, ""];
($res:expr, $msg:expr) => {
error![$res, $msg, ()];
};
($res:expr) => {
error![$res, (), ""];
error![$res, "", ()];
};
}

View File

@ -113,7 +113,7 @@ impl Notcurses {
/// Returns a Notcurses context, expects [NotcursesOptions].
pub fn with_options<'a>(options: NotcursesOptions) -> NcResult<&'a mut Notcurses> {
let res = unsafe { notcurses_init(&options, null_mut()) };
error_ref_mut![res, "Initializing Notcurses"]
error_ref_mut![res, "Notcurses.with_options()"]
}
/// Returns a Notcurses context. Expects [NcLogLevel] and flags.
@ -355,7 +355,7 @@ impl Notcurses {
let mut blitter = 0;
error![
unsafe { crate::notcurses_lex_blitter(cstring![op], &mut blitter) },
blitter, "Invalid blitter name"
"Invalid blitter name", blitter
]
}
@ -376,7 +376,7 @@ impl Notcurses {
let mut scalemode = 0;
error![
unsafe { crate::notcurses_lex_scalemode(cstring![op], &mut scalemode) },
scalemode
"", scalemode
]
}
@ -412,7 +412,10 @@ impl Notcurses {
///
/// *C style function: [notcurses_mouse_enable()][crate::notcurses_mouse_enable].*
pub fn mouse_enable(&mut self) -> NcResult<()> {
error![unsafe { crate::notcurses_mouse_enable(self) }]
error![
unsafe { crate::notcurses_mouse_enable(self) },
"Notcurses.mouse_enable()"
]
}
/// Returns the number of simultaneous colors claimed to be supported,
@ -440,6 +443,7 @@ impl Notcurses {
let (mut y, mut x) = (0, 0);
error![
unsafe { crate::notcurses_refresh(self, &mut y, &mut x) },
"",
(y as NcDimension, x as NcDimension)
]
}
@ -448,7 +452,10 @@ impl Notcurses {
///
/// *C style function: [notcurses_render()][crate::notcurses_render].*
pub fn render(&mut self) -> NcResult<()> {
error![unsafe { crate::notcurses_render(self) }]
error![
unsafe { crate::notcurses_render(self) },
"Notcurses.render()"
]
}
/// Performs the rendering and rasterization portion of
@ -540,7 +547,10 @@ impl Notcurses {
///
/// *C style function: [notcurses_stdplane()][crate::notcurses_stdplane].*
pub fn stdplane<'a>(&mut self) -> NcResult<&'a mut NcPlane> {
error_ref_mut![unsafe { crate::notcurses_stdplane(self) }]
error_ref_mut![
unsafe { crate::notcurses_stdplane(self) },
"Notcurses.stdplane()"
]
}
/// Returns a reference to the standard [NcPlane] for this terminal.

View File

@ -248,7 +248,7 @@ impl NcPlane {
) -> NcResult<NcDimension> {
let res =
unsafe { crate::ncplane_stain(self, y_stop as i32, x_stop as i32, ul, ur, ll, lr) };
error![res, res as NcDimension];
error![res, "", res as NcDimension];
}
}
@ -410,7 +410,7 @@ impl NcPlane {
) -> NcResult<NcDimension> {
let res =
unsafe { crate::ncplane_format(self, y_stop as i32, x_stop as i32, stylemask as u32) };
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Returns the current styling for this NcPlane.
@ -503,7 +503,7 @@ impl NcPlane {
#[inline]
pub fn at_cursor_cell(&mut self, cell: &mut NcCell) -> NcResult<u32> {
let bytes = unsafe { crate::ncplane_at_cursor_cell(self, cell) };
error![bytes, bytes as u32]
error![bytes, "", bytes as u32]
}
/// Retrieves the current contents of the specified [NcCell], returning the
@ -541,7 +541,7 @@ impl NcPlane {
cell: &mut NcCell,
) -> NcResult<u32> {
let bytes = unsafe { crate::ncplane_at_yx_cell(self, y as i32, x as i32, cell) };
error![bytes, bytes as u32]
error![bytes, "", bytes as u32]
}
/// Extracts this NcPlane's base [NcCell] into `cell`.
@ -570,7 +570,10 @@ impl NcPlane {
stylemask: NcStyleMask,
channels: NcChannelPair,
) -> NcResult<()> {
error![unsafe { crate::ncplane_set_base(self, &(egc as i8), stylemask as u32, channels) }]
error![
unsafe { crate::ncplane_set_base(self, &(egc as i8), stylemask as u32, channels) },
&format!("NcPlane.set_base({}, {}, {})", egc, stylemask, channels)
]
}
/// Sets this NcPlane's base NcCell.
@ -650,7 +653,7 @@ impl NcPlane {
cell: &NcCell,
) -> NcResult<NcDimension> {
let res = unsafe { crate::ncplane_putc_yx(self, y as i32, x as i32, cell) };
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Replaces the NcCell at the current coordinates with the provided NcCell,
@ -662,7 +665,7 @@ impl NcPlane {
/// *C style function: [ncplane_putc()][crate::ncplane_putc].*
pub fn putc(&mut self, cell: &NcCell) -> NcResult<NcDimension> {
let res = crate::ncplane_putc(self, cell);
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Calls [putchar_yx][NcPlane#method.putchar_yx] at the current cursor location.
@ -672,7 +675,7 @@ impl NcPlane {
/// *C style function: [ncplane_putchar()][crate::ncplane_putchar].*
pub fn putchar(&mut self, ch: char) -> NcResult<NcDimension> {
let res = crate::ncplane_putchar(self, ch);
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
// TODO: call put_egc
@ -696,7 +699,7 @@ impl NcPlane {
ch: char,
) -> NcResult<NcDimension> {
let res = crate::ncplane_putchar_yx(self, y, x, ch);
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Writes a series of [NcEgc][crate::NcEgc]s to the current location,
@ -712,7 +715,7 @@ impl NcPlane {
#[inline]
pub fn putstr(&mut self, string: &str) -> NcResult<NcDimension> {
let res = crate::ncplane_putstr(self, string);
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Same as [putstr][NcPlane#method.putstr], but it also tries to move the
@ -742,7 +745,11 @@ impl NcPlane {
string: &str,
) -> NcResult<NcDimension> {
let res = unsafe { crate::ncplane_putstr_aligned(self, y as i32, align, cstring![string]) };
error![res, res as NcDimension]
error![
res,
&format!("NcPlane.putstr_aligned({}, {}, {})", y, align, string),
res as NcDimension
]
}
/// Writes a series of [NcEgc][crate::NcEgc]s to the current location, but
@ -758,7 +765,7 @@ impl NcPlane {
/// *C style function: [ncplane_putstr_stained()][crate::ncplane_putstr_stained].*
pub fn putstr_stained(&mut self, string: &str) -> NcResult<NcDimension> {
let res = unsafe { crate::ncplane_putstr_stained(self, cstring![string]) };
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Write a string, which is a series of [NcEgc][crate::NcEgc]s, to the
@ -780,7 +787,7 @@ impl NcPlane {
string: &str,
) -> NcResult<NcDimension> {
let res = unsafe { crate::ncplane_putstr_yx(self, y as i32, x as i32, cstring![string]) };
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
}
@ -1625,7 +1632,7 @@ impl NcPlane {
x_stop as i32,
)
};
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Draw a gradient with its upper-left corner at the current cursor position,
@ -1647,7 +1654,7 @@ impl NcPlane {
x_len: NcDimension,
) -> NcResult<NcDimension> {
let res = crate::ncplane_gradient_sized(self, egc, stylemask, ul, ur, ll, lr, y_len, x_len);
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Draws a high-resolution gradient using upper blocks and synced backgrounds.
@ -1671,7 +1678,7 @@ impl NcPlane {
let res = unsafe {
crate::ncplane_highgradient(self, ul, ur, ll, lr, y_stop as i32, x_stop as i32)
};
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// [`gradient_sized`][NcPlane#method.gradient_sized]
@ -1690,7 +1697,7 @@ impl NcPlane {
let res = unsafe {
crate::ncplane_highgradient_sized(self, ul, ur, ll, lr, y_stop as i32, x_stop as i32)
};
error![res, res as NcDimension]
error![res, "", res as NcDimension]
}
/// Converts this NcPlane's content to greyscale.

View File

@ -32,9 +32,15 @@ impl NcMenu {
///
/// *C style function: [ncmenu_item_set_status()][crate::ncmenu_item_set_status].*
pub fn item_set_status(&mut self, section: &str, item: &str, enabled: bool) -> NcResult<()> {
error![unsafe {
crate::ncmenu_item_set_status(self, cstring![section], cstring![item], enabled)
}]
error![
unsafe {
crate::ncmenu_item_set_status(self, cstring![section], cstring![item], enabled)
},
&format!(
".item_set_status({:?}, {:?}, {:?}, {})",
self, section, item, enabled
)
]
}
/// Returns the [NcMenuItem][crate::NcMenuItem] description