From d4176567072dc260a1f598d88c615c3db1c885a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 26 May 2020 18:52:49 +0300 Subject: [PATCH] Use thiserror more widely for deriving Error/Display impls of error types --- gstreamer-pbutils/Cargo.toml | 1 + gstreamer-pbutils/src/encoding_profile.rs | 24 ++++++----------- gstreamer-pbutils/src/lib.rs | 2 ++ gstreamer/src/format.rs | 23 ++++------------ gstreamer/src/structure.rs | 32 +++++------------------ 5 files changed, 23 insertions(+), 59 deletions(-) diff --git a/gstreamer-pbutils/Cargo.toml b/gstreamer-pbutils/Cargo.toml index 17a092a61..cf86ad610 100644 --- a/gstreamer-pbutils/Cargo.toml +++ b/gstreamer-pbutils/Cargo.toml @@ -21,6 +21,7 @@ gstreamer-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs-s gstreamer-pbutils-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs-sys", features = ["v1_8"] } glib = { git = "https://github.com/gtk-rs/glib" } gstreamer = { path = "../gstreamer" } +thiserror = "1.0" [build-dependencies] rustdoc-stripper = { version = "0.1", optional = true } diff --git a/gstreamer-pbutils/src/encoding_profile.rs b/gstreamer-pbutils/src/encoding_profile.rs index f0d59e671..c5698e08e 100644 --- a/gstreamer-pbutils/src/encoding_profile.rs +++ b/gstreamer-pbutils/src/encoding_profile.rs @@ -12,8 +12,7 @@ use gst; use gst_pbutils_sys; use gst_sys; -use std::error; -use std::fmt; +use thiserror::Error; use glib::object::IsA; use glib::translate::*; @@ -257,16 +256,9 @@ impl EncodingContainerProfile { } } -#[derive(Debug, Clone)] -pub struct EncodingProfileBuilderError; - -impl fmt::Display for EncodingProfileBuilderError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "failed to build encoding profile") - } -} - -impl error::Error for EncodingProfileBuilderError {} +#[derive(Debug, Clone, Error)] +#[error("failed to build encoding profile")] +pub struct EncodingProfileBuilderError(()); #[derive(Debug)] struct EncodingProfileBuilderCommonData<'a> { @@ -395,7 +387,7 @@ impl<'a> EncodingAudioProfileBuilder<'a> { pub fn build(self) -> Result { if self.base.format.is_none() { - return Err(EncodingProfileBuilderError); + return Err(EncodingProfileBuilderError(())); } let profile = EncodingAudioProfile::new( @@ -447,7 +439,7 @@ impl<'a> EncodingVideoProfileBuilder<'a> { pub fn build(self) -> Result { if self.base.format.is_none() { - return Err(EncodingProfileBuilderError); + return Err(EncodingProfileBuilderError(())); } let video_profile = EncodingVideoProfile::new( @@ -483,7 +475,7 @@ impl<'a> EncodingContainerProfileBuilder<'a> { pub fn build(self) -> Result { if self.base.format.is_none() { - return Err(EncodingProfileBuilderError); + return Err(EncodingProfileBuilderError(())); } let container_profile = EncodingContainerProfile::new( @@ -496,7 +488,7 @@ impl<'a> EncodingContainerProfileBuilder<'a> { for profile in self.profiles { container_profile .add_profile(&profile) - .or_else(|_error| Err(EncodingProfileBuilderError))?; + .or_else(|_error| Err(EncodingProfileBuilderError(())))?; } set_common_fields(&container_profile, &self.base); diff --git a/gstreamer-pbutils/src/lib.rs b/gstreamer-pbutils/src/lib.rs index 7fe45e5e1..f9c9ca626 100644 --- a/gstreamer-pbutils/src/lib.rs +++ b/gstreamer-pbutils/src/lib.rs @@ -20,6 +20,8 @@ extern crate gstreamer as gst; extern crate gstreamer_pbutils_sys as gst_pbutils_sys; extern crate gstreamer_sys as gst_sys; +extern crate thiserror; + static PBUTILS_INIT: Once = Once::new(); macro_rules! assert_initialized_main_thread { diff --git a/gstreamer/src/format.rs b/gstreamer/src/format.rs index 8df51739b..92b353381 100644 --- a/gstreamer/src/format.rs +++ b/gstreamer/src/format.rs @@ -9,6 +9,7 @@ use muldiv::MulDiv; use std::convert::TryFrom; use std::ops; +use thiserror::Error; use ClockTime; use Format; @@ -36,17 +37,10 @@ pub struct Buffers(pub Option); #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] pub struct Percent(pub Option); -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)] +#[error("invalid generic value format")] pub struct TryFromGenericFormattedValueError(()); -impl std::error::Error for TryFromGenericFormattedValueError {} - -impl std::fmt::Display for TryFromGenericFormattedValueError { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(fmt, "invalid generic value format") - } -} - pub trait FormattedValue: Copy + Clone + Sized + Into + 'static { fn get_default_format() -> Format; fn get_format(&self) -> Format; @@ -670,17 +664,10 @@ impl AsMut> for Percent { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)] +#[error("value out of range")] pub struct TryPercentFromFloatError(()); -impl std::error::Error for TryPercentFromFloatError {} - -impl std::fmt::Display for TryPercentFromFloatError { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(fmt, "value out of range") - } -} - impl TryFrom for Percent { type Error = TryPercentFromFloatError; diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index d8bd897f9..fe58cc665 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -7,7 +7,6 @@ // except according to those terms. use std::borrow::{Borrow, BorrowMut, ToOwned}; -use std::error; use std::ffi::CStr; use std::fmt; use std::marker::PhantomData; @@ -16,6 +15,8 @@ use std::ops::{Deref, DerefMut}; use std::ptr; use std::str; +use thiserror::Error; + use Fraction; use glib; @@ -28,13 +29,14 @@ use glib_sys::gpointer; use gobject_sys; use gst_sys; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Error)] pub enum GetError<'name> { - FieldNotFound { - name: &'name str, - }, + #[error("GetError: Structure field with name {name} not found")] + FieldNotFound { name: &'name str }, + #[error("GetError: Structure field with name {name} not retrieved")] ValueGetError { name: &'name str, + #[source] value_get_error: glib::value::GetError, }, } @@ -54,26 +56,6 @@ impl<'name> GetError<'name> { } } -impl<'name> fmt::Display for GetError<'name> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - GetError::FieldNotFound { name } => { - write!(f, "GetError: Structure field with name {} not found", name) - } - GetError::ValueGetError { - name, - value_get_error, - } => write!( - f, - "GetError: Structure field with name {} value: {}", - name, value_get_error, - ), - } - } -} - -impl<'name> error::Error for GetError<'name> {} - pub struct Structure(ptr::NonNull, PhantomData); unsafe impl Send for Structure {} unsafe impl Sync for Structure {}