Refactor enums
authorLaniusTrolling <lanius@laniustrolling.dev>
Sun, 23 Mar 2025 16:51:50 +0000 (12:51 -0400)
committerLaniusTrolling <lanius@laniustrolling.dev>
Sun, 23 Mar 2025 16:51:50 +0000 (12:51 -0400)
src/main/kotlin/info/mechyrdia/data/Data.kt
src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt
src/main/kotlin/info/mechyrdia/lore/ParserHtml.kt
src/main/kotlin/info/mechyrdia/lore/ParserPlain.kt
src/main/kotlin/info/mechyrdia/lore/ViewsLore.kt
src/main/kotlin/info/mechyrdia/lore/ViewsPrefs.kt
src/main/kotlin/info/mechyrdia/lore/ViewsQuote.kt
src/main/kotlin/info/mechyrdia/lore/ViewsRobots.kt
src/main/kotlin/info/mechyrdia/route/ResourceTypes.kt

index 8a55276357dea067b880bfd569e9919edd438ded..67614221354fc82bcedc51088958aaaf04e125c1 100644 (file)
@@ -121,7 +121,6 @@ const val MONGODB_ID_KEY = "_id"
 enum class IndexSort {
        ASCENDING,
        DESCENDING,
-       ;
 }
 
 typealias IndexSortProperty<T> = Pair<KProperty1<T, *>, IndexSort>
