examples: Return anyhow::Result out of main()

It is annoying to see only a single line of error when debugging a chain
of (e.g. `anyhow::Context::context()`-created) errors, and have a
zero exit-code while at it.

Instead Rust supports returning a `Result` type straight from main which
is `Debug`- instead of `Display`-printed, so that - in the case of
`anyhow::Error` - all nested (via `.source()`) `Error`s are printed in
backtrace-like form, and the exit code is appropriately non-zero.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1336>
This commit is contained in:
Marijn Suijten 2023-10-27 21:02:20 +02:00
parent 1ec4560b62
commit ce98a4755e
2 changed files with 12 additions and 12 deletions

View file

@ -1,5 +1,7 @@
#![allow(clippy::non_send_fields_in_send_ty)] #![allow(clippy::non_send_fields_in_send_ty)]
use anyhow::Result;
#[path = "../glupload.rs"] #[path = "../glupload.rs"]
mod glupload; mod glupload;
use glupload::*; use glupload::*;
@ -161,14 +163,12 @@ mod mirror {
} }
} }
fn example_main() { fn example_main() -> Result<()> {
gst::init().unwrap(); gst::init().unwrap();
let glfilter = mirror::GLMirrorFilter::new(Some("foo")); let glfilter = mirror::GLMirrorFilter::new(Some("foo"));
App::new(Some(glfilter.as_ref())) App::new(Some(glfilter.as_ref())).and_then(main_loop)
.and_then(main_loop)
.unwrap_or_else(|e| eprintln!("Error! {e}"))
} }
fn main() { fn main() -> Result<()> {
examples_common::run(example_main); examples_common::run(example_main)
} }

View file

@ -1,5 +1,7 @@
#![allow(clippy::non_send_fields_in_send_ty)] #![allow(clippy::non_send_fields_in_send_ty)]
use anyhow::Result;
#[path = "../glupload.rs"] #[path = "../glupload.rs"]
mod glupload; mod glupload;
use glupload::*; use glupload::*;
@ -7,12 +9,10 @@ use glupload::*;
#[path = "../examples-common.rs"] #[path = "../examples-common.rs"]
pub mod examples_common; pub mod examples_common;
fn example_main() { fn example_main() -> Result<()> {
App::new(None) App::new(None).and_then(main_loop)
.and_then(main_loop)
.unwrap_or_else(|e| eprintln!("Error! {e}"))
} }
fn main() { fn main() -> Result<()> {
examples_common::run(example_main); examples_common::run(example_main)
} }