Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilterMatcher: turn into metaconfig-parsable type #4102

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@ import org.scalafmt.sysops.AbsoluteFile

import scala.util.matching.Regex

import metaconfig._

case class FilterMatcher(include: Regex, exclude: Regex) {
def matchesFile(file: AbsoluteFile): Boolean = matches(file.toString())
def matches(input: String): Boolean = include.pattern.matcher(input).find() &&
!exclude.pattern.matcher(input).find()
}

object FilterMatcher {
val default = FilterMatcher(".*".r, "^$".r)

implicit val filterMatcherSurface: generic.Surface[FilterMatcher] = generic
.deriveSurface[FilterMatcher]
implicit val filterMatcherEncoder: ConfEncoder[FilterMatcher] = generic
.deriveEncoder[FilterMatcher]
implicit val filterMatcherDecoder: ConfDecoderEx[FilterMatcher] = generic
.deriveDecoderEx(default)

implicit val regexEncoder: ConfEncoder[Regex] = ConfEncoder.StringEncoder
.contramap[Regex](_.regex)
implicit val regexDecoder: ConfDecoderEx[Regex] = {
val seqStringDecoder = implicitly[ConfDecoderEx[Seq[String]]]
(state, conf) =>
seqStringDecoder.read(state.map(x => Seq(x.regex)), conf) match {
case Configured.Ok(x) => Configured.Ok(mkRegexp(x))
case x: Configured.NotOk => conf match {
case Conf.Str(s) => Configured.Ok(s.r)
case _ => x
}
}
}

def mkRegexp(filters: Seq[String], strict: Boolean = false): Regex =
filters match {
case Nil => "$a".r // will never match anything
Expand Down
Loading