Skip to content

Commit 7339e29

Browse files
authored
Merge pull request #109 from valenotary/106
Replace Capturer::new with Capturer::build
2 parents 691bd88 + 12cb7b8 commit 7339e29

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
.DS_Store
33
/test
4+
/.idea

src/capturer/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::mpsc;
44

55
use crate::{
66
frame::{Frame, FrameType},
7+
has_permission, is_supported, request_permission,
78
targets::Target,
89
};
910

@@ -73,15 +74,40 @@ pub struct Capturer {
7374
rx: mpsc::Receiver<Frame>,
7475
}
7576

77+
pub enum CapturerBuildError {
78+
NotSupported,
79+
PermissionNotGranted,
80+
}
81+
7682
impl Capturer {
7783
/// Create a new capturer instance with the provided options
84+
#[deprecated(
85+
since = "0.0.6",
86+
note = "Use `build` instead of `new` to create a new capturer instance."
87+
)]
7888
pub fn new(options: Options) -> Capturer {
7989
let (tx, rx) = mpsc::channel::<Frame>();
8090
let engine = engine::Engine::new(&options, tx);
8191

8292
Capturer { engine, rx }
8393
}
8494

95+
/// Build a new [Capturer] instance with the provided options
96+
pub fn build(options: Options) -> Result<Capturer, CapturerBuildError> {
97+
if !is_supported() {
98+
return Err(CapturerBuildError::NotSupported);
99+
}
100+
101+
if !has_permission() {
102+
return Err(CapturerBuildError::PermissionNotGranted);
103+
}
104+
105+
let (tx, rx) = mpsc::channel::<Frame>();
106+
let engine = engine::Engine::new(&options, tx);
107+
108+
Ok(Capturer { engine, rx })
109+
}
110+
85111
// TODO
86112
// Prevent starting capture if already started
87113
/// Start capturing the frames

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
use scap::{
55
capturer::{Area, Capturer, Options, Point, Size},
6-
frame::Frame,
7-
Target,
6+
frame::Frame
7+
,
88
};
9+
use std::process;
910

1011
fn main() {
1112
// Check if the platform is supported
@@ -46,7 +47,10 @@ fn main() {
4647
};
4748

4849
// Create Recorder with options
49-
let mut recorder = Capturer::new(options);
50+
let mut recorder = Capturer::build(options).unwrap_or_else(|err| {
51+
println!("Problem with building Capturer: {err}");
52+
process::exit(1);
53+
});
5054

5155
// Start Capture
5256
recorder.start_capture();

0 commit comments

Comments
 (0)