use std::{error::Error,
fmt::{Display, Result}};
use r3bl_rs_utils_core::*;
use crate::*;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct LayoutError {
err_type: LayoutErrorType,
msg: Option<String>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum LayoutErrorType {
MismatchedSurfaceEnd,
MismatchedSurfaceStart,
MismatchedBoxEnd,
StackOfBoxesShouldNotBeEmpty,
InvalidSizePercentage,
ErrorCalculatingNextBoxPos,
ContainerBoxBoundsUndefined,
BoxCursorPositionUndefined,
ContentCursorPositionUndefined,
}
impl Error for LayoutError {}
impl Display for LayoutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result { write!(f, "{self:?}") }
}
impl LayoutError {
pub fn new_err<T>(err_type: LayoutErrorType) -> CommonResult<T> {
Err(LayoutError::new(err_type, None))
}
pub fn new_err_with_msg<T>(
err_type: LayoutErrorType,
msg: String,
) -> CommonResult<T> {
Err(LayoutError::new(err_type, Some(msg)))
}
pub fn new(err_type: LayoutErrorType, msg: Option<String>) -> Box<Self> {
Box::new(LayoutError { err_type, msg })
}
pub fn format_msg_with_stack_len(stack_of_boxes: &Vec<FlexBox>, msg: &str) -> String {
format!("{msg}, stack_of_boxes.len(): {}", stack_of_boxes.len())
}
}
#[macro_export]
macro_rules! unwrap_or_err {
($option:expr, $err_type:expr) => {
match $option {
Some(value) => value,
None => return LayoutError::new_err($err_type),
}
};
($option:expr, $err_type:expr, $msg:expr) => {
match $option {
Some(value) => value,
None => return LayoutError::new_err_with_msg($err_type, $msg.to_string()),
}
};
($option:expr, $err_type:expr, $msg:expr, $($arg:tt)*) => {
match $option {
Some(value) => value,
None => return LayoutError::new_err_with_msg($err_type, format!($msg, $($arg)*)),
}
};
}