zrx_stream/stream/function.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
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};
29
30use zrx_scheduler::action::Error;
31
32mod adapter;
33mod argument;
34mod traits;
35
36pub use adapter::{with_id, with_splat};
37pub use argument::Splat;
38pub use traits::*;
39
40// ----------------------------------------------------------------------------
41// Functions
42// ----------------------------------------------------------------------------
43
44/// Catches panics and converts them to errors.
45///
46/// This function is useful for wrapping code that may panic, i.e., to shield
47/// against panics in user-defined code or third-party libraries. It captures
48/// the panic and returns it as an [`Error::Panic`], allowing the program to
49/// continue running gracefully instead of terminating unexpectedly.
50///
51/// All of the function traits that we provide use this internally to ensure
52/// that any panic is caught and converted to an error. Thus, it's absolutely
53/// recommended to wrap any user-defined function in this function when
54/// implementing a custom function trait.
55///
56/// # Errors
57///
58/// Returns [`Error::Panic`] if the provided function panics.
59///
60/// # Examples
61///
62/// ```
63/// use zrx_scheduler::action::Error;
64/// use zrx_stream::function::catch;
65///
66/// // Define function that panics
67/// let res = catch(|| {
68/// panic!("don't panic!");
69/// Ok(42) // Never returned
70/// });
71///
72/// // Assert that panic was caught
73/// assert!(matches!(res, Err(Error::Panic(_))));
74/// ```
75#[inline]
76pub fn catch<F, T>(f: F) -> Result<T, Error>
77where
78 F: FnOnce() -> Result<T, Error>,
79{
80 panic::catch_unwind(AssertUnwindSafe(f))
81 .map_err(Error::Panic)
82 .and_then(|res| res)
83}