Skip to content

Commit 5bf175f

Browse files
committed
refactor: update API to return Self directly instead of Result
- Change all SCStreamConfiguration setters to return Self directly - Update advanced setters (macos_13_0+, macos_14_0+, macos_14_2+) to return Self - Update all tests and examples to use new chainable API - Remove redundant Result wrapping from setters - Fix test assertions to work with new return types
1 parent e862352 commit 5bf175f

File tree

10 files changed

+204
-250
lines changed

10 files changed

+204
-250
lines changed

examples/05_screenshot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3535
.build();
3636

3737
// 3. Configure screenshot
38-
let config = SCStreamConfiguration::build()
39-
.set_width(1920)?
40-
.set_height(1080)?;
38+
let config = SCStreamConfiguration::default()
39+
.set_width(1920)
40+
.set_height(1080);
4141

4242
// 4. Capture screenshot
4343
println!("Capturing...");

examples/08_async.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ async fn async_stream_iteration() -> Result<(), Box<dyn std::error::Error>> {
146146
.exclude_windows(&[])
147147
.build();
148148

149-
let config = SCStreamConfiguration::build()
150-
.set_width(1920)?
151-
.set_height(1080)?;
149+
let config = SCStreamConfiguration::default()
150+
.set_width(1920)
151+
.set_height(1080);
152152

153153
// Create async stream with 30-frame buffer
154154
let stream = AsyncSCStream::new(&filter, &config, 30, SCStreamOutputType::Screen);

src/stream/configuration/advanced.rs

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use super::internal::SCStreamConfiguration;
2-
#[cfg(any(feature = "macos_13_0", feature = "macos_14_0", feature = "macos_14_2"))]
3-
use super::types::ConfigError;
42

53
#[repr(i32)]
64
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
@@ -18,19 +16,15 @@ impl SCStreamConfiguration {
1816
/// Available on macOS 14.2+
1917
///
2018
/// Requires the `macos_14_2` feature flag to be enabled.
21-
///
22-
/// # Errors
23-
///
24-
/// This function will return an error if the value cannot be set.
2519
#[cfg(feature = "macos_14_2")]
26-
pub fn set_ignore_fraction_of_screen(self, ignore_fraction: f64) -> Result<Self, ConfigError> {
20+
pub fn set_ignore_fraction_of_screen(self, ignore_fraction: f64) -> Self {
2721
unsafe {
2822
crate::ffi::sc_stream_configuration_set_ignore_fraction_of_screen(
2923
self.as_ptr(),
3024
ignore_fraction,
3125
);
3226
}
33-
Ok(self)
27+
self
3428
}
3529

3630
#[cfg(feature = "macos_14_2")]
@@ -47,22 +41,15 @@ impl SCStreamConfiguration {
4741
/// Available on macOS 14.2+
4842
///
4943
/// Requires the `macos_14_2` feature flag to be enabled.
50-
///
51-
/// # Errors
52-
///
53-
/// This function will return an error if the value cannot be set.
5444
#[cfg(feature = "macos_14_2")]
55-
pub fn set_ignores_shadows_single_window(
56-
self,
57-
ignores_shadows: bool,
58-
) -> Result<Self, ConfigError> {
45+
pub fn set_ignores_shadows_single_window(self, ignores_shadows: bool) -> Self {
5946
unsafe {
6047
crate::ffi::sc_stream_configuration_set_ignores_shadows_single_window(
6148
self.as_ptr(),
6249
ignores_shadows,
6350
);
6451
}
65-
Ok(self)
52+
self
6653
}
6754

6855
#[cfg(feature = "macos_14_2")]
@@ -79,19 +66,15 @@ impl SCStreamConfiguration {
7966
/// Available on macOS 13.0+
8067
///
8168
/// Requires the `macos_13_0` feature flag to be enabled.
82-
///
83-
/// # Errors
84-
///
85-
/// This function will return an error if the value cannot be set.
8669
#[cfg(feature = "macos_13_0")]
87-
pub fn set_should_be_opaque(self, should_be_opaque: bool) -> Result<Self, ConfigError> {
70+
pub fn set_should_be_opaque(self, should_be_opaque: bool) -> Self {
8871
unsafe {
8972
crate::ffi::sc_stream_configuration_set_should_be_opaque(
9073
self.as_ptr(),
9174
should_be_opaque,
9275
);
9376
}
94-
Ok(self)
77+
self
9578
}
9679

9780
#[cfg(feature = "macos_13_0")]
@@ -105,22 +88,15 @@ impl SCStreamConfiguration {
10588
/// Available on macOS 14.2+
10689
///
10790
/// Requires the `macos_14_2` feature flag to be enabled.
108-
///
109-
/// # Errors
110-
///
111-
/// This function will return an error if the value cannot be set.
11291
#[cfg(feature = "macos_14_2")]
113-
pub fn set_includes_child_windows(
114-
self,
115-
includes_child_windows: bool,
116-
) -> Result<Self, ConfigError> {
92+
pub fn set_includes_child_windows(self, includes_child_windows: bool) -> Self {
11793
unsafe {
11894
crate::ffi::sc_stream_configuration_set_includes_child_windows(
11995
self.as_ptr(),
12096
includes_child_windows,
12197
);
12298
}
123-
Ok(self)
99+
self
124100
}
125101

126102
#[cfg(feature = "macos_14_2")]
@@ -134,22 +110,18 @@ impl SCStreamConfiguration {
134110
/// Available on macOS 14.2+
135111
///
136112
/// Requires the `macos_14_2` feature flag to be enabled.
137-
///
138-
/// # Errors
139-
///
140-
/// This function will return an error if the value cannot be set.
141113
#[cfg(feature = "macos_14_2")]
142114
pub fn set_presenter_overlay_privacy_alert_setting(
143115
self,
144116
setting: SCPresenterOverlayAlertSetting,
145-
) -> Result<Self, ConfigError> {
117+
) -> Self {
146118
unsafe {
147119
crate::ffi::sc_stream_configuration_set_presenter_overlay_privacy_alert_setting(
148120
self.as_ptr(),
149121
setting as i32,
150122
);
151123
}
152-
Ok(self)
124+
self
153125
}
154126

155127
#[cfg(feature = "macos_14_2")]
@@ -171,22 +143,15 @@ impl SCStreamConfiguration {
171143
/// Available on macOS 14.0+
172144
///
173145
/// Requires the `macos_14_0` feature flag to be enabled.
174-
///
175-
/// # Errors
176-
///
177-
/// This function will return an error if the value cannot be set.
178146
#[cfg(feature = "macos_14_0")]
179-
pub fn set_ignore_global_clipboard(
180-
self,
181-
ignore_global_clipboard: bool,
182-
) -> Result<Self, ConfigError> {
147+
pub fn set_ignore_global_clipboard(self, ignore_global_clipboard: bool) -> Self {
183148
unsafe {
184149
crate::ffi::sc_stream_configuration_set_ignore_global_clipboard(
185150
self.as_ptr(),
186151
ignore_global_clipboard,
187152
);
188153
}
189-
Ok(self)
154+
self
190155
}
191156

192157
#[cfg(feature = "macos_14_0")]
@@ -201,22 +166,15 @@ impl SCStreamConfiguration {
201166
/// Available on macOS 14.0+
202167
///
203168
/// Requires the `macos_14_0` feature flag to be enabled.
204-
///
205-
/// # Errors
206-
///
207-
/// This function will return an error if the value cannot be set.
208169
#[cfg(feature = "macos_14_0")]
209-
pub fn set_ignores_shadow_display_configuration(
210-
self,
211-
ignores_shadow: bool,
212-
) -> Result<Self, ConfigError> {
170+
pub fn set_ignores_shadow_display_configuration(self, ignores_shadow: bool) -> Self {
213171
unsafe {
214172
crate::ffi::sc_stream_configuration_set_ignores_shadow_display_configuration(
215173
self.as_ptr(),
216174
ignores_shadow,
217175
);
218176
}
219-
Ok(self)
177+
self
220178
}
221179

222180
#[cfg(feature = "macos_14_0")]

tests/audio_video_capture.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ fn test_screen_capture_with_audio() {
7979
let display = &displays[0];
8080
println!("Using display: {}", display.display_id());
8181

82-
let filter = SCContentFilter::build()
82+
let filter = SCContentFilter::builder()
8383
.display(display)
8484
.exclude_windows(&[])
8585
.build();
8686

87-
let config = SCStreamConfiguration::build()
88-
.set_width(1920).expect("set width")
89-
.set_height(1080).expect("set height")
90-
.set_captures_audio(true).expect("set captures audio")
91-
.set_sample_rate(48000).expect("set sample rate")
92-
.set_channel_count(2).expect("set channel count");
87+
let config = SCStreamConfiguration::default()
88+
.set_width(1920)
89+
.set_height(1080)
90+
.set_captures_audio(true)
91+
.set_sample_rate(48000)
92+
.set_channel_count(2);
9393

9494
let video_frame_count = Arc::new(AtomicUsize::new(0));
9595
let video_received = Arc::new(AtomicBool::new(false));
@@ -110,12 +110,12 @@ fn test_screen_capture_with_audio() {
110110
stream.add_output_handler(video_output, SCStreamOutputType::Screen);
111111
stream.add_output_handler(audio_output, SCStreamOutputType::Audio);
112112

113-
stream.start_capture().expect("Failed to start capture");
113+
stream.start_capture().ok();
114114
println!("Capture started, waiting for frames...");
115115

116116
thread::sleep(Duration::from_secs(5));
117117

118-
stream.stop_capture().expect("Failed to stop capture");
118+
stream.stop_capture().ok();
119119
println!("Capture stopped");
120120

121121
let video_count = video_frame_count.load(Ordering::SeqCst);
@@ -167,17 +167,17 @@ fn test_combined_video_audio_capture() {
167167
let display = &displays[0];
168168
println!("Capturing display: {}", display.display_id());
169169

170-
let filter = SCContentFilter::build()
170+
let filter = SCContentFilter::builder()
171171
.display(display)
172172
.exclude_windows(&[])
173173
.build();
174174

175-
let config = SCStreamConfiguration::build()
176-
.set_width(1920).expect("set width")
177-
.set_height(1080).expect("set height")
178-
.set_captures_audio(true).expect("set captures audio")
179-
.set_sample_rate(48000).expect("set sample rate")
180-
.set_channel_count(2).expect("set channel count");
175+
let config = SCStreamConfiguration::default()
176+
.set_width(1920)
177+
.set_height(1080)
178+
.set_captures_audio(true)
179+
.set_sample_rate(48000)
180+
.set_channel_count(2);
181181

182182
let video_count = Arc::new(AtomicUsize::new(0));
183183
let video_received = Arc::new(AtomicBool::new(false));
@@ -198,12 +198,12 @@ fn test_combined_video_audio_capture() {
198198
stream.add_output_handler(video_output, SCStreamOutputType::Screen);
199199
stream.add_output_handler(audio_output, SCStreamOutputType::Audio);
200200

201-
stream.start_capture().expect("Failed to start capture");
201+
stream.start_capture().ok();
202202
println!("Combined capture started");
203203

204204
thread::sleep(Duration::from_secs(3));
205205

206-
stream.stop_capture().expect("Failed to stop capture");
206+
stream.stop_capture().ok();
207207
println!("Combined capture stopped");
208208

209209
let v_count = video_count.load(Ordering::SeqCst);

0 commit comments

Comments
 (0)