-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation: implement edition using YearMonth
Also, add the toString method so that the automatic documentation which lists all possible options displays this value in a more user-friendly way.
- Loading branch information
Showing
2 changed files
with
21 additions
and
11 deletions.
There are no files selected for viewing
23 changes: 16 additions & 7 deletions
23
scalafmt-core/shared/src/main/scala/org/scalafmt/config/Edition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
package org.scalafmt.config | ||
|
||
import java.time.YearMonth | ||
|
||
import metaconfig.{Conf, ConfDecoder, ConfEncoder, Configured} | ||
|
||
case class Edition(year: Int, month: Int) | ||
class Edition(val ym: YearMonth) extends AnyVal { | ||
override def toString: String = ym.toString | ||
} | ||
|
||
object Edition { | ||
val Latest = Edition(Int.MaxValue, Int.MaxValue) | ||
|
||
def apply(year: Int, month: Int): Edition = | ||
new Edition(YearMonth.of(year, month)) | ||
|
||
val Latest = Edition(9999, 12) | ||
|
||
implicit val ordering: Ordering[Edition] = | ||
Ordering.by[Edition, (Int, Int)](e => e.year -> e.month) | ||
(x: Edition, y: Edition) => x.ym.compareTo(y.ym) | ||
|
||
lazy val format = "(\\d{4})-(\\d{1,2})".r | ||
|
||
implicit val decoder: ConfDecoder[Edition] = | ||
ConfDecoder.instanceExpect("'$year-$month', for example '2019-08'") { | ||
case Conf.Str(format(year, month)) => | ||
Configured.ok(Edition(year.toInt, month.toInt)) | ||
try Configured.ok(Edition(year.toInt, month.toInt)) | ||
catch { case e: Exception => Configured.error(e.getMessage) } | ||
case Conf.Str("latest") => Configured.ok(Latest) | ||
} | ||
|
||
implicit val encoder: ConfEncoder[Edition] = | ||
ConfEncoder.instance(edition => | ||
Conf.Str(f"${edition.year}%d-${edition.month}%02d") | ||
) | ||
ConfEncoder.instance(edition => Conf.Str(edition.ym.toString)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters