<commit_before>package com.natpryce.krouton.http4k

import com.natpryce.krouton.Empty
import com.natpryce.krouton.PathTemplate
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status


class ResourceRoutesBuilder(private val monitor: RequestMonitor?) {
    private val routes = mutableListOf<PathMatchingHttpHandler<*>>()
    private var handlerIfNoMatch: HttpHandler = { Response(Status.NOT_FOUND) }
    
    operator fun <T> PathTemplate<T>.invoke(handler: (Request, T) -> Response) {
        addPathHandler(this, handler)
    }
    
    operator fun PathTemplate<Empty>.invoke(handler: (Request) -> Response) {
        addPathHandler(this, emptyHandler(handler))
    }
    
    infix fun <T> PathTemplate<T>.methods(block: MethodRoutesBuilder<T>.() -> Unit) {
        addPathHandler(this, MethodRoutesBuilder<T>().apply(block).toHandler())
    }
    
    @JvmName("methods0")
    infix fun PathTemplate<Empty>.methods(block: MethodRoutesBuilderEmpty.() -> Unit) {
        addPathHandler(this, MethodRoutesBuilderEmpty().apply(block).toHandler())
    }
    
    fun otherwise(handler: HttpHandler) {
        handlerIfNoMatch = handler
    }
    
    protected fun <T> addPathHandler(pathTemplate: PathTemplate<T>, handler: Request.(T) -> Response) {
        routes.add(PathMatchingHttpHandler(pathTemplate, handler, monitor))
    }
    
    internal fun toHandler() =
        PathRouter(routes.toList(), handlerIfNoMatch)
}

class MethodRoutesBuilder<T> {
    private val routes = mutableListOf<(Request, T) -> Response?>()
    private var handlerIfNoMatch: (Request, T) -> Response = { _, _ -> Response(Status.METHOD_NOT_ALLOWED) }
    
    operator fun Method.invoke(handler: (Request, T) -> Response) {
        val requiredMethod = this
        routes += methodHandler(requiredMethod, handler)
    }
    
    fun otherwise(handler: (Request, T) -> Response) {
        handlerIfNoMatch = handler
    }
    
    internal fun toHandler() =
        router(routes, handlerIfNoMatch)
}

class MethodRoutesBuilderEmpty {
    private val routes = mutableListOf<(Request,Empty) -> Response?>()
    private var handlerIfNoMatch: (Request) -> Response = { Response(Status.METHOD_NOT_ALLOWED) }
    
    operator fun Method.invoke(handler: (Request) -> Response) {
        val requiredMethod = this
        routes += methodHandler(requiredMethod, emptyHandler(handler))
    }
    
    fun otherwise(handler: (Request) -> Response) {
        handlerIfNoMatch = handler
    }
    
    internal fun toHandler() =
        router(routes, emptyHandler(handlerIfNoMatch))
}
<commit_msg>Prevent nested resource blocks at compile time.<commit_after>package com.natpryce.krouton.http4k

import com.natpryce.krouton.Empty
import com.natpryce.krouton.PathTemplate
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import kotlin.DeprecationLevel.ERROR


@DslMarker
annotation class RoutingSyntax

@RoutingSyntax
class ResourceRoutesBuilder(private val monitor: RequestMonitor?) {
    private val routes = mutableListOf<PathMatchingHttpHandler<*>>()
    private var handlerIfNoMatch: HttpHandler = { Response(Status.NOT_FOUND) }
    
    operator fun <T> PathTemplate<T>.invoke(handler: (Request, T) -> Response) {
        addPathHandler(this, handler)
    }
    
    operator fun PathTemplate<Empty>.invoke(handler: (Request) -> Response) {
        addPathHandler(this, emptyHandler(handler))
    }
    
    infix fun <T> PathTemplate<T>.methods(block: MethodRoutesBuilder<T>.() -> Unit) {
        addPathHandler(this, MethodRoutesBuilder<T>().apply(block).toHandler())
    }
    
    @JvmName("methods0")
    infix fun PathTemplate<Empty>.methods(block: MethodRoutesBuilderEmpty.() -> Unit) {
        addPathHandler(this, MethodRoutesBuilderEmpty().apply(block).toHandler())
    }
    
    fun otherwise(handler: HttpHandler) {
        handlerIfNoMatch = handler
    }
    
    protected fun <T> addPathHandler(pathTemplate: PathTemplate<T>, handler: Request.(T) -> Response) {
        routes.add(PathMatchingHttpHandler(pathTemplate, handler, monitor))
    }
    
    @Deprecated(level = ERROR, message = "Don't nest resources blocks")
    fun resources(setup: ResourceRoutesBuilder.() -> Unit): Nothing = throw UnsupportedOperationException("don't nest resources blocks")
    
    internal fun toHandler() =
        PathRouter(routes.toList(), handlerIfNoMatch)
}

@RoutingSyntax
class MethodRoutesBuilder<T> {
    private val routes = mutableListOf<(Request, T) -> Response?>()
    private var handlerIfNoMatch: (Request, T) -> Response = { _, _ -> Response(Status.METHOD_NOT_ALLOWED) }
    
    operator fun Method.invoke(handler: (Request, T) -> Response) {
        val requiredMethod = this
        routes += methodHandler(requiredMethod, handler)
    }
    
    fun otherwise(handler: (Request, T) -> Response) {
        handlerIfNoMatch = handler
    }
    
    internal fun toHandler() =
        router(routes, handlerIfNoMatch)
}

@RoutingSyntax
class MethodRoutesBuilderEmpty {
    private val routes = mutableListOf<(Request, Empty) -> Response?>()
    private var handlerIfNoMatch: (Request) -> Response = { Response(Status.METHOD_NOT_ALLOWED) }
    
    operator fun Method.invoke(handler: (Request) -> Response) {
        val requiredMethod = this
        routes += methodHandler(requiredMethod, emptyHandler(handler))
    }
    
    fun otherwise(handler: (Request) -> Response) {
        handlerIfNoMatch = handler
    }
    
    internal fun toHandler() =
        router(routes, emptyHandler(handlerIfNoMatch))
}

{'fraction_non_alphanumeric': 0.11167423977630199, 'fraction_numerical': 0.002446696959105208, 'mean_word_length': 3.6490658001624694, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2275234', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1676, 'n_words': 347}