diff --git a/src/playdate/build/pdxinfo.nim b/src/playdate/build/pdxinfo.nim index a74cb86..d067668 100644 --- a/src/playdate/build/pdxinfo.nim +++ b/src/playdate/build/pdxinfo.nim @@ -1,14 +1,17 @@ -import std/[os, parsecfg, streams, strutils, strformat, times, osproc], nimbledump +import std/[os, parsecfg, streams, strutils, strformat, times, osproc, options], nimbledump type PdxInfo* = object ## Details used to populate the pdxinfo file - name*, author*, description*, bundleId*, imagePath*, version*: string - buildNumber*, launchSoundPath*, contentWarning*, contentWarning2*: string + name*, author*, description*, bundleId*, imagePath*, version*, buildNumber*: string + launchSoundPath*, contentWarning*, contentWarning2*: Option[string] + +proc isSome(value: string): bool = not value.isEmptyOrWhitespace proc `$`*(pdx: PdxInfo): string = for key, value in pdx.fieldPairs: - if value != "": - result &= key & "=" & value & "\n" + if value.isSome: + let str = when value is Option: value.get() else: value + result &= key & "=" & str & "\n" proc write*(pdx: PdxInfo) = ## Writes the pdxinfo file @@ -19,14 +22,16 @@ proc join*(a, b: PdxInfo): PdxInfo = ## Combins two PdxInfo instances result = a for current, override in fields(result, b): - if override != "": + if override.isSome: current = override proc parsePdx*(data: Stream, filename: string): PdxInfo = ## Parses a pdx config from a string let dict = loadConfig(data, filename) for key, value in result.fieldPairs: - value = dict.getSectionValue("", key) + let raw = dict.getSectionValue("", key) + if raw != "": + value = when typeof(value) is Option: some(raw) else: raw proc readPdx*(path: string): PdxInfo = ## Creates a pdx by reading a local pxinfo file diff --git a/tests/t_pdxinfo.nim b/tests/t_pdxinfo.nim index d1213ac..8ceb46c 100644 --- a/tests/t_pdxinfo.nim +++ b/tests/t_pdxinfo.nim @@ -1,4 +1,4 @@ -import std/[unittest, strutils, streams], playdate/build/[pdxinfo, nimbledump] +import std/[unittest, strutils, streams, options], playdate/build/[pdxinfo, nimbledump] suite "Pdxinfo generation": @@ -56,12 +56,16 @@ suite "Pdxinfo generation": imagePath: "launcher", version: "1.2.3", buildNumber: "20250216", + launchSoundPath: some("foo"), + contentWarning2: some("above"), ) let pdx2 = PdxInfo( author: "Twas Brillig", bundleId: "com.twasbrillig.loremipsum", version: "3.4.5", + contentWarning: some("look out"), + contentWarning2: some("below"), ) check($join(pdx1, pdx2) == """ @@ -72,4 +76,7 @@ suite "Pdxinfo generation": imagePath=launcher version=3.4.5 buildNumber=20250216 + launchSoundPath=foo + contentWarning=look out + contentWarning2=below """.dedent())