Fix April 1st mode
authorLanius Trolling <lanius@laniustrolling.dev>
Sat, 22 Jun 2024 01:43:44 +0000 (21:43 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Sat, 22 Jun 2024 01:43:44 +0000 (21:43 -0400)
src/jvmMain/kotlin/info/mechyrdia/lore/ParserPreprocess.kt
src/jvmMain/kotlin/info/mechyrdia/lore/ViewsPrefs.kt

index 5e68d7717549338339447b39cd8a3ea7f3e94b5c..ba8debd355cf3c652ee4211238b625f38c98f563 100644 (file)
@@ -7,6 +7,7 @@ import io.ktor.server.request.*
 import kotlinx.coroutines.*
 import java.time.Instant
 import kotlin.math.roundToInt
+import java.util.function.Function as JFunction
 
 class PreProcessorContext private constructor(
        val variables: MutableMap<String, ParserTree>,
@@ -337,9 +338,9 @@ enum class PreProcessorTags(val type: PreProcessorLexerTag) {
                }
        }),
        FOR_EACH(PreProcessorLexerTag { env, param, subNodes ->
-               val itemToContext: (ParserTree) -> Map<String, ParserTree> = if (param == null)
-                       ParserTree::asPreProcessorMap
-               else ({ mapOf(param to it) })
+               val itemToContext: JFunction<ParserTree, Map<String, ParserTree>> = if (param == null)
+                       JFunction(ParserTree::asPreProcessorMap)
+               else JFunction { mapOf(param to it) }
                
                val subTags = subNodes.filterIsInstance<ParserTreeNode.Tag>()
                val list = subTags.singleOrNull { it isTag "in" }?.subNodes
@@ -349,14 +350,14 @@ enum class PreProcessorTags(val type: PreProcessorLexerTag) {
                val body = subTags.singleOrNull { it isTag "do" }?.subNodes
                if (list != null && body != null)
                        list.mapSuspend { item ->
-                               PreProcessorUtils.processWithContext(env, env.context + itemToContext(item), body)
+                               PreProcessorUtils.processWithContext(env, env.context + itemToContext.apply(item), body)
                        }.flatten()
                else formatErrorToParserTree("Expected child tag [in] to take list input and child tag [do] to take loop body")
        }),
        MAP(PreProcessorLexerTag { env, param, subNodes ->
-               val itemToContext: (ParserTree) -> Map<String, ParserTree> = if (param == null)
-                       ParserTree::asPreProcessorMap
-               else ({ mapOf(param to it) })
+               val itemToContext: JFunction<ParserTree, Map<String, ParserTree>> = if (param == null)
+                       JFunction(ParserTree::asPreProcessorMap)
+               else JFunction { mapOf(param to it) }
                
                val subTags = subNodes.filterIsInstance<ParserTreeNode.Tag>()
                val list = subTags.singleOrNull { it isTag "in" }?.subNodes
@@ -366,7 +367,7 @@ enum class PreProcessorTags(val type: PreProcessorLexerTag) {
                val body = subTags.singleOrNull { it isTag "do" }?.subNodes
                if (list != null && body != null)
                        list.mapSuspend { item ->
-                               ParserTreeNode.Tag("item", null, PreProcessorUtils.processWithContext(env, env.context + itemToContext(item), body))
+                               ParserTreeNode.Tag("item", null, PreProcessorUtils.processWithContext(env, env.context + itemToContext.apply(item), body))
                        }
                else formatErrorToParserTree("Expected child tag [in] to take list input and child tag [do] to take loop body")
        }),
index 99307e96b7c1645c4b0623d0ac5703c943a57059..faf41dbfabb519e8050a5082487d4b75f3a32221 100644 (file)
@@ -44,11 +44,9 @@ enum class April1stMode {
 object April1stModeSerializer : KeyedEnumSerializer<April1stMode>(April1stMode.entries)
 
 val ApplicationCall.april1stMode: April1stMode
-       get() = when (request.cookies["APRIL_1ST_MODE"]) {
-               "always" -> April1stMode.ALWAYS
-               "never" -> April1stMode.NEVER
-               else -> April1stMode.DEFAULT
-       }
+       get() = request.cookies["APRIL_1ST_MODE"]?.let { modeCookie ->
+               April1stMode.entries.firstOrNull { mode -> mode.name.equals(modeCookie, ignoreCase = true) }
+       } ?: April1stMode.DEFAULT
 
 class JoinToHtmlConsumer<E>(val iterator: Iterator<E>) {
        inline fun <T : Tag> T.invokeReceiver(separator: T.() -> Unit, body: T.(E) -> Unit) {