Use thiserror more widely for deriving Error/Display impls of error types

This commit is contained in:
Sebastian Dröge 2020-05-26 18:52:49 +03:00
parent f0e12bbf24
commit d417656707
5 changed files with 23 additions and 59 deletions

View file

@ -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 }

View file

@ -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<EncodingAudioProfile, EncodingProfileBuilderError> {
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<EncodingVideoProfile, EncodingProfileBuilderError> {
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<EncodingContainerProfile, EncodingProfileBuilderError> {
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);

View file

@ -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 {

View file

@ -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<u64>);
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
pub struct Percent(pub Option<u32>);
#[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<GenericFormattedValue> + 'static {
fn get_default_format() -> Format;
fn get_format(&self) -> Format;
@ -670,17 +664,10 @@ impl AsMut<Option<u32>> 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<f64> for Percent {
type Error = TryPercentFromFloatError;

View file

@ -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<StructureRef>, PhantomData<StructureRef>);
unsafe impl Send for Structure {}
unsafe impl Sync for Structure {}