zrx_stream/stream/
function.rs

1// Copyright (c) Zensical LLC <https://zensical.org>
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under CLA
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Functions for use in operators.
27
28use std::panic::{self, AssertUnwindSafe};
29use zrx_scheduler::action::Error;
30
31mod adapter;
32mod argument;
33mod traits;
34
35pub use adapter::{with_id, with_splat};
36pub use argument::Splat;
37pub use traits::*;
38
39// ----------------------------------------------------------------------------
40// Functions
41// ----------------------------------------------------------------------------
42
43/// Catches panics and converts them to errors.
44///
45/// This function is useful for wrapping code that may panic, i.e., to shield
46/// against panics in user-defined code or third-party libraries. It captures
47/// the panic and returns it as an [`Error::Panic`], allowing the program to
48/// continue running gracefully instead of terminating unexpectedly.
49///
50/// All of the function traits that we provide use this internally to ensure
51/// that any panic is caught and converted to an error. Thus, it's absolutely
52/// recommended to wrap any user-defined function in this function when
53/// implementing a custom function trait.
54///
55/// # Errors
56///
57/// Returns [`Error::Panic`] if the provided function panics.
58///
59/// # Examples
60///
61/// ```
62/// use zrx_scheduler::action::Error;
63/// use zrx_stream::function::catch;
64///
65/// // Define function that panics
66/// let res = catch(|| {
67///     panic!("don't panic!");
68///     Ok(42) // Never returned
69/// });
70///
71/// // Assert that panic was caught
72/// assert!(matches!(res, Err(Error::Panic(_))));
73/// ```
74#[inline]
75pub fn catch<F, T>(f: F) -> Result<T, Error>
76where
77    F: FnOnce() -> Result<T, Error>,
78{
79    panic::catch_unwind(AssertUnwindSafe(f))
80        .map_err(Error::Panic)
81        .and_then(|res| res)
82}