use std::fmt::{Display, Formatter};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GuiError {
WidgetNotFound(u64),
InvalidParentChild {
parent: u64,
child: u64,
},
UnsupportedWidget(&'static str),
UnsupportedCapability(String),
Backend(String),
ResourceExhausted {
operation: &'static str,
code: Option<u32>,
},
InvalidNumericInput,
InvalidLayoutGeometry,
UnsupportedLayoutMode(&'static str),
Property(crate::properties::PropertyError),
#[cfg(feature = "styled-text")]
StyledText(crate::styled_text::StyledTextError),
#[cfg(feature = "webview")]
WebView(crate::webview::WebViewError),
}
impl From<crate::properties::PropertyError> for GuiError {
fn from(error: crate::properties::PropertyError) -> Self {
GuiError::Property(error)
}
}
impl GuiError {
pub fn is_resource_exhaustion(&self) -> bool {
match self {
GuiError::ResourceExhausted { .. } => true,
GuiError::Backend(msg) => msg.contains("OS resources exhausted"),
_ => false,
}
}
}
impl Display for GuiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
GuiError::WidgetNotFound(id) => write!(f, "Widget not found: {id}"),
GuiError::InvalidParentChild { parent, child } => {
write!(
f,
"Invalid parent-child relationship: parent={parent}, child={child}"
)
}
GuiError::UnsupportedWidget(kind) => write!(f, "Unsupported widget type: {kind}"),
GuiError::UnsupportedCapability(capability) => {
write!(f, "Unsupported capability: {capability}")
}
GuiError::Backend(msg) => write!(f, "Backend error: {msg}"),
GuiError::ResourceExhausted { operation, code } => {
if let Some(c) = code {
write!(f, "OS resources exhausted during {operation}: code {c}")
} else {
write!(f, "OS resources exhausted during {operation}")
}
}
GuiError::InvalidNumericInput => write!(f, "Invalid numeric input in layout"),
GuiError::InvalidLayoutGeometry => write!(f, "Invalid layout geometry constraints"),
GuiError::UnsupportedLayoutMode(mode) => write!(f, "Unsupported layout mode: {mode}"),
GuiError::Property(error) => write!(f, "Property error: {error}"),
#[cfg(feature = "styled-text")]
GuiError::StyledText(error) => write!(f, "Styled text error: {error}"),
#[cfg(feature = "webview")]
GuiError::WebView(error) => write!(f, "WebView error: {error}"),
}
}
}
impl std::error::Error for GuiError {}
pub type GuiResult<T> = Result<T, GuiError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_resource_exhaustion_matches_typed_and_fallback() {
assert!(GuiError::ResourceExhausted {
operation: "test",
code: None
}
.is_resource_exhaustion());
assert!(
GuiError::Backend("OS resources exhausted: GDI limit".into()).is_resource_exhaustion()
);
assert!(!GuiError::Backend("other error".into()).is_resource_exhaustion());
}
}