1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt::Debug;
use peace_resource_rt::{resources::ts::SetUp, type_reg::untagged::DataType, Resources};
use serde::{Serialize, Serializer};
use crate::{ParamsResolveError, ValueResolutionCtx};
/// Type erased mapping function.
///
/// This is used by Peace to hold type-erased mapping functions, and is not
/// intended to be implemented by users or implementors.
pub trait MappingFn: Debug + DataType {
/// Type that is output by the function.
type Output;
/// Maps data in resources to the output type.
///
/// The data being accessed is defined by the implementation of this
/// function.
///
/// # Parameters
///
/// * `resources`: Resources to resolve values from.
/// * `value_resolution_ctx`: Fields traversed during this value resolution.
fn map(
&self,
resources: &Resources<SetUp>,
value_resolution_ctx: &mut ValueResolutionCtx,
) -> Result<Self::Output, ParamsResolveError>;
/// Maps data in resources to the output type.
///
/// The data being accessed is defined by the implementation of this
/// function.
///
/// # Parameters
///
/// * `resources`: Resources to resolve values from.
/// * `value_resolution_ctx`: Fields traversed during this value resolution.
fn try_map(
&self,
resources: &Resources<SetUp>,
value_resolution_ctx: &mut ValueResolutionCtx,
) -> Result<Option<Self::Output>, ParamsResolveError>;
/// Returns whether this mapping function actually holds the function logic.
///
/// Deserialized mapping functions will not hold any function logic, and
/// Peace uses this function to determine if this is an empty `MappingFn`.
fn is_valued(&self) -> bool;
}
impl<T> Clone for Box<dyn MappingFn<Output = T>> {
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}
impl<T> Serialize for dyn MappingFn<Output = T> + '_ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Sadly the following doesn't work, it says the lifetime of:
// `&'1 self` must outlive `'static`
//
// let data_type: &(dyn DataType + 'a) = &self;
// Serialize::serialize(data_type, serializer)
// so we have to depend on `erased_serde` directly
erased_serde::serialize(self, serializer)
}
}