From fc2e3944b4bb4609a94a4a613f7657875cfc2323 Mon Sep 17 00:00:00 2001 From: drendog Date: Wed, 21 Aug 2024 12:44:53 +0200 Subject: [PATCH] feat: dismiss anyhow by replacing with thiserror --- Cargo.lock | 8 +-- Cargo.toml | 2 +- src/errors.rs | 43 ++++++++++++++++ src/lib.rs | 1 + src/reexports.rs | 2 - src/rendering/egl_context.rs | 87 +++++++++++++++++++-------------- src/rendering/femtovg_window.rs | 6 +-- src/rendering/slint_platform.rs | 1 - src/windowing/builder.rs | 9 ++-- src/windowing/macros.rs | 4 +- src/windowing/mod.rs | 75 ++++++++++++++++++---------- src/windowing/state/builder.rs | 14 +++--- src/windowing/state/mod.rs | 26 ++++++---- 13 files changed, 179 insertions(+), 99 deletions(-) create mode 100644 src/errors.rs diff --git a/Cargo.lock b/Cargo.lock index 6641f6b..ff9bb25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,12 +189,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anyhow" -version = "1.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" - [[package]] name = "arrayref" version = "0.3.8" @@ -2118,13 +2112,13 @@ dependencies = [ name = "layer-shika" version = "0.1.0" dependencies = [ - "anyhow", "glutin", "log", "raw-window-handle", "slint", "slint-interpreter", "smithay-client-toolkit", + "thiserror", "wayland-client", ] diff --git a/Cargo.toml b/Cargo.toml index d69b99e..6838802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,6 @@ module_name_repetitions = "allow" unwrap_used = "warn" [dependencies] -anyhow = "1.0.86" glutin = { version = "0.32.0", default-features = false, features = [ "wayland", ] } @@ -45,4 +44,5 @@ slint = { version = "1.7.2", default-features = false, features = [ ] } slint-interpreter = "1.7.2" smithay-client-toolkit = "0.19.2" +thiserror = "1.0.63" wayland-client = "0.31.5" diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..a26c107 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,43 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum LayerShikaError { + #[error("Failed to connect to Wayland: {0}")] + WaylandConnection(#[from] wayland_client::ConnectError), + + #[error("Failed to initialize Wayland globals: {0}")] + GlobalInitialization(String), + + #[error("Failed to dispatch Wayland event: {0}")] + WaylandDispatch(String), + + #[error("Failed to create EGL context: {0}")] + EGLContextCreation(String), + + #[error("Failed to create FemtoVG renderer: {0}")] + FemtoVGRendererCreation(String), + + #[error("Failed to create Slint component: {0}")] + SlintComponentCreation(String), + + #[error("Failed to run event loop: {0}")] + EventLoop(String), + + #[error("Window configuration error: {0}")] + WindowConfiguration(String), + + #[error("Rendering error: {0}")] + Rendering(String), + + #[error("Invalid input: {0}")] + InvalidInput(String), + + #[error("Wayland protocol error: {0}")] + WaylandProtocol(String), + + #[error("Failed to set platform: {0}")] + PlatformSetup(String), + + #[error("Failed to flush connection: {0}")] + ConnectionFlush(#[from] wayland_client::backend::WaylandError), +} diff --git a/src/lib.rs b/src/lib.rs index d8a28d5..d88d561 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod errors; mod reexports; mod rendering; mod windowing; diff --git a/src/reexports.rs b/src/reexports.rs index fec9e1c..c929ae0 100644 --- a/src/reexports.rs +++ b/src/reexports.rs @@ -8,5 +8,3 @@ pub mod sctk { pub mod wayland_client { pub use wayland_client::*; } - -pub use anyhow; diff --git a/src/rendering/egl_context.rs b/src/rendering/egl_context.rs index 9675ad2..a34d252 100644 --- a/src/rendering/egl_context.rs +++ b/src/rendering/egl_context.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use crate::errors::LayerShikaError; use glutin::{ api::egl::{context::PossiblyCurrentContext, display::Display, surface::Surface}, config::ConfigTemplateBuilder, @@ -22,6 +22,7 @@ pub struct EGLContext { context: PossiblyCurrentContext, surface: Surface, } + #[derive(Default)] pub struct EGLContextBuilder { display_id: Option, @@ -31,7 +32,6 @@ pub struct EGLContextBuilder { context_attributes: Option, } -#[allow(dead_code)] impl EGLContextBuilder { pub fn new() -> Self { Self::default() @@ -52,11 +52,13 @@ impl EGLContextBuilder { self } + #[allow(dead_code)] pub const fn with_config_template(mut self, config_template: ConfigTemplateBuilder) -> Self { self.config_template = Some(config_template); self } + #[allow(dead_code)] pub const fn with_context_attributes( mut self, context_attributes: ContextAttributesBuilder, @@ -65,17 +67,21 @@ impl EGLContextBuilder { self } - pub fn build(self) -> Result { + pub fn build(self) -> Result { let display_id = self .display_id - .ok_or_else(|| anyhow!("Display ID is required"))?; + .ok_or_else(|| LayerShikaError::InvalidInput("Display ID is required".into()))?; let surface_id = self .surface_id - .ok_or_else(|| anyhow!("Surface ID is required"))?; - let size = self.size.ok_or_else(|| anyhow!("Size is required"))?; + .ok_or_else(|| LayerShikaError::InvalidInput("Surface ID is required".into()))?; + let size = self + .size + .ok_or_else(|| LayerShikaError::InvalidInput("Size is required".into()))?; let display_handle = create_wayland_display_handle(&display_id)?; - let glutin_display = unsafe { Display::new(display_handle) }?; + let glutin_display = unsafe { Display::new(display_handle) }.map_err(|e| { + LayerShikaError::EGLContextCreation(format!("Failed to create display: {e}")) + })?; let config_template = self.config_template.unwrap_or_default(); @@ -90,7 +96,7 @@ impl EGLContextBuilder { let context = context .make_current(&surface) - .map_err(|e| anyhow!("Unable to activate EGL context: {}. This may indicate a problem with the graphics drivers.", e))?; + .map_err(|e| LayerShikaError::EGLContextCreation(format!("Unable to activate EGL context: {e}. This may indicate a problem with the graphics drivers.")))?; Ok(EGLContext { context, surface }) } @@ -101,17 +107,22 @@ impl EGLContext { EGLContextBuilder::new() } - fn ensure_current(&self) -> Result<()> { + fn ensure_current(&self) -> Result<(), LayerShikaError> { if !self.context.is_current() { - self.context.make_current(&self.surface)?; + self.context.make_current(&self.surface).map_err(|e| { + LayerShikaError::EGLContextCreation(format!("Failed to make context current: {e}")) + })?; } Ok(()) } } -fn create_wayland_display_handle(display_id: &ObjectId) -> Result { - let display = NonNull::new(display_id.as_ptr().cast::()) - .ok_or_else(|| anyhow!("Failed to create NonNull pointer for display"))?; +fn create_wayland_display_handle( + display_id: &ObjectId, +) -> Result { + let display = NonNull::new(display_id.as_ptr().cast::()).ok_or_else(|| { + LayerShikaError::InvalidInput("Failed to create NonNull pointer for display".into()) + })?; let handle = WaylandDisplayHandle::new(display); Ok(RawDisplayHandle::Wayland(handle)) } @@ -119,24 +130,27 @@ fn create_wayland_display_handle(display_id: &ObjectId) -> Result Result { - let mut configs = unsafe { glutin_display.find_configs(config_template.build()) }?; - configs - .next() - .ok_or_else(|| anyhow!("No compatible EGL configurations found.")) +) -> Result { + let mut configs = unsafe { glutin_display.find_configs(config_template.build()) } + .map_err(|e| LayerShikaError::EGLContextCreation(format!("Failed to find configs: {e}")))?; + configs.next().ok_or_else(|| { + LayerShikaError::EGLContextCreation("No compatible EGL configurations found.".into()) + }) } + fn create_context( glutin_display: &Display, config: &glutin::api::egl::config::Config, context_attributes: ContextAttributesBuilder, -) -> Result { +) -> Result { unsafe { glutin_display.create_context(config, &context_attributes.build(None)) } - .map_err(|e| anyhow!("Failed to create context: {}", e)) + .map_err(|e| LayerShikaError::EGLContextCreation(format!("Failed to create context: {e}"))) } -fn create_surface_handle(surface_id: &ObjectId) -> Result { - let surface = NonNull::new(surface_id.as_ptr().cast::()) - .ok_or_else(|| anyhow!("Failed to create NonNull pointer for surface"))?; +fn create_surface_handle(surface_id: &ObjectId) -> Result { + let surface = NonNull::new(surface_id.as_ptr().cast::()).ok_or_else(|| { + LayerShikaError::InvalidInput("Failed to create NonNull pointer for surface".into()) + })?; let handle = WaylandWindowHandle::new(surface); Ok(RawWindowHandle::Wayland(handle)) } @@ -146,32 +160,31 @@ fn create_surface( config: &glutin::api::egl::config::Config, surface_handle: RawWindowHandle, size: PhysicalSize, -) -> Result> { - let Some(width) = NonZeroU32::new(size.width) else { - return Err(anyhow!("Width cannot be zero")); - }; +) -> Result, LayerShikaError> { + let width = NonZeroU32::new(size.width) + .ok_or_else(|| LayerShikaError::InvalidInput("Width cannot be zero".into()))?; - let Some(height) = NonZeroU32::new(size.height) else { - return Err(anyhow!("Height cannot be zero")); - }; + let height = NonZeroU32::new(size.height) + .ok_or_else(|| LayerShikaError::InvalidInput("Height cannot be zero".into()))?; let attrs = SurfaceAttributesBuilder::::new().build(surface_handle, width, height); - unsafe { glutin_display.create_window_surface(config, &attrs) } - .map_err(|e| anyhow!("Failed to create window surface: {}", e)) + unsafe { glutin_display.create_window_surface(config, &attrs) }.map_err(|e| { + LayerShikaError::EGLContextCreation(format!("Failed to create window surface: {e}")) + }) } unsafe impl OpenGLInterface for EGLContext { fn ensure_current(&self) -> Result<(), Box> { - Ok(self.ensure_current()?) + self.ensure_current() + .map_err(|e| Box::new(e) as Box) } fn swap_buffers(&self) -> Result<(), Box> { - self.surface - .swap_buffers(&self.context) - .map_err(|e| anyhow!("Failed to swap buffers: {}", e)) - .map_err(Into::into) + self.surface.swap_buffers(&self.context).map_err(|e| { + LayerShikaError::EGLContextCreation(format!("Failed to swap buffers: {e}")).into() + }) } fn resize( diff --git a/src/rendering/femtovg_window.rs b/src/rendering/femtovg_window.rs index 5200a18..91785cf 100644 --- a/src/rendering/femtovg_window.rs +++ b/src/rendering/femtovg_window.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use crate::errors::LayerShikaError; use log::info; use slint::{ platform::{femtovg_renderer::FemtoVGRenderer, Renderer, WindowAdapter, WindowEvent}, @@ -34,14 +34,14 @@ impl FemtoVGWindow { }) } - pub fn render_frame_if_dirty(&self) -> Result<()> { + pub fn render_frame_if_dirty(&self) -> Result<(), LayerShikaError> { if matches!( self.render_state.replace(RenderState::Clean), RenderState::Dirty ) { self.renderer .render() - .map_err(|e| anyhow!("Error rendering frame: {}", e))?; + .map_err(|e| LayerShikaError::Rendering(format!("Error rendering frame: {e}")))?; } Ok(()) } diff --git a/src/rendering/slint_platform.rs b/src/rendering/slint_platform.rs index a4627ca..c290127 100644 --- a/src/rendering/slint_platform.rs +++ b/src/rendering/slint_platform.rs @@ -1,4 +1,3 @@ -use anyhow::Result; use slint::{ platform::{Platform, WindowAdapter}, PlatformError, diff --git a/src/windowing/builder.rs b/src/windowing/builder.rs index 7f3fb98..b47ddf6 100644 --- a/src/windowing/builder.rs +++ b/src/windowing/builder.rs @@ -1,10 +1,11 @@ -use anyhow::Result; use slint_interpreter::ComponentDefinition; use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{ zwlr_layer_shell_v1::{self}, zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity}, }; +use crate::errors::LayerShikaError; + use super::{config::WindowConfig, WindowingSystem}; pub struct WindowingSystemBuilder { @@ -84,10 +85,12 @@ impl WindowingSystemBuilder { } #[allow(clippy::missing_errors_doc)] - pub fn build(&mut self) -> Result { + pub fn build(&mut self) -> Result { match self.config.component_definition { Some(_) => WindowingSystem::new(&mut self.config), - None => Err(anyhow::anyhow!("Slint component not set")), + None => Err(LayerShikaError::InvalidInput( + "Slint component not set".into(), + )), } } } diff --git a/src/windowing/macros.rs b/src/windowing/macros.rs index faaf30f..f0ea52e 100644 --- a/src/windowing/macros.rs +++ b/src/windowing/macros.rs @@ -24,9 +24,9 @@ macro_rules! bind_globals { { $( let $name: $interface = $global_list.bind($queue_handle, $version, ()) - .with_context(|| format!("Failed to bind {}", stringify!($name)))?; + .map_err(|e| LayerShikaError::WaylandDispatch(e.to_string()))?; )+ - Ok::<($($interface,)+), anyhow::Error>(($($name,)+)) + Ok::<($($interface,)+), LayerShikaError>(($($name,)+)) } }; } diff --git a/src/windowing/mod.rs b/src/windowing/mod.rs index 6357ab3..30918d7 100644 --- a/src/windowing/mod.rs +++ b/src/windowing/mod.rs @@ -1,9 +1,9 @@ use self::state::WindowState; use crate::{ bind_globals, + errors::LayerShikaError, rendering::{egl_context::EGLContext, femtovg_window::FemtoVGWindow}, }; -use anyhow::{Context, Result}; use config::WindowConfig; use log::{debug, error, info}; use slint::{platform::femtovg_renderer::FemtoVGRenderer, LogicalPosition, PhysicalSize}; @@ -38,13 +38,15 @@ pub struct WindowingSystem { } impl WindowingSystem { - fn new(config: &mut WindowConfig) -> Result { + fn new(config: &mut WindowConfig) -> Result { info!("Initializing WindowingSystem"); - let connection = Rc::new(Connection::connect_to_env()?); + let connection = + Rc::new(Connection::connect_to_env().map_err(LayerShikaError::WaylandConnection)?); let event_queue = connection.new_event_queue(); let (compositor, output, layer_shell, seat) = - Self::initialize_globals(&connection, &event_queue.handle())?; + Self::initialize_globals(&connection, &event_queue.handle()) + .map_err(|e| LayerShikaError::GlobalInitialization(e.to_string()))?; let (surface, layer_surface) = Self::setup_surface( &compositor, @@ -55,11 +57,11 @@ impl WindowingSystem { ); let pointer = Rc::new(seat.get_pointer(&event_queue.handle(), ())); - let window = Self::initialize_renderer(&surface, &connection.display(), config)?; - let component_definition = config - .component_definition - .take() - .context("Component definition is required")?; + let window = Self::initialize_renderer(&surface, &connection.display(), config) + .map_err(|e| LayerShikaError::EGLContextCreation(e.to_string()))?; + let component_definition = config.component_definition.take().ok_or_else(|| { + LayerShikaError::WindowConfiguration("Component definition is required".to_string()) + })?; let state = WindowStateBuilder::new() .with_component_definition(component_definition) @@ -70,9 +72,11 @@ impl WindowingSystem { .with_height(config.height) .with_exclusive_zone(config.exclusive_zone) .with_window(window) - .build()?; + .build() + .map_err(|e| LayerShikaError::WindowConfiguration(e.to_string()))?; - let event_loop = EventLoop::try_new().context("Failed to create event loop")?; + let event_loop = + EventLoop::try_new().map_err(|e| LayerShikaError::EventLoop(e.to_string()))?; Ok(Self { state, @@ -85,10 +89,10 @@ impl WindowingSystem { fn initialize_globals( connection: &Connection, queue_handle: &QueueHandle, - ) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat)> { + ) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat), LayerShikaError> { let global_list = registry_queue_init::(connection) .map(|(global_list, _)| global_list) - .context("Failed to initialize registry")?; + .map_err(|e| LayerShikaError::GlobalInitialization(e.to_string()))?; let (compositor, output, layer_shell, seat) = bind_globals!( &global_list, @@ -147,16 +151,18 @@ impl WindowingSystem { surface: &Rc, display: &WlDisplay, config: &WindowConfig, - ) -> Result> { + ) -> Result, LayerShikaError> { let init_size = PhysicalSize::new(1, 1); let context = EGLContext::builder() .with_display_id(display.id()) .with_surface_id(surface.id()) .with_size(init_size) - .build()?; + .build() + .map_err(|e| LayerShikaError::EGLContextCreation(e.to_string()))?; - let renderer = FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")?; + let renderer = FemtoVGRenderer::new(context) + .map_err(|e| LayerShikaError::FemtoVGRendererCreation(e.to_string()))?; let femtovg_window = FemtoVGWindow::new(renderer); femtovg_window.set_size(slint::WindowSize::Physical(init_size)); @@ -170,12 +176,22 @@ impl WindowingSystem { self.event_loop.handle() } - pub fn run(&mut self) -> Result<()> { + pub fn run(&mut self) -> Result<(), LayerShikaError> { info!("Starting WindowingSystem main loop"); - while self.event_queue.blocking_dispatch(&mut self.state)? > 0 { - self.connection.flush()?; - self.state.window().render_frame_if_dirty()?; + while self + .event_queue + .blocking_dispatch(&mut self.state) + .map_err(|e| LayerShikaError::WaylandProtocol(e.to_string()))? + > 0 + { + self.connection + .flush() + .map_err(|e| LayerShikaError::WaylandProtocol(e.to_string()))?; + self.state + .window() + .render_frame_if_dirty() + .map_err(|e| LayerShikaError::Rendering(e.to_string()))?; } self.setup_wayland_event_source()?; @@ -189,10 +205,10 @@ impl WindowingSystem { error!("Error processing events: {}", e); } }) - .map_err(|e| anyhow::anyhow!("Failed to run event loop: {}", e)) + .map_err(|e| LayerShikaError::EventLoop(e.to_string())) } - fn setup_wayland_event_source(&self) -> Result<()> { + fn setup_wayland_event_source(&self) -> Result<(), LayerShikaError> { debug!("Setting up Wayland event source"); let connection = Rc::clone(&self.connection); @@ -203,7 +219,7 @@ impl WindowingSystem { calloop::generic::Generic::new(connection, Interest::READ, Mode::Level), move |_, _connection, _shared_data| Ok(PostAction::Continue), ) - .map_err(|e| anyhow::anyhow!("Failed to set up Wayland event source: {}", e))?; + .map_err(|e| LayerShikaError::EventLoop(e.to_string()))?; Ok(()) } @@ -212,19 +228,24 @@ impl WindowingSystem { connection: &Connection, event_queue: &mut EventQueue, shared_data: &mut WindowState, - ) -> Result<()> { + ) -> Result<(), LayerShikaError> { if let Some(guard) = event_queue.prepare_read() { guard .read() - .map_err(|e| anyhow::anyhow!("Failed to read events: {}", e))?; + .map_err(|e| LayerShikaError::WaylandProtocol(e.to_string()))?; } connection.flush()?; - event_queue.dispatch_pending(shared_data)?; + event_queue + .dispatch_pending(shared_data) + .map_err(|e| LayerShikaError::WaylandProtocol(e.to_string()))?; slint::platform::update_timers_and_animations(); - shared_data.window().render_frame_if_dirty()?; + shared_data + .window() + .render_frame_if_dirty() + .map_err(|e| LayerShikaError::Rendering(e.to_string()))?; Ok(()) } diff --git a/src/windowing/state/builder.rs b/src/windowing/state/builder.rs index 66f6727..b5fe076 100644 --- a/src/windowing/state/builder.rs +++ b/src/windowing/state/builder.rs @@ -3,8 +3,7 @@ use slint::PhysicalSize; use slint_interpreter::ComponentDefinition; use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1; use wayland_client::protocol::{wl_pointer::WlPointer, wl_surface::WlSurface}; -use crate::rendering::{femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform}; -use anyhow::{Context, Result}; +use crate::{errors::LayerShikaError, rendering::{femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform}}; use super::WindowState; @@ -76,12 +75,15 @@ impl WindowStateBuilder { self } - pub fn build(self) -> Result { + pub fn build(self) -> Result { let platform = CustomSlintPlatform::new(Rc::clone( - self.window.as_ref().context("Window is required")?, + self.window + .as_ref() + .ok_or_else(|| LayerShikaError::InvalidInput("Window is required".into()))?, )); - slint::platform::set_platform(Box::new(platform)) - .map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?; + slint::platform::set_platform(Box::new(platform)).map_err(|e| { + LayerShikaError::PlatformSetup(format!("Failed to set platform: {e:?}")) + })?; WindowState::new(self) } diff --git a/src/windowing/state/mod.rs b/src/windowing/state/mod.rs index 5f3a6f3..fcbb796 100644 --- a/src/windowing/state/mod.rs +++ b/src/windowing/state/mod.rs @@ -6,7 +6,7 @@ use slint_interpreter::ComponentInstance; use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1; use wayland_client::protocol::wl_surface::WlSurface; use crate::rendering::femtovg_window::FemtoVGWindow; -use anyhow::{Context, Result}; +use crate::errors::LayerShikaError; pub mod builder; pub mod dispatches; @@ -25,23 +25,29 @@ pub struct WindowState { } impl WindowState { - pub fn new(builder: WindowStateBuilder) -> Result { - let component_definition = builder - .component_definition - .context("Component definition is required")?; + pub fn new(builder: WindowStateBuilder) -> Result { + let component_definition = builder.component_definition.ok_or_else(|| { + LayerShikaError::InvalidInput("Component definition is required".into()) + })?; let component_instance = component_definition .create() - .context("Failed to create component instance")?; + .map_err(|e| LayerShikaError::SlintComponentCreation(e.to_string()))?; component_instance .show() - .context("Failed to show component")?; + .map_err(|e| LayerShikaError::SlintComponentCreation(e.to_string()))?; Ok(Self { component_instance, - surface: builder.surface.context("Surface is required")?, - layer_surface: builder.layer_surface.context("Layer surface is required")?, + surface: builder + .surface + .ok_or_else(|| LayerShikaError::InvalidInput("Surface is required".into()))?, + layer_surface: builder + .layer_surface + .ok_or_else(|| LayerShikaError::InvalidInput("Layer surface is required".into()))?, size: builder.size.unwrap_or_default(), output_size: builder.output_size.unwrap_or_default(), - window: builder.window.context("Window is required")?, + window: builder + .window + .ok_or_else(|| LayerShikaError::InvalidInput("Window is required".into()))?, current_pointer_position: LogicalPosition::default(), scale_factor: builder.scale_factor, height: builder.height,