video: Reuse Self::Err in from_str

This commit is contained in:
Marijn Suijten 2020-11-28 12:34:36 +01:00
parent 2447664df6
commit 0763d2645d
10 changed files with 29 additions and 32 deletions

View file

@ -138,14 +138,12 @@ impl crate::AudioFormat {
impl str::FromStr for crate::AudioFormat { impl str::FromStr for crate::AudioFormat {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let fmt = crate::AudioFormat::from_glib(ffi::gst_audio_format_from_string( let fmt = Self::from_glib(ffi::gst_audio_format_from_string(s.to_glib_none().0));
s.to_glib_none().0, if fmt == Self::Unknown {
));
if fmt == crate::AudioFormat::Unknown {
Err(glib::glib_bool_error!( Err(glib::glib_bool_error!(
"Failed to parse audio format from string" "Failed to parse audio format from string"
)) ))

View file

@ -292,10 +292,10 @@ impl fmt::Display for AudioFormatInfo {
impl str::FromStr for crate::AudioFormatInfo { impl str::FromStr for crate::AudioFormatInfo {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
skip_assert_initialized!(); skip_assert_initialized!();
let format = s.parse()?; let format = s.parse()?;
Ok(AudioFormatInfo::from_format(format)) Ok(Self::from_format(format))
} }
} }

View file

@ -339,15 +339,13 @@ impl crate::VideoFormat {
impl str::FromStr for crate::VideoFormat { impl str::FromStr for crate::VideoFormat {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let fmt = crate::VideoFormat::from_glib(ffi::gst_video_format_from_string( let fmt = Self::from_glib(ffi::gst_video_format_from_string(s.to_glib_none().0));
s.to_glib_none().0,
));
if fmt == crate::VideoFormat::Unknown { if fmt == Self::Unknown {
Err(glib::glib_bool_error!( Err(glib::glib_bool_error!(
"Failed to parse video format from string" "Failed to parse video format from string"
)) ))

View file

@ -431,10 +431,10 @@ impl fmt::Display for VideoFormatInfo {
impl str::FromStr for crate::VideoFormatInfo { impl str::FromStr for crate::VideoFormatInfo {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
skip_assert_initialized!(); skip_assert_initialized!();
let format = s.parse()?; let format = s.parse()?;
Ok(VideoFormatInfo::from_format(format)) Ok(Self::from_format(format))
} }
} }

View file

@ -139,7 +139,7 @@ impl Eq for VideoColorimetry {}
impl str::FromStr for crate::VideoColorimetry { impl str::FromStr for crate::VideoColorimetry {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
@ -149,7 +149,7 @@ impl str::FromStr for crate::VideoColorimetry {
s.to_glib_none().0, s.to_glib_none().0,
)); ));
if valid { if valid {
Ok(VideoColorimetry(colorimetry.assume_init())) Ok(Self(colorimetry.assume_init()))
} else { } else {
Err(glib::glib_bool_error!("Invalid colorimetry info")) Err(glib::glib_bool_error!("Invalid colorimetry info"))
} }
@ -179,12 +179,13 @@ impl fmt::Display for crate::VideoColorimetry {
impl str::FromStr for crate::VideoChromaSite { impl str::FromStr for crate::VideoChromaSite {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let chroma_site = from_glib(ffi::gst_video_chroma_from_string(s.to_glib_none().0)); let chroma_site: Self =
if chroma_site == crate::VideoChromaSite::empty() { from_glib(ffi::gst_video_chroma_from_string(s.to_glib_none().0));
if chroma_site.is_empty() {
Err(glib::glib_bool_error!("Invalid chroma site")) Err(glib::glib_bool_error!("Invalid chroma site"))
} else { } else {
Ok(chroma_site) Ok(chroma_site)
@ -1034,7 +1035,7 @@ impl crate::VideoFieldOrder {
impl str::FromStr for crate::VideoFieldOrder { impl str::FromStr for crate::VideoFieldOrder {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
@ -1066,7 +1067,7 @@ impl crate::VideoInterlaceMode {
impl str::FromStr for crate::VideoInterlaceMode { impl str::FromStr for crate::VideoInterlaceMode {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {

View file

@ -475,10 +475,10 @@ generic_impl!(ValidVideoTimeCode);
impl str::FromStr for VideoTimeCode { impl str::FromStr for VideoTimeCode {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
Option::<VideoTimeCode>::from_glib_full(ffi::gst_video_time_code_new_from_string( Option::<Self>::from_glib_full(ffi::gst_video_time_code_new_from_string(
s.to_glib_none().0, s.to_glib_none().0,
)) ))
.ok_or_else(|| glib::glib_bool_error!("Failed to create VideoTimeCode from string")) .ok_or_else(|| glib::glib_bool_error!("Failed to create VideoTimeCode from string"))

View file

@ -120,12 +120,12 @@ impl fmt::Display for VideoTimeCodeInterval {
impl str::FromStr for VideoTimeCodeInterval { impl str::FromStr for VideoTimeCodeInterval {
type Err = glib::error::BoolError; type Err = glib::error::BoolError;
fn from_str(s: &str) -> Result<Self, glib::error::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
Option::<VideoTimeCodeInterval>::from_glib_full( Option::<Self>::from_glib_full(ffi::gst_video_time_code_interval_new_from_string(
ffi::gst_video_time_code_interval_new_from_string(s.to_glib_none().0), s.to_glib_none().0,
) ))
.ok_or_else(|| { .ok_or_else(|| {
glib::glib_bool_error!("Failed to create VideoTimeCodeInterval from string") glib::glib_bool_error!("Failed to create VideoTimeCodeInterval from string")
}) })

View file

@ -161,7 +161,7 @@ impl Caps {
impl str::FromStr for Caps { impl str::FromStr for Caps {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_caps_from_string(s.to_glib_none().0)) Option::<_>::from_glib_full(ffi::gst_caps_from_string(s.to_glib_none().0))

View file

@ -123,7 +123,7 @@ impl fmt::Display for CapsFeatures {
impl str::FromStr for CapsFeatures { impl str::FromStr for CapsFeatures {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let ptr = ffi::gst_caps_features_from_string(s.to_glib_none().0); let ptr = ffi::gst_caps_features_from_string(s.to_glib_none().0);
@ -133,7 +133,7 @@ impl str::FromStr for CapsFeatures {
)); ));
} }
Ok(CapsFeatures(ptr::NonNull::new_unchecked( Ok(Self(ptr::NonNull::new_unchecked(
ptr as *mut CapsFeaturesRef, ptr as *mut CapsFeaturesRef,
))) )))
} }

View file

@ -181,7 +181,7 @@ impl Eq for Structure {}
impl str::FromStr for Structure { impl str::FromStr for Structure {
type Err = glib::BoolError; type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, glib::BoolError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let structure = ffi::gst_structure_from_string(s.to_glib_none().0, ptr::null_mut()); let structure = ffi::gst_structure_from_string(s.to_glib_none().0, ptr::null_mut());
@ -190,7 +190,7 @@ impl str::FromStr for Structure {
"Failed to parse structure from string" "Failed to parse structure from string"
)) ))
} else { } else {
Ok(Structure(ptr::NonNull::new_unchecked( Ok(Self(ptr::NonNull::new_unchecked(
structure as *mut StructureRef, structure as *mut StructureRef,
))) )))
} }