use std::{error::Error,
fmt::{Debug, Display, Result}};
use super::FlexBox;
use crate::CommonResult;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct LayoutError {
pub error_type: LayoutErrorType,
pub error_message: 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 { Debug::fmt(self, f) }
}
impl LayoutError {
pub fn new_error_result_with_only_type<T>(
err_type: LayoutErrorType,
) -> CommonResult<T> {
core::result::Result::Err(miette::miette!(LayoutError {
error_type: err_type,
error_message: None,
}))
}
pub fn new_error_result<T>(
err_type: LayoutErrorType,
msg: String,
) -> CommonResult<T> {
core::result::Result::Err(miette::miette!(LayoutError {
error_type: err_type,
error_message: Some(msg),
}))
}
#[must_use]
pub fn format_msg_with_stack_len(stack_of_boxes: &[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 $crate::LayoutError::new_error_result_with_only_type($err_type),
}
};
($option:expr, $err_type:expr, $msg:expr) => {
match $option {
Some(value) => value,
None => return $crate::LayoutError::new_error_result($err_type, $msg.to_string()),
}
};
($option:expr, $err_type:expr, $msg:expr, $($arg:tt)*) => {
match $option {
Some(value) => value,
None => return $crate::LayoutError::new_error_result($err_type, format!($msg, $($arg)*)),
}
};
}