feat: dismiss anyhow by replacing with thiserror

WIP/draft
drendog 2024-08-21 12:44:53 +02:00
parent 721c8ca91d
commit fc2e3944b4
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
13 changed files with 179 additions and 99 deletions

8
Cargo.lock generated
View File

@ -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",
]

View File

@ -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"

43
src/errors.rs Normal file
View File

@ -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),
}

View File

@ -1,3 +1,4 @@
mod errors;
mod reexports;
mod rendering;
mod windowing;

View File

@ -8,5 +8,3 @@ pub mod sctk {
pub mod wayland_client {
pub use wayland_client::*;
}
pub use anyhow;

View File

@ -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<WindowSurface>,
}
#[derive(Default)]
pub struct EGLContextBuilder {
display_id: Option<ObjectId>,
@ -31,7 +32,6 @@ pub struct EGLContextBuilder {
context_attributes: Option<ContextAttributesBuilder>,
}
#[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<EGLContext> {
pub fn build(self) -> Result<EGLContext, LayerShikaError> {
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<RawDisplayHandle> {
let display = NonNull::new(display_id.as_ptr().cast::<c_void>())
.ok_or_else(|| anyhow!("Failed to create NonNull pointer for display"))?;
fn create_wayland_display_handle(
display_id: &ObjectId,
) -> Result<RawDisplayHandle, LayerShikaError> {
let display = NonNull::new(display_id.as_ptr().cast::<c_void>()).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<RawDisplayHand
fn select_config(
glutin_display: &Display,
config_template: ConfigTemplateBuilder,
) -> Result<glutin::api::egl::config::Config> {
let mut configs = unsafe { glutin_display.find_configs(config_template.build()) }?;
configs
.next()
.ok_or_else(|| anyhow!("No compatible EGL configurations found."))
) -> Result<glutin::api::egl::config::Config, LayerShikaError> {
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<glutin::api::egl::context::NotCurrentContext> {
) -> Result<glutin::api::egl::context::NotCurrentContext, LayerShikaError> {
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<RawWindowHandle> {
let surface = NonNull::new(surface_id.as_ptr().cast::<c_void>())
.ok_or_else(|| anyhow!("Failed to create NonNull pointer for surface"))?;
fn create_surface_handle(surface_id: &ObjectId) -> Result<RawWindowHandle, LayerShikaError> {
let surface = NonNull::new(surface_id.as_ptr().cast::<c_void>()).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<Surface<WindowSurface>> {
let Some(width) = NonZeroU32::new(size.width) else {
return Err(anyhow!("Width cannot be zero"));
};
) -> Result<Surface<WindowSurface>, 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::<WindowSurface>::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<dyn std::error::Error + Send + Sync>> {
Ok(self.ensure_current()?)
self.ensure_current()
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
fn swap_buffers(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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(

View File

@ -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(())
}

View File

@ -1,4 +1,3 @@
use anyhow::Result;
use slint::{
platform::{Platform, WindowAdapter},
PlatformError,

View File

@ -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<WindowingSystem> {
pub fn build(&mut self) -> Result<WindowingSystem, LayerShikaError> {
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(),
)),
}
}
}

View File

@ -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,)+))
}
};
}

View File

@ -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<Self> {
fn new(config: &mut WindowConfig) -> Result<Self, LayerShikaError> {
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<WindowState>,
) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat)> {
) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat), LayerShikaError> {
let global_list = registry_queue_init::<WindowState>(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<WlSurface>,
display: &WlDisplay,
config: &WindowConfig,
) -> Result<Rc<FemtoVGWindow>> {
) -> Result<Rc<FemtoVGWindow>, 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<WindowState>,
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(())
}

View File

@ -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<WindowState> {
pub fn build(self) -> Result<WindowState, LayerShikaError> {
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)
}

View File

@ -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<Self> {
let component_definition = builder
.component_definition
.context("Component definition is required")?;
pub fn new(builder: WindowStateBuilder) -> Result<Self, LayerShikaError> {
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,