iota_graphql_rpc/types/
intersect.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5/// Merges two filter fields. If both values exist, `merge` is used to combine
6/// them, which returns some combined value if there is some consistent
7/// combination, and `None` otherwise. The overall function returns
8/// `Some(None)`, if the filters combined to no filter, `Some(Some(f))` if the
9/// filters combined to `f`, and `None` if the filters couldn't be combined.
10pub(crate) fn field<T>(
11    this: Option<T>,
12    that: Option<T>,
13    merge: impl FnOnce(T, T) -> Option<T>,
14) -> Option<Option<T>> {
15    match (this, that) {
16        (None, None) => Some(None),
17        (Some(this), None) => Some(Some(this)),
18        (None, Some(that)) => Some(Some(that)),
19        (Some(this), Some(that)) => merge(this, that).map(Some),
20    }
21}
22
23/// Merge options by equality check (equal values get merged, everything else is
24/// inconsistent).
25pub(crate) fn by_eq<T: Eq>(a: T, b: T) -> Option<T> {
26    (a == b).then_some(a)
27}
28
29/// Merge options by taking the max.
30pub(crate) fn by_max<T: Ord>(a: T, b: T) -> Option<T> {
31    Some(a.max(b))
32}
33
34/// Merge options by taking the min.
35pub(crate) fn by_min<T: Ord>(a: T, b: T) -> Option<T> {
36    Some(a.min(b))
37}