From fe13f5bb4571536f1d21f856f403ab8f49a6e510 Mon Sep 17 00:00:00 2001 From: LaniusTrolling Date: Sun, 23 Mar 2025 12:51:50 -0400 Subject: [PATCH] Refactor enums --- src/main/kotlin/info/mechyrdia/data/Data.kt | 1 - .../kotlin/info/mechyrdia/lore/FontDrawing.kt | 27 ++++------- .../kotlin/info/mechyrdia/lore/ParserHtml.kt | 15 +++--- .../kotlin/info/mechyrdia/lore/ParserPlain.kt | 2 +- .../kotlin/info/mechyrdia/lore/ViewsLore.kt | 1 - .../kotlin/info/mechyrdia/lore/ViewsPrefs.kt | 26 ++++------ .../kotlin/info/mechyrdia/lore/ViewsQuote.kt | 47 +++++++++---------- .../kotlin/info/mechyrdia/lore/ViewsRobots.kt | 1 - .../info/mechyrdia/route/ResourceTypes.kt | 3 +- 9 files changed, 52 insertions(+), 71 deletions(-) diff --git a/src/main/kotlin/info/mechyrdia/data/Data.kt b/src/main/kotlin/info/mechyrdia/data/Data.kt index 8a55276..6761422 100644 --- a/src/main/kotlin/info/mechyrdia/data/Data.kt +++ b/src/main/kotlin/info/mechyrdia/data/Data.kt @@ -121,7 +121,6 @@ const val MONGODB_ID_KEY = "_id" enum class IndexSort { ASCENDING, DESCENDING, - ; } typealias IndexSortProperty = Pair, IndexSort> diff --git a/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt b/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt index 9c8e006..5face4f 100644 --- a/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt +++ b/src/main/kotlin/info/mechyrdia/lore/FontDrawing.kt @@ -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.entries) diff --git a/src/main/kotlin/info/mechyrdia/lore/ParserHtml.kt b/src/main/kotlin/info/mechyrdia/lore/ParserHtml.kt index a70bde2..031903f 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ParserHtml.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ParserHtml.kt @@ -253,14 +253,13 @@ enum class HtmlTagMode { BLOCK, ITEM, LAYOUT, - ; - - fun combine(env: LexerTagEnvironment, 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, 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( diff --git a/src/main/kotlin/info/mechyrdia/lore/ParserPlain.kt b/src/main/kotlin/info/mechyrdia/lore/ParserPlain.kt index b516e7c..0c1b697 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ParserPlain.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ParserPlain.kt @@ -8,7 +8,7 @@ typealias PlainTextBuilderSubject = String enum class PlainTextTagBehavior { PASS_THROUGH, PASS_THROUGH_SPACED, - ABSORB + ABSORB, } abstract class PlainTextFormattingProcessor : LexerTagFallback, LexerTextProcessor, LexerLineBreakProcessor, LexerCombiner { diff --git a/src/main/kotlin/info/mechyrdia/lore/ViewsLore.kt b/src/main/kotlin/info/mechyrdia/lore/ViewsLore.kt index d68b3ba..bc999ce 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ViewsLore.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ViewsLore.kt @@ -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.entries, LoreArticleFormat::format) diff --git a/src/main/kotlin/info/mechyrdia/lore/ViewsPrefs.kt b/src/main/kotlin/info/mechyrdia/lore/ViewsPrefs.kt index 22acc69..e122e88 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ViewsPrefs.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ViewsPrefs.kt @@ -12,7 +12,6 @@ enum class PageTheme(val attributeValue: String?) { SYSTEM(null), LIGHT("light"), DARK("dark"), - ; } object PageThemeSerializer : KeyedEnumSerializer(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.entries) val ApplicationCall.april1stMode: April1stMode diff --git a/src/main/kotlin/info/mechyrdia/lore/ViewsQuote.kt b/src/main/kotlin/info/mechyrdia/lore/ViewsQuote.kt index 55d6e5c..50ccbdf 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ViewsQuote.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ViewsQuote.kt @@ -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.entries, QuoteFormat::format) diff --git a/src/main/kotlin/info/mechyrdia/lore/ViewsRobots.kt b/src/main/kotlin/info/mechyrdia/lore/ViewsRobots.kt index f97c092..0706e90 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ViewsRobots.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ViewsRobots.kt @@ -26,7 +26,6 @@ enum class SitemapChangeFrequency { DAILY, HOURLY, ALWAYS, - ; } val SitemapChangeFrequency.xmlValue: String diff --git a/src/main/kotlin/info/mechyrdia/route/ResourceTypes.kt b/src/main/kotlin/info/mechyrdia/route/ResourceTypes.kt index 9ccca62..e21440f 100644 --- a/src/main/kotlin/info/mechyrdia/route/ResourceTypes.kt +++ b/src/main/kotlin/info/mechyrdia/route/ResourceTypes.kt @@ -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) } } -- 2.25.1