Skip to main content

zrx_stream/stream/function/signature/
default.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//! Filter map function.
27
28use std::fmt::Display;
29
30use zrx_scheduler::step::Result;
31use zrx_scheduler::Scope;
32
33use crate::stream::function::arguments::{ForId, ForScope, ForValue};
34use crate::stream::function::catch;
35
36// ----------------------------------------------------------------------------
37// Traits
38// ----------------------------------------------------------------------------
39
40/// Default function.
41pub trait DefaultFn<A, I, T>: Send + 'static {
42    /// Executes the map function.
43    ///
44    /// # Errors
45    ///
46    /// This method returns an error if the function fails to execute.
47    fn execute(&self, scope: &Scope<I>) -> Result<Option<T>>;
48}
49
50// ----------------------------------------------------------------------------
51// Blanket implementations
52// ----------------------------------------------------------------------------
53
54impl<F, I, T> DefaultFn<ForScope, I, T> for F
55where
56    F: Fn(&Scope<I>) -> Result<Option<T>> + Send + 'static,
57    I: Display,
58{
59    #[cfg_attr(
60        feature = "tracing",
61        tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
62    )]
63    #[inline]
64    fn execute(&self, scope: &Scope<I>) -> Result<Option<T>> {
65        catch(|| self(scope))
66    }
67}
68
69impl<F, I, T> DefaultFn<ForId, I, T> for F
70where
71    F: Fn(&I) -> Result<Option<T>> + Send + 'static,
72    I: Display,
73{
74    #[cfg_attr(
75        feature = "tracing",
76        tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
77    )]
78    #[inline]
79    fn execute(&self, scope: &Scope<I>) -> Result<Option<T>> {
80        catch(|| self(scope.try_as_id()?))
81    }
82}
83
84impl<F, I, T> DefaultFn<ForValue, I, T> for F
85where
86    F: Fn() -> Result<Option<T>> + Send + 'static,
87    I: Display,
88{
89    #[cfg_attr(
90        feature = "tracing",
91        tracing::instrument(level = "debug", skip_all, fields(scope = %scope))
92    )]
93    #[inline]
94    fn execute(&self, scope: &Scope<I>) -> Result<Option<T>> {
95        catch(self)
96    }
97}