commit 24fee4b4be815b907838aa9b2ec56b78df862d5f Author: Pat Garrity Date: Mon Nov 3 09:00:35 2025 -0600 Initialize the repository with a baseline predicate implementation and minimal tests. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78eb111 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target/ +project/target/ +project/project/ +.version +.scala-build/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..d3cafd8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - id: fix-byte-order-marker + - id: mixed-line-ending + args: ['--fix=lf'] + description: Enforces using only 'LF' line endings. + - id: trailing-whitespace + - id: check-yaml + - repo: https://git.garrity.co/garrity-software/gs-pre-commit-scala + rev: v1.0.1 + hooks: + - id: scalafmt diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..be3b2cb --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,72 @@ +// See: https://github.com/scalameta/scalafmt/tags for the latest tags. +version = 3.9.9 +runner.dialect = scala3 +maxColumn = 80 + +rewrite { + rules = [RedundantBraces, RedundantParens, Imports, SortModifiers] + imports.expand = true + imports.sort = scalastyle + redundantBraces.ifElseExpressions = true + redundantBraces.stringInterpolation = true +} + +indent { + main = 2 + callSite = 2 + defnSite = 2 + extendSite = 4 + withSiteRelativeToExtends = 2 + commaSiteRelativeToExtends = 2 +} + +align { + preset = more + openParenCallSite = false + openParenDefnSite = false +} + +newlines { + implicitParamListModifierForce = [before,after] + topLevelStatementBlankLines = [ + { + blanks = 1 + } + ] + afterCurlyLambdaParams = squash +} + +danglingParentheses { + defnSite = true + callSite = true + ctrlSite = true + exclude = [] +} + +verticalMultiline { + atDefnSite = true + arityThreshold = 2 + newlineAfterOpenParen = true +} + +comments { + wrap = standalone +} + +docstrings { + style = "SpaceAsterisk" + oneline = unfold + wrap = yes + forceBlankLineBefore = true +} + +project { + excludePaths = [ + "glob:**target/**", + "glob:**.metals/**", + "glob:**.bloop/**", + "glob:**.bsp/**", + "glob:**metals.sbt", + "glob:**.git/**" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f2b2735 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright Patrick Garrity + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9adc379 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# gs-predicate + +[GS Open Source](https://garrity.co/open-source.html) | +[License (MIT)](./LICENSE) + +Serializable predicates for Scala 3. + +- [Usage](#usage) + - [Dependency](#dependency) +- [Donate](#donate) + +## Usage + +### Dependency + +This artifact is available in the Garrity Software Maven repository. + +```scala +externalResolvers += + "Garrity Software Releases" at "https://maven.garrity.co/gs" + +val GsPredicate: ModuleID = + "gs" %% "gs-predicate-api-v0" % "$VERSION" +``` + +## Donate + +Enjoy this project or want to help me achieve my [goals](https://garrity.co)? +Consider [Donating to Pat on Ko-fi](https://ko-fi.com/gspfm). diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..5c52768 --- /dev/null +++ b/build.sbt @@ -0,0 +1,91 @@ +val scala3: String = "3.7.3" + +ThisBuild / scalaVersion := scala3 +ThisBuild / versionScheme := Some("semver-spec") +ThisBuild / gsProjectName := "gs-predicate" + +ThisBuild / externalResolvers := Seq( + "Garrity Software Mirror" at "https://maven.garrity.co/releases", + "Garrity Software Releases" at "https://maven.garrity.co/gs" +) + +ThisBuild / licenses := Seq( + "MIT" -> url("https://git.garrity.co/garrity-software/gs-predicate/LICENSE") +) + +val noPublishSettings = Seq( + publish := {} +) + +val sharedSettings = Seq( + scalaVersion := scala3, + version := semVerSelected.value, + coverageFailOnMinimum := true + /* coverageMinimumStmtTotal := 100, coverageMinimumBranchTotal := 100 */ +) + +val Deps = new { + val Cats = new { + val Core: ModuleID = "org.typelevel" %% "cats-core" % "2.13.0" + val Effect: ModuleID = "org.typelevel" %% "cats-effect" % "3.6.3" + } + + val Gs = new { + val Datagen: ModuleID = "gs" %% "gs-datagen-core-v0" % "0.3.3" + val Uuid: ModuleID = "gs" %% "gs-uuid-v0" % "0.4.1" + } + + val MUnit: ModuleID = "org.scalameta" %% "munit" % "1.1.1" +} + +lazy val testSettings = Seq( + libraryDependencies ++= Seq( + Deps.MUnit % Test, + Deps.Gs.Datagen % Test + ) +) + +lazy val `gs-predicate` = project + .in(file(".")) + .aggregate( + `test-support`, + api + ) + .settings(noPublishSettings) + .settings(name := s"${gsProjectName.value}-v${semVerMajor.value}") + +/** Internal project used for unit tests. + */ +lazy val `test-support` = project + .in(file("modules/test-support")) + .settings(sharedSettings) + .settings(testSettings) + .settings(noPublishSettings) + .settings( + name := s"${gsProjectName.value}-test-support" + ) + .settings( + libraryDependencies ++= Seq( + Deps.Cats.Core, + Deps.Cats.Effect + ) + ) + +/** Core API - the only dependency needed to write tests. + */ +lazy val api = project + .in(file("modules/api")) + .dependsOn(`test-support` % "test->test") + .settings(sharedSettings) + .settings(testSettings) + .settings( + name := s"${gsProjectName.value}-api-v${semVerMajor.value}" + ) + .settings( + libraryDependencies ++= Seq( + Deps.Cats.Core, + Deps.Cats.Effect, + Deps.Gs.Uuid + ) + ) + diff --git a/modules/api/src/main/scala/gs/predicate/v0/api/And.scala b/modules/api/src/main/scala/gs/predicate/v0/api/And.scala new file mode 100644 index 0000000..d44fefd --- /dev/null +++ b/modules/api/src/main/scala/gs/predicate/v0/api/And.scala @@ -0,0 +1,29 @@ +package gs.predicate.v0.api + +import cats.syntax.all.* +import cats.Applicative +import gs.predicate.v0.api.Predicate.Result.forall +import gs.uuid.v0.UUID + +/** + * Implements logical AND. + * + * @param ps The predicates to evaluate. + */ +final class And[F[_]: Applicative, -A]( + val id: UUID, + private val ps: List[Predicate[F, A]] +) extends Predicate[F, A]: + /** @inheritDocs */ + override def eval(input: A): F[Predicate.Result] = + ps.map(_.eval(input)).sequence.map(_.forall()) + +object And: + + def apply[F[_]: Applicative, A](ps: Predicate[F, A]*): And[F, A] = + new And(UUID.v7(), ps.toList) + + def apply[F[_]: Applicative, A](id: UUID, ps: Predicate[F, A]*): And[F, A] = + new And(id, ps.toList) + +end And diff --git a/modules/api/src/main/scala/gs/predicate/v0/api/False.scala b/modules/api/src/main/scala/gs/predicate/v0/api/False.scala new file mode 100644 index 0000000..03a16b2 --- /dev/null +++ b/modules/api/src/main/scala/gs/predicate/v0/api/False.scala @@ -0,0 +1,23 @@ +package gs.predicate.v0.api + +import cats.Applicative + +import gs.uuid.v0.UUID + +/** + * Always returns a miss. + */ +final class False[F[_]: Applicative]( + val id: UUID +) extends Predicate[F, Any]: + /** @inheritDocs */ + override def eval(input: Any): F[Predicate.Result] = + Applicative[F].pure(Predicate.Result.missed()) + +object False: + + def apply[F[_]: Applicative]: False[F] = new False[F](UUID.v7()) + + def apply[F[_]: Applicative](id: UUID): False[F] = new False[F](id) + +end False diff --git a/modules/api/src/main/scala/gs/predicate/v0/api/Or.scala b/modules/api/src/main/scala/gs/predicate/v0/api/Or.scala new file mode 100644 index 0000000..ce72e78 --- /dev/null +++ b/modules/api/src/main/scala/gs/predicate/v0/api/Or.scala @@ -0,0 +1,29 @@ +package gs.predicate.v0.api + +import cats.syntax.all.* +import cats.Applicative +import gs.predicate.v0.api.Predicate.Result.forany +import gs.uuid.v0.UUID + +/** + * Implements logical OR. + * + * @param ps The predicates to evaluate. + */ +final class Or[F[_]: Applicative, -A]( + val id: UUID, + private val ps: List[Predicate[F, A]] +) extends Predicate[F, A]: + /** @inheritDocs */ + override def eval(input: A): F[Predicate.Result] = + ps.map(_.eval(input)).sequence.map(_.forany()) + +object Or: + + def apply[F[_]: Applicative, A](ps: Predicate[F, A]*): Or[F, A] = + new Or(UUID.v7(), ps.toList) + + def apply[F[_]: Applicative, A](id: UUID, ps: Predicate[F, A]*): Or[F, A] = + new Or(id, ps.toList) + +end Or diff --git a/modules/api/src/main/scala/gs/predicate/v0/api/Predicate.scala b/modules/api/src/main/scala/gs/predicate/v0/api/Predicate.scala new file mode 100644 index 0000000..7bb3a7f --- /dev/null +++ b/modules/api/src/main/scala/gs/predicate/v0/api/Predicate.scala @@ -0,0 +1,120 @@ +package gs.predicate.v0.api + +import gs.uuid.v0.UUID + +/** + * 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 predicate matches that input. + */ +trait Predicate[F[_], -A]: + /** + * @return The unique identifier of this Predicate. + */ + def id: UUID + + /** + * Evaluate this predicate against the given input. + * + * @param input The input to evaluate this predicate against. + * @return Some [[Predicate.Result]] that describes whether the input matched the predicate. + */ + def eval(input: A): F[Predicate.Result] + + /** + * Predicate equality is based on the _unique identifier_. Two predicates with + * the same ID are considered equal. + * + * @param that The other object. + * @return True if the other object is a predicate with the same ID, false otherwise. + */ + override def equals(that: Any): Boolean = + that match + case p: Predicate[?, ?] => p.id == id + case _ => false + + /** + * @return The hash code of the unique identifier. + */ + override def hashCode(): Int = id.hashCode() + + /** @inheritDocs */ + override def toString(): String = s"predicate:${id.withoutDashes()}" + +object Predicate: + + /** + * 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. + */ + def forall(): Result = results.forall(x => x) + + /** + * @return True if any results match. + */ + def forany(): Result = 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 diff --git a/modules/api/src/main/scala/gs/predicate/v0/api/True.scala b/modules/api/src/main/scala/gs/predicate/v0/api/True.scala new file mode 100644 index 0000000..7ef55a7 --- /dev/null +++ b/modules/api/src/main/scala/gs/predicate/v0/api/True.scala @@ -0,0 +1,23 @@ +package gs.predicate.v0.api + +import cats.Applicative + +import gs.uuid.v0.UUID + +/** + * Always returns a match. + */ +final class True[F[_]: Applicative]( + val id: UUID +) extends Predicate[F, Any]: + /** @inheritDocs */ + override def eval(input: Any): F[Predicate.Result] = + Applicative[F].pure(Predicate.Result.matched()) + +object True: + + def apply[F[_]: Applicative]: True[F] = new True[F](UUID.v7()) + + def apply[F[_]: Applicative](id: UUID): True[F] = new True[F](id) + +end True diff --git a/modules/api/src/test/scala/gs/predicate/v0/api/AndTests.scala b/modules/api/src/test/scala/gs/predicate/v0/api/AndTests.scala new file mode 100644 index 0000000..0fa9532 --- /dev/null +++ b/modules/api/src/test/scala/gs/predicate/v0/api/AndTests.scala @@ -0,0 +1,27 @@ +package gs.predicate.v0.api + +import support.IOSuite + +import cats.effect.IO + +import gs.predicate.v0.api.And + +class AndTests extends IOSuite: + iotest("should return true if all are true") { + val and: And[IO, Any] = And( + True[IO], True[IO], True[IO] + ) + and.eval(()).map(_.unwrap()) + } + + iotest("should return false if any are false") { + val and: And[IO, Any] = And( + True[IO], False[IO], True[IO] + ) + and.eval(()).map(x => !x.unwrap()) + } + + iotest("should return true for an empty list") { + val and: And[IO, Any] = And() + and.eval(()).map(_.unwrap()) + } diff --git a/modules/test-support/src/test/scala/support/IOSuite.scala b/modules/test-support/src/test/scala/support/IOSuite.scala new file mode 100644 index 0000000..1fc7f44 --- /dev/null +++ b/modules/test-support/src/test/scala/support/IOSuite.scala @@ -0,0 +1,19 @@ +package support + +import cats.effect.IO +import cats.effect.unsafe.IORuntime +import munit.FunSuite +import munit.Location + +abstract class IOSuite extends FunSuite: + implicit val runtime: IORuntime = IORuntime.global + + def iotest( + name: String + )( + body: => IO[Any] + )( + implicit + loc: Location + ): Unit = + test(name)(body.unsafeRunSync()) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..5e6884d --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.11.6 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..83e5dc1 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,33 @@ +def selectCredentials(): Credentials = + if ((Path.userHome / ".sbt" / ".credentials").exists()) + Credentials(Path.userHome / ".sbt" / ".credentials") + else + Credentials.apply( + realm = "Reposilite", + host = "maven.garrity.co", + userName = sys.env + .get("GS_MAVEN_USER") + .getOrElse( + throw new RuntimeException( + "You must either provide ~/.sbt/.credentials or specify the GS_MAVEN_USER environment variable." + ) + ), + passwd = sys.env + .get("GS_MAVEN_TOKEN") + .getOrElse( + throw new RuntimeException( + "You must either provide ~/.sbt/.credentials or specify the GS_MAVEN_TOKEN environment variable." + ) + ) + ) + +credentials += selectCredentials() + +externalResolvers := Seq( + "Garrity Software Mirror" at "https://maven.garrity.co/releases", + "Garrity Software Releases" at "https://maven.garrity.co/gs" +) + +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.3.1") +addSbtPlugin("gs" % "sbt-garrity-software" % "0.6.0") +addSbtPlugin("gs" % "sbt-gs-semver" % "0.3.0")