Skip to content

Instantly share code, notes, and snippets.

@NthPortal
Last active April 9, 2021 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NthPortal/4f3619381e09c6cd8941c689338559ae to your computer and use it in GitHub Desktop.
Save NthPortal/4f3619381e09c6cd8941c689338559ae to your computer and use it in GitHub Desktop.
WConf sample API [WIP]
import scala.util.matching.Regex
object Impl {
sealed trait WConfAnyFilterable {
type Result = WConfFilterable with WConfCompletable
@annotation.inline
private[this] def quote(literal: String): String = Regex.quote(literal)
def category: Result
def messageRegex(regex: String): Result
final def message(regex: Regex): Result = messageRegex(regex.regex)
final def messageLiteral(literal: String): Result = messageRegex(quote(literal))
def siteRegex(regex: String): Result
final def site(regex: Regex): Result = siteRegex(regex.regex)
final def siteLiteral(literal: String): Result = siteRegex(quote(literal))
def sourceRegex(regex: String): Result
final def source(regex: Regex): Result = sourceRegex(regex.regex)
final def sourceLiteral(literal: String): Result = sourceRegex(quote(literal))
def originRegex(regex: String): Result
final def origin(regex: Regex): Result = originRegex(regex.regex)
final def originLiteral(literal: String): Result = originRegex(quote(literal))
protected[this] def sinceFilter(filter: String): Result
final def since_<(version: String): Result = sinceFilter(s"since<$version")
final def since_=(version: String): Result = sinceFilter(s"since=$version")
final def since_>(version: String): Result = sinceFilter(s"since>$version")
}
sealed trait WConfAppendable {
protected[this] def currentFlag: WConfFlag
def any: WConfCompletable = new WConfAny(currentFlag)
}
case object WConf extends WConfAppendable {
protected[this] final val currentFlag = new WConfFlag(Vector.empty)
}
private class WConfAnd(protected[this] val currentFlag) extends WConfAppendable
private[Impl] final class WConfAny(protected[this] val currentFlag) extends WConfCompletable {
protected[this] def filters: String = "any"
}
sealed trait WConfFilterable {
}
sealed trait WConfCompletable {
protected[this] def currentFlag: WConfFlag
protected[this] def filters: String
private[this] def action(_action: String): WConfFlag = new WConfFlag(currentFlag.filterActions :+ s"$filters:$_action")
final def error: WConfFlag = action("error")
final def warning: WConfFlag = action("warning")
final def warningSummary: WConfFlag = action("warning-summary")
final def warningVerbose: WConfFlag = action("warning-verbose")
final def info: WConfFlag = action("info")
final def infoSummary: WConfFlag = action("info-summary")
final def infoVerbose: WConfFlag = action("info-verbose")
final def silent: WConfFlag = action("silent")
}
final class WConfFlag(private[Impl] filterActions: Seq[String]) {
def and: WConfAppendable = new WConfAnd(this)
def toFlag: String =
if (filterActions.isEmpty) throw new IllegalStateException("no configuration for WConf")
else s"-WConf:${filterActions mkString ","}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment