PatternApi.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* 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.
*/
package com.github.jonathanxd.dracon.log
import java.io.Serializable
import java.nio.charset.Charset
interface PatternMatch {
val matchResult: MatchResult
}
abstract class Pattern<M : PatternMatch>(val regex: Regex) {
abstract val factory: (MatchResult) -> M
fun matchEntire(str: String): M? =
this.regex.matchEntire(str)?.let(this.factory)
fun find(str: String): M? =
this.regex.find(str)?.let(this.factory)
}
interface HunkMatch : PatternMatch {
val changeNumber: Int get() = matchResult.groups["changeNumber"]!!.value.toInt()
}
interface EncodingHunkMatch : HunkMatch {
val encoding: Encoding
get() = matchResult.groups["encoding"]!!.value.let {
if (it == "binary") Encoding.Binary
else Encoding.Charset(it)
}
}
sealed class Encoding : Serializable {
companion object {
const val serialVersionUID = 1L
}
data class Charset(val charset: String) : Encoding(), Serializable {
fun resolve() = java.nio.charset.Charset.forName(this.charset)
companion object {
const val serialVersionUID = 1L
}
}
data object Binary : Encoding(), Serializable {
private fun readResolve(): Any = Binary
const val serialVersionUID = 1L
}
}