use super::*;
use quither_proc_macros::quither;
#[quither]
impl<L, R> Quither<L, R> {
#[quither(has_either || has_both)]
pub fn left_or(self, #[allow(unused)] other: L) -> L {
match self {
#[either]
Self::Left(l) => l,
#[either]
Self::Right(_) => other,
#[neither]
Self::Neither => other,
#[both]
Self::Both(l, _) => l,
}
}
#[quither(has_either || has_both)]
pub fn right_or(self, #[allow(unused)] other: R) -> R {
match self {
#[either]
Self::Left(_) => other,
#[either]
Self::Right(r) => r,
#[neither]
Self::Neither => other,
#[both]
Self::Both(_, r) => r,
}
}
#[quither(has_either || has_both)]
pub fn both_or(self, #[allow(unused)] l: L, #[allow(unused)] r: R) -> (L, R) {
match self {
#[either]
Self::Left(l2) => (l2, r),
#[either]
Self::Right(r2) => (l, r2),
#[neither]
Self::Neither => (l, r),
#[both]
Self::Both(l2, r2) => (l2, r2),
}
}
#[quither(has_either || has_both)]
pub fn left_or_default(self) -> L
where
L: Default,
{
match self {
#[either]
Self::Left(l) => l,
#[both]
Self::Both(l, _) => l,
#[allow(unused)]
_ => L::default(),
}
}
#[quither(has_either || has_both)]
pub fn right_or_default(self) -> R
where
R: Default,
{
match self {
#[either]
Self::Right(r) => r,
#[both]
Self::Both(_, r) => r,
#[allow(unused)]
_ => R::default(),
}
}
#[quither(has_either || has_both)]
pub fn both_or_default(self) -> (L, R)
where
L: Default,
R: Default,
{
match self {
#[either]
Self::Left(l) => (l, R::default()),
#[either]
Self::Right(r) => (L::default(), r),
#[neither]
Self::Neither => (L::default(), R::default()),
#[both]
Self::Both(l, r) => (l, r),
}
}
#[quither(has_either || has_both)]
pub fn left_or_else<F>(self, #[allow(unused)] f: F) -> L
where
F: FnOnce() -> L,
{
match self {
#[either]
Self::Left(l) => l,
#[either]
Self::Right(_) => f(),
#[neither]
Self::Neither => f(),
#[both]
Self::Both(l, _) => l,
}
}
#[quither(has_either || has_both)]
pub fn right_or_else<F>(self, #[allow(unused)] f: F) -> R
where
F: FnOnce() -> R,
{
match self {
#[either]
Self::Left(_) => f(),
#[either]
Self::Right(r) => r,
#[neither]
Self::Neither => f(),
#[both]
Self::Both(_, r) => r,
}
}
#[quither(has_either || has_both)]
pub fn both_or_else<F, G>(self, #[allow(unused)] f: F, #[allow(unused)] g: G) -> (L, R)
where
F: FnOnce() -> L,
G: FnOnce() -> R,
{
match self {
#[either]
Self::Left(l) => (l, g()),
#[either]
Self::Right(r) => (f(), r),
#[neither]
Self::Neither => (f(), g()),
#[both]
Self::Both(l, r) => (l, r),
}
}
#[quither(has_either || has_both)]
pub fn left_and_then<F, L2>(self, f: F) -> Quither<L2, R>
where
F: FnOnce(L) -> Quither<L2, R>,
{
match self {
#[either]
Self::Left(l) => f(l),
#[either]
Self::Right(r) => Quither::Right(r),
#[neither]
Self::Neither => Quither::Neither,
#[both]
Self::Both(l, _) => f(l),
}
}
#[quither(has_either || has_both)]
pub fn right_and_then<F, R2>(self, f: F) -> Quither<L, R2>
where
F: FnOnce(R) -> Quither<L, R2>,
{
match self {
#[either]
Self::Left(l) => Quither::Left(l),
#[either]
Self::Right(r) => f(r),
#[neither]
Self::Neither => Quither::Neither,
#[both]
Self::Both(_, r) => f(r),
}
}
}
impl<L, R> Either<L, R> {
pub fn either<F, G, T>(self, f: F, g: G) -> T
where
F: FnOnce(L) -> T,
G: FnOnce(R) -> T,
{
match self {
Either::Left(l) => f(l),
Either::Right(r) => g(r),
}
}
pub fn either_with<Ctx, F, G, T>(self, ctx: Ctx, f: F, g: G) -> T
where
F: FnOnce(Ctx, L) -> T,
G: FnOnce(Ctx, R) -> T,
{
match self {
Either::Left(l) => f(ctx, l),
Either::Right(r) => g(ctx, r),
}
}
}
impl<L, R> EitherOrBoth<L, R> {
pub fn or(self, #[allow(unused)] l: L, #[allow(unused)] r: R) -> (L, R) {
self.both_or(l, r)
}
pub fn or_default(self) -> (L, R)
where
L: Default,
R: Default,
{
self.both_or_default()
}
pub fn or_else<F, G>(self, #[allow(unused)] f: F, #[allow(unused)] g: G) -> (L, R)
where
F: FnOnce() -> L,
G: FnOnce() -> R,
{
self.both_or_else(f, g)
}
}