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