index 9c8e0065c0098f935d1e8d054880840c47361b54..5face4f7c232ca94dccc1ecd4e9af840f820f677 100644 (file)
@@ -33,24 +33,15 @@ private val FontsLogger: Logger = LoggerFactory.getLogger("info.mechyrdia.lore.F
 
 @Serializable(with = TextAlignmentSerializer::class)
 enum class TextAlignment {
-       LEFT {
-               override fun processWidth(widthDiff: Int): Int {
-                       return 0
-               }
-       },
-       CENTER {
-               override fun processWidth(widthDiff: Int): Int {
-                       return widthDiff / 2
-               }
-       },
-       RIGHT {
-               override fun processWidth(widthDiff: Int): Int {
-                       return widthDiff
-               }
-       },
-       ;
-       
-       abstract fun processWidth(widthDiff: Int): Int
+       LEFT,
+       CENTER,
+       RIGHT,
+}
+
+fun TextAlignment.processWidth(widthDiff: Int) = when (this) {
+       TextAlignment.LEFT -> 0
+       TextAlignment.CENTER -> widthDiff / 2
+       TextAlignment.RIGHT -> widthDiff
 }
 
 object TextAlignmentSerializer : KeyedEnumSerializer<TextAlignment>(TextAlignment.entries)
index a70bde2aa3a953047f7260fe8f1e1168735792a1..031903f2881b188e9c9443b41a8b9fa4d10c9e7d 100644 (file)
@@ -253,14 +253,13 @@ enum class HtmlTagMode {
        BLOCK,
        ITEM,
        LAYOUT,
-       ;
-       
-       fun combine(env: LexerTagEnvironment<HtmlBuilderContext, HtmlBuilderSubject>, subNodes: ParserTree) = when (this) {
-               INLINE -> HtmlLexerProcessor.combineInline(env, subNodes)
-               BLOCK -> HtmlLexerProcessor.combineBlock(env, subNodes)
-               ITEM -> HtmlLexerProcessor.combineItem(env, subNodes)
-               LAYOUT -> HtmlLexerProcessor.combineLayout(env, subNodes)
-       }
+}
+
+fun HtmlTagMode.combine(env: LexerTagEnvironment<HtmlBuilderContext, HtmlBuilderSubject>, subNodes: ParserTree) = when (this) {
+       HtmlTagMode.INLINE -> HtmlLexerProcessor.combineInline(env, subNodes)
+       HtmlTagMode.BLOCK -> HtmlLexerProcessor.combineBlock(env, subNodes)
+       HtmlTagMode.ITEM -> HtmlLexerProcessor.combineItem(env, subNodes)
+       HtmlTagMode.LAYOUT -> HtmlLexerProcessor.combineLayout(env, subNodes)
 }
 
 class HtmlTagLexerTag(
index b516e7cd91c00e9729ef3d077793a6994f0fa0f0..0c1b6971ee6e4d89fd87fb7c8077d1f36def8671 100644 (file)
@@ -8,7 +8,7 @@ typealias PlainTextBuilderSubject = String
 enum class PlainTextTagBehavior {
        PASS_THROUGH,
        PASS_THROUGH_SPACED,
-       ABSORB
+       ABSORB,
 }
 
 abstract class PlainTextFormattingProcessor : LexerTagFallback<PlainTextBuilderContext, PlainTextBuilderSubject>, LexerTextProcessor<PlainTextBuilderContext, PlainTextBuilderSubject>, LexerLineBreakProcessor<PlainTextBuilderContext, PlainTextBuilderSubject>, LexerCombiner<PlainTextBuilderContext, PlainTextBuilderSubject> {
index d68b3ba516854a190ceee24a5cd2ccdbc3cf8f07..bc999cebaf6eb1ede6302fcdeee6572d1be98b41 100644 (file)
@@ -73,7 +73,6 @@ const val TOC_TITLE = "Table of Contents"
 enum class LoreArticleFormat(val format: String? = null) {
        HTML(null),
        RAW_HTML("raw"),
-       ;
 }
 
 object LoreArticleFormatSerializer : KeyedEnumSerializer<LoreArticleFormat>(LoreArticleFormat.entries, LoreArticleFormat::format)
index 22acc69db33356e4bac6cdf7d562f9e28c97a290..e122e88c35616bbce4ff3b1fd2e3eeaf0c9c4a99 100644 (file)
@@ -12,7 +12,6 @@ enum class PageTheme(val attributeValue: String?) {
        SYSTEM(null),
        LIGHT("light"),
        DARK("dark"),
-       ;
 }
 
 object PageThemeSerializer : KeyedEnumSerializer<PageTheme>(PageTheme.entries, PageTheme::attributeValue)
@@ -26,23 +25,18 @@ val ApplicationCall.pageTheme: PageTheme
 
 @Serializable(with = April1stModeSerializer::class)
 enum class April1stMode {
-       DEFAULT {
-               override val isEnabled: Boolean
-                       get() = isApril1st()
-       },
-       ALWAYS {
-               override val isEnabled: Boolean
-                       get() = true
-       },
-       NEVER {
-               override val isEnabled: Boolean
-                       get() = false
-       },
-       ;
-       
-       abstract val isEnabled: Boolean
+       DEFAULT,
+       ALWAYS,
+       NEVER,
 }
 
+val April1stMode.isEnabled: Boolean
+       get() = when (this) {
+               April1stMode.DEFAULT -> isApril1st()
+               April1stMode.ALWAYS -> true
+               April1stMode.NEVER -> false
+       }
+
 object April1stModeSerializer : KeyedEnumSerializer<April1stMode>(April1stMode.entries)
 
 val ApplicationCall.april1stMode: April1stMode
index 55d6e5c72bb3c35218c863e757ed665b356b4f34..50ccbdf9737da1c59792a62111b37afadcc26de1 100644 (file)
@@ -14,7 +14,18 @@ import io.ktor.http.ContentType
 import io.ktor.server.application.ApplicationCall
 import io.ktor.server.html.respondHtml
 import io.ktor.server.response.respondText
-import kotlinx.html.*
+import kotlinx.html.Entities
+import kotlinx.html.FlowContent
+import kotlinx.html.HTML
+import kotlinx.html.a
+import kotlinx.html.blockQuote
+import kotlinx.html.h1
+import kotlinx.html.id
+import kotlinx.html.img
+import kotlinx.html.p
+import kotlinx.html.section
+import kotlinx.html.style
+import kotlinx.html.unsafe
 import kotlinx.serialization.Serializable
 import kotlinx.serialization.builtins.ListSerializer
 import kotlinx.serialization.json.buildJsonObject
@@ -52,29 +63,17 @@ suspend fun randomQuote(): Quote = getQuotesList().random()
 
 @Serializable(with = QuoteFormatSerializer::class)
 enum class QuoteFormat(val format: String?) {
-       HTML(null) {
-               override suspend fun ApplicationCall.respondQuote(quote: Quote) {
-                       respondHtml(block = quote.toHtml(RANDOM_QUOTE_HTML_TITLE, this))
-               }
-       },
-       RAW_HTML("raw") {
-               override suspend fun ApplicationCall.respondQuote(quote: Quote) {
-                       respondHtml(block = quote.toRawHtml(RANDOM_QUOTE_HTML_TITLE, this))
-               }
-       },
-       JSON("json") {
-               override suspend fun ApplicationCall.respondQuote(quote: Quote) {
-                       respondText(quote.toJson(), contentType = ContentType.Application.Json)
-               }
-       },
-       XML("xml") {
-               override suspend fun ApplicationCall.respondQuote(quote: Quote) {
-                       respondXml { quote(quote) }
-               }
-       },
-       ;
-       
-       abstract suspend fun ApplicationCall.respondQuote(quote: Quote)
+       HTML(null),
+       RAW_HTML("raw"),
+       JSON("json"),
+       XML("xml"),
+}
+
+suspend fun ApplicationCall.respondQuote(quote: Quote, format: QuoteFormat) = when (format) {
+       QuoteFormat.HTML -> respondHtml(block = quote.toHtml(RANDOM_QUOTE_HTML_TITLE, this))
+       QuoteFormat.RAW_HTML -> respondHtml(block = quote.toRawHtml(RANDOM_QUOTE_HTML_TITLE, this))
+       QuoteFormat.JSON -> respondText(quote.toJson(), contentType = ContentType.Application.Json)
+       QuoteFormat.XML -> respondXml { quote(quote) }
 }
 
 object QuoteFormatSerializer : KeyedEnumSerializer<QuoteFormat>(QuoteFormat.entries, QuoteFormat::format)
index f97c092c90adfa44ec72639b770127c3f72c11ee..0706e900685ac3b341919310ff25fabfc0b8fa6a 100644 (file)
@@ -26,7 +26,6 @@ enum class SitemapChangeFrequency {
        DAILY,
        HOURLY,
        ALWAYS,
-       ;
 }
 
 val SitemapChangeFrequency.xmlValue: String
index 9ccca62d83d70154044c4e1c037479b49e5efd3e..e21440f3e7b350989824caae5f3ff66c1cf721aa 100644 (file)
@@ -55,6 +55,7 @@ import info.mechyrdia.lore.randomQuote
 import info.mechyrdia.lore.recentCommentsRssFeedGenerator
 import info.mechyrdia.lore.redirectHref
 import info.mechyrdia.lore.respondAsset
+import info.mechyrdia.lore.respondQuote
 import info.mechyrdia.lore.respondRss
 import info.mechyrdia.lore.sitemap
 import info.mechyrdia.lore.toCommentHtml
@@ -146,7 +147,7 @@ class Root : ResourceHandler, ResourceFilter {
                override suspend fun RoutingContext.handleCall() {
                        with(root) { call.filterCall() }
                        
-                       with(format) { call.respondQuote(randomQuote()) }
+                       call.respondQuote(randomQuote(), format)
                }
        }