Skip to content

Commit

Permalink
Custom Error Type 🦖
Browse files Browse the repository at this point in the history
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   README.md
	modified:   src/capture.rs
	modified:   src/lib.rs
	modified:   windows-capture-python/Cargo.toml
	modified:   windows-capture-python/pyproject.toml
	modified:   windows-capture-python/src/lib.rs
  • Loading branch information
NiiightmareXD committed Dec 6, 2023
1 parent b565bce commit 7f28904
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 59 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windows-capture"
version = "1.0.45"
version = "1.0.47"
authors = ["NiiightmareXD"]
edition = "2021"
description = "Fastest Windows Screen Capture Library For Rust 🔥"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add this library to your `Cargo.toml`:

```toml
[dependencies]
windows-capture = "1.0.45"
windows-capture = "1.0.47"
```
or run this command

Expand Down
19 changes: 9 additions & 10 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::{
os::windows::prelude::AsRawHandle,
sync::{
atomic::{self, AtomicBool},
mpsc::{self, SendError},
Arc,
mpsc, Arc,
},
thread::{self, JoinHandle},
};
Expand Down Expand Up @@ -171,10 +170,10 @@ pub enum WindowsCaptureError<E> {
FailedToSetDispatcherQueueCompletedHandler,
#[error("Graphics Capture Error")]
GraphicsCaptureError(graphics_capture_api::Error),
#[error("Handler Error")]
HandlerError(E),
#[error(transparent)]
FailedToThreadID(SendError<u32>),
#[error("New Handler Error")]
NewHandlerError(E),
#[error("Frame Handler Error")]
FrameHandlerError(E),
}

/// Event Handler Trait
Expand Down Expand Up @@ -218,7 +217,7 @@ pub trait WindowsCaptureHandler: Sized {
info!("Starting Capture Thread");
let result = Arc::new(Mutex::new(None));
let callback = Arc::new(Mutex::new(
Self::new(settings.flags).map_err(WindowsCaptureError::HandlerError)?,
Self::new(settings.flags).map_err(WindowsCaptureError::NewHandlerError)?,
));
let mut capture = GraphicsCaptureApi::new(
settings.item,
Expand Down Expand Up @@ -279,7 +278,7 @@ pub trait WindowsCaptureHandler: Sized {
// Check Handler Result
trace!("Checking Handler Result");
if let Some(e) = result.lock().take() {
return Err(WindowsCaptureError::HandlerError(e));
return Err(WindowsCaptureError::FrameHandlerError(e));
}

Ok(())
Expand Down Expand Up @@ -326,7 +325,7 @@ pub trait WindowsCaptureHandler: Sized {
info!("Starting Capture Thread");
let result = Arc::new(Mutex::new(None));
let callback = Arc::new(Mutex::new(
Self::new(settings.flags).map_err(WindowsCaptureError::HandlerError)?,
Self::new(settings.flags).map_err(WindowsCaptureError::NewHandlerError)?,
));
let mut capture = GraphicsCaptureApi::new(
settings.item,
Expand Down Expand Up @@ -397,7 +396,7 @@ pub trait WindowsCaptureHandler: Sized {
// Check Handler Result
trace!("Checking Handler Result");
if let Some(e) = result.lock().take() {
return Err(WindowsCaptureError::HandlerError(e));
return Err(WindowsCaptureError::FrameHandlerError(e));
}

Ok(())
Expand Down
32 changes: 17 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! ```toml
//! [dependencies]
//! windows-capture = "1.0.45"
//! windows-capture = "1.0.47"
//! ```
//! or run this command
//!
Expand All @@ -33,26 +33,28 @@
//! ## Usage
//!
//! ```no_run
//! use std::error::Error;
//!
//! use windows_capture::{
//! capture::WindowsCaptureHandler,
//! frame::Frame,
//! graphics_capture_api::InternalCaptureControl,
//! settings::{ColorFormat, WindowsCaptureSettings},
//! settings::{ColorFormat, Settings},
//! window::Window,
//! };
//!
//! // Struct To Implement Methods For
//! // Struct To Implement The Trait For
//! struct Capture;
//!
//! impl WindowsCaptureHandler for Capture {
//! type Flags = String; // To Get The Message From The Settings
//! // To Get The Message From The Settings
//! type Flags = String;
//!
//! // To Redirect To CaptureControl Or Start Method
//! type Error = Box<dyn std::error::Error + Send + Sync>;
//!
//! // Function That Will Be Called To Create The Struct The Flags Can Be Passed
//! // From Settings
//! fn new(message: Self::Flags) -> Result<Self, Box<dyn Error + Send + Sync>> {
//! println!("Got The Message: {message}");
//! // From `WindowsCaptureSettings`
//! fn new(message: Self::Flags) -> Result<Self, Self::Error> {
//! println!("Got The Flag: {message}");
//!
//! Ok(Self {})
//! }
Expand All @@ -62,10 +64,10 @@
//! &mut self,
//! frame: &mut Frame,
//! capture_control: InternalCaptureControl,
//! ) -> Result<(), Box<dyn Error + Send + Sync>> {
//! ) -> Result<(), Self::Error> {
//! println!("New Frame Arrived");
//!
//! // Save The Frame As An Image To Specified Path
//! // Save The Frame As An Image To The Specified Path
//! frame.save_as_image("image.png")?;
//!
//! // Gracefully Stop The Capture Thread
Expand All @@ -76,17 +78,17 @@
//!
//! // Called When The Capture Item Closes Usually When The Window Closes, Capture
//! // Session Will End After This Function Ends
//! fn on_closed(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
//! fn on_closed(&mut self) -> Result<(), Self::Error> {
//! println!("Capture Session Closed");
//!
//! Ok(())
//! }
//! }
//!
//! // Checkout Docs For Other Capture Items
//! let foreground_window = Window::foreground().unwrap();
//! let foreground_window = Window::foreground().expect("No Active Window Found");
//!
//! let settings = WindowsCaptureSettings::new(
//! let settings = Settings::new(
//! // Item To Captue
//! foreground_window,
//! // Capture Cursor
Expand All @@ -100,7 +102,7 @@
//! )
//! .unwrap();
//!
//! // Every Error From on_closed and on_frame_arrived Will End Up Here
//! // Every Error From `new`, `on_frame_arrived` and `on_closed` Will End Up Here
//! Capture::start(settings).unwrap();
//! ```
#![warn(clippy::pedantic)]
Expand Down
3 changes: 2 additions & 1 deletion windows-capture-python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windows-capture-python"
version = "1.0.45"
version = "1.0.47"
authors = ["NiiightmareXD"]
edition = "2021"
description = "Fastest Windows Screen Capture Library For Python 🔥"
Expand All @@ -25,4 +25,5 @@ pyo3 = { version = "0.20.0", features = [
"extension-module",
"auto-initialize",
] }
thiserror = "1.0.50"
windows-capture = { path = ".." }
2 changes: 1 addition & 1 deletion windows-capture-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "windows-capture"
version = "1.0.45"
version = "1.0.47"
description = "Fastest Windows Screen Capture Library For Python 🔥"
readme = "README.md"
requires-python = ">=3.9"
Expand Down
Loading

0 comments on commit 7f28904

Please sign in to comment.