Skip to content

Commit

Permalink
Use Option for optional pdxinfo fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Feb 26, 2025
1 parent ed9b994 commit 75bfbdf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/playdate/build/pdxinfo.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/t_pdxinfo.nim
Original file line number Diff line number Diff line change
@@ -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":

Expand Down Expand Up @@ -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) == """
Expand All @@ -72,4 +76,7 @@ suite "Pdxinfo generation":
imagePath=launcher
version=3.4.5
buildNumber=20250216
launchSoundPath=foo
contentWarning=look out
contentWarning2=below
""".dedent())

0 comments on commit 75bfbdf

Please sign in to comment.