package gs.predicate.v0.api /** A _Predicate_ is some function that accepts any input and emits some * [[Predicate.Result]] (whether the predicate matched). * * Predicates evaluate input to see if the logic matches that input. */ trait Predicate: /** @return * The serializable predicate type. */ def predicateType: String /** Evaluate this predicate against the given input. * * @return * Some [[Predicate.Result]] that describes whether the input matched the * predicate. */ def eval(input: Any): Predicate.Result end Predicate object Predicate: def alwaysTrue: Predicate = True def alwaysFalse: Predicate = False /** The result of evaluating a [[Predicate]] is a Boolean value where: * * - `true`: The predicate matched the given input. * - `false`: The predicate missed (did not match) the given input. */ opaque type Result = Boolean object Result: /** @return * A result indicating a predicate matched (`true`). */ def matched(): Result = true /** @return * A result indicating a predicate missed (`false`). */ def missed(): Result = false /** Instantiate a new [[Predicate.Result]] from the given value. * * @param value * The underlying `Boolean` value. * @return * The new predicate result. */ def apply(value: Boolean): Result = value given CanEqual[Result, Result] = CanEqual.derived extension (results: List[Result]) /** @return * True if all results are true. False if no results are given. */ def allMatch(): Result = results.nonEmpty && results.forall(x => x) /** @return * True if any results match. False if no results are given. */ def anyMatch(): Result = results.nonEmpty && results.find(x => x).isDefined extension (result: Result) /** @return * The underlying value. */ def unwrap(): Boolean = result /** Logical AND operation. * * @param other * The other result. * @return * True if both results match. False otherwise. */ def and(other: Result): Result = result && other /** Logical OR operation. * * @param other * The other result. * @return * True if either result matches. False otherwise. */ def or(other: Result): Result = result || other /** @return * True if this result is a match. False otherwise. */ def isMatch: Boolean = result /** @return * True if this result is a miss. False otherwise. */ def isMiss: Boolean = !result end Result end Predicate