Skip to content

Commit

Permalink
Ability to lift a ScalaVersion from a String
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 committed Oct 1, 2023
1 parent 9953268 commit e239c58
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ object ScalaVersion {
val V3_3_0 = ScalaVersion(3, 3, 0)
val V3_3_1 = ScalaVersion(3, 3, 1)

def unsafeFromString(version: String): ScalaVersion =
fromString(version).fold(throw _, identity)

def fromString(version: String): Either[IllegalArgumentException, ScalaVersion] = {
val regex = raw"""(\d+)\.(\d+)\.(\d+).*""".r
version match {
case regex(major, minor, patch) =>
Right(ScalaVersion(major.toLong, minor.toLong, patch.toLong))
case _ => Left(new IllegalArgumentException(s"Scala version $version not recognized"))
}
}

implicit val scalaVersionOrdering: Ordering[ScalaVersion] =
Ordering.by(version => (version.major, version.minor, version.patch))
}

0 comments on commit e239c58

Please sign in to comment.