Skip to content

Commit 7c148de

Browse files
authored
Improve error types in webrtc-constraints, using thiserror (webrtc-rs#486)
1 parent 713d86f commit 7c148de

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

constraints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rust-version = "1.63.0"
1616
indexmap = "1.9.1"
1717
serde = { version = "1.0.137", features = ["derive"], optional = true }
1818
ordered-float = { version = "3.0.0", default-features = false }
19+
thiserror = "1.0"
1920

2021
[dev-dependencies]
2122
env_logger = "0.9.0"

constraints/src/algorithms/select_settings.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::collections::HashSet;
22

3+
use thiserror::Error;
4+
35
use crate::algorithms::fitness_distance::SettingFitnessDistanceError;
46
use crate::errors::OverconstrainedError;
57
use crate::{MediaTrackSettings, SanitizedMediaTrackConstraints};
@@ -24,16 +26,11 @@ pub enum DeviceInformationExposureMode {
2426
}
2527

2628
/// An error type indicating a failure of the `SelectSettings` algorithm.
27-
#[derive(Clone, Eq, PartialEq, Debug)]
29+
#[derive(Error, Clone, Eq, PartialEq, Debug)]
2830
pub enum SelectSettingsError {
2931
/// An error caused by one or more over-constrained settings.
30-
Overconstrained(OverconstrainedError),
31-
}
32-
33-
impl From<OverconstrainedError> for SelectSettingsError {
34-
fn from(error: OverconstrainedError) -> Self {
35-
Self::Overconstrained(error)
36-
}
32+
#[error(transparent)]
33+
Overconstrained(#[from] OverconstrainedError),
3734
}
3835

3936
/// This function implements steps 1-5 of the `SelectSettings` algorithm

constraints/src/errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
55
use std::collections::HashMap;
66

7+
use thiserror::Error;
8+
79
use crate::algorithms::{ConstraintFailureInfo, SettingFitnessDistanceErrorKind};
810
use crate::MediaTrackProperty;
911

1012
/// An error indicating one or more over-constrained settings.
11-
#[derive(Clone, Eq, PartialEq, Debug)]
13+
#[derive(Error, Clone, Eq, PartialEq, Debug)]
1214
pub struct OverconstrainedError {
1315
/// The offending constraint's name.
1416
pub constraint: MediaTrackProperty,
@@ -27,16 +29,14 @@ impl Default for OverconstrainedError {
2729

2830
impl std::fmt::Display for OverconstrainedError {
2931
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30-
write!(f, "Overconstrained property {:?}", self.constraint)?;
32+
write!(f, "Over-constrained property {:?}", self.constraint)?;
3133
if let Some(message) = self.message.as_ref() {
3234
write!(f, ": {message}")?;
3335
}
3436
Ok(())
3537
}
3638
}
3739

38-
impl std::error::Error for OverconstrainedError {}
39-
4040
impl OverconstrainedError {
4141
pub(super) fn exposing_device_information(
4242
failed_constraints: HashMap<MediaTrackProperty, ConstraintFailureInfo>,

0 commit comments

Comments
 (0)