From 647d8f8196d3ba264286c52db33b7912b6880e73 Mon Sep 17 00:00:00 2001 From: mohammad5305 Date: Sat, 11 Nov 2023 15:51:17 +0330 Subject: [PATCH] refactor: better err handling using anyhow --- .github/workflows/ci.yaml | 13 ++++----- Cargo.toml | 1 + src/display/x11.rs | 56 +++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5d825f8..43bd16e 100755 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,15 +14,15 @@ jobs: semantic: runs-on: ubuntu-latest outputs: - version: ${{ steps.voutput.outputs.version }} - version_tag: ${{ steps.voutput.outputs.version_tag }} + version: ${{ steps.version.outputs.version }} + version_tag: ${{ steps.version.outputs.version_tag }} steps: - name: Check out repository code uses: actions/checkout@v4 with: fetch-depth: '0' - uses: paulhatch/semantic-version@v5.3.0 - id: voutput + id: version with: tag_prefix: "v" major_pattern: "(MAJOR)" @@ -41,8 +41,10 @@ jobs: needs: semantic strategy: + fail-fast: true matrix: target: [ i686-unknown-linux-gnu, x86_64-unknown-linux-gnu ] + steps: - name: Check out repository code uses: actions/checkout@v4 @@ -64,11 +66,6 @@ jobs: uses: pierotofy/set-swap-space@master with: swap-size-gb: 10 - - name: print tag - run: | - echo ${{ needs.semantic.outputs.version }} - echo ${{ needs.semantic.outputs.version_tag }} - - name: Build target uses: actions-rs/cargo@v1 diff --git a/Cargo.toml b/Cargo.toml index 27199d3..5e048e4 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.75" clap = { version = "4.4.7", default-features = false, features = ["derive", "std", "help", "usage", "error-context"] } fast_image_resize = "2.7.3" image = { version = "0.24.7", default-features = false, features = ["png", "jpeg", "jpeg_rayon"] } diff --git a/src/display/x11.rs b/src/display/x11.rs index 12208bc..c5baced 100755 --- a/src/display/x11.rs +++ b/src/display/x11.rs @@ -1,13 +1,19 @@ +extern crate anyhow; extern crate image; extern crate x11rb; +use anyhow::{Context, Result}; use image::{io::Reader, DynamicImage}; -use std::{borrow::Cow, error::Error, num::NonZeroU32, path::PathBuf}; +use std::{ + borrow::Cow, + num::NonZeroU32, + path::PathBuf, +}; use x11rb::{ connection::Connection, image::{BitsPerPixel, ColorComponent, Image, ImageOrder, PixelLayout, ScanlinePad}, protocol::xproto::*, - rust_connection::RustConnection, + rust_connection::{ParseError::InvalidValue, RustConnection}, wrapper::ConnectionExt as _, }; @@ -34,8 +40,10 @@ fn get_ppid(pid: u32) -> Option> { Some(parents) } -fn get_parent_winid(_conn: &RustConnection, _root: u32) -> Result> { - Ok(var("WINDOWID")?.parse::()?) +fn get_parent_winid(_conn: &RustConnection, _root: u32) -> Result { + Ok(var("WINDOWID") + .context("Failed to get parent Window id")? + .parse::()?) // TODO: getting window id with pid is tricky find a way to handle it // below isn't a correct and working example and sometimes needs to be plused by 12 or 5 idk why :) @@ -61,24 +69,17 @@ fn get_parent_winid(_conn: &RustConnection, _root: u32) -> Result PixelLayout { +fn check_visual(screen: &Screen, id: Visualid) -> Option { // TODO: refactor this // Find the information about the visual and at the same time check its depth. - let visual_info = screen + let (depth, visual_type) = screen .allowed_depths .iter() .filter_map(|depth| { let info = depth.visuals.iter().find(|depth| depth.visual_id == id); info.map(|info| (depth.depth, info)) }) - .next(); - let (depth, visual_type) = match visual_info { - Some(info) => info, - None => { - eprintln!("Did not find the root visual's description?!"); - std::process::exit(1); - } - }; + .next()?; // Check that the pixels have red/green/blue components that we can set directly. match visual_type.class { VisualClass::TRUE_COLOR | VisualClass::DIRECT_COLOR => {} @@ -93,7 +94,7 @@ fn check_visual(screen: &Screen, id: Visualid) -> PixelLayout { let result = PixelLayout::from_visual_type(*visual_type) .expect("The server sent a malformed visual type"); assert_eq!(result.depth(), depth); - result + Some(result) } fn create_image<'a>( @@ -102,7 +103,7 @@ fn create_image<'a>( width: u32, height: u32, pixel_layout: PixelLayout, -) -> Result, Box> { +) -> Result> { let x11_image = Image::new( width as u16, height as u16, @@ -138,23 +139,20 @@ fn resize_image( width: NonZeroU32, height: NonZeroU32, algorithm: fr::ResizeAlg, -) -> Vec { +) -> Result> { let src_image = fr::Image::from_vec_u8( NonZeroU32::new(img.width()).unwrap(), NonZeroU32::new(img.height()).unwrap(), img.to_rgb8().into_raw(), fr::PixelType::U8x3, - ) - .unwrap(); + )?; let mut dst_image = fr::Image::new(width, height, src_image.pixel_type()); let mut resizer = fr::Resizer::new(algorithm); - resizer - .resize(&src_image.view(), &mut dst_image.view_mut()) - .unwrap(); + resizer.resize(&src_image.view(), &mut dst_image.view_mut())?; - dst_image.into_vec() + Ok(dst_image.into_vec()) } pub fn display_image( image_path: PathBuf, @@ -163,10 +161,12 @@ pub fn display_image( width: u32, height: u32, resize_alg: fr::ResizeAlg, -) -> Result> { - let image = Reader::open(image_path)?.decode()?; +) -> Result { + let image = Reader::open(image_path) + .context("Invalid Image format")? + .decode()?; - let (conn, screen_num) = x11rb::connect(None)?; + let (conn, screen_num) = x11rb::connect(None).context("Failed to connect to x11")?; let screen = &conn.setup().roots[screen_num]; let depth = screen.root_depth; @@ -178,14 +178,14 @@ pub fn display_image( let gc = conn.generate_id()?; create_gc(&conn, gc, pixmap, &CreateGCAux::new())?; - let pixel_layout = check_visual(screen, screen.root_visual); + let pixel_layout = check_visual(screen, screen.root_visual).ok_or(InvalidValue)?; let resized_image = resize_image( image, NonZeroU32::new(width).unwrap(), NonZeroU32::new(height).unwrap(), resize_alg, - ); + )?; let x11_image = create_image(&conn, resized_image.as_slice(), width, height, pixel_layout); x11_image?.put(&conn, pixmap, gc, 0, 0)?;