Files
Nim/tools/nimblepkglist.nim
ee7 e8657c7107 make implicit cstring conversions explicit (#19488)
The Nim manual says that an implicit conversion to cstring will
eventually not be allowed [1]:

    A Nim `string` is implicitly convertible to `cstring` for convenience.

    [...]

    Even though the conversion is implicit, it is not *safe*: The garbage collector
    does not consider a `cstring` to be a root and may collect the underlying
    memory. For this reason, the implicit conversion will be removed in future
    releases of the Nim compiler. Certain idioms like conversion of a `const` string
    to `cstring` are safe and will remain to be allowed.

And from Nim 1.6.0, such a conversion triggers a warning [2]:

    A dangerous implicit conversion to `cstring` now triggers a `[CStringConv]` warning.
    This warning will become an error in future versions! Use an explicit conversion
    like `cstring(x)` in order to silence the warning.

However, some files in this repo produced such a warning. For example,
before this commit, compiling `parsejson.nim` would produce:

    /foo/Nim/lib/pure/parsejson.nim(221, 37) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]
    /foo/Nim/lib/pure/parsejson.nim(231, 39) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]

This commit resolves the most visible `CStringConv` warnings, making the
cstring conversions explicit.

[1] https://github.com/nim-lang/Nim/blob/d2318d9ccfe6/doc/manual.md#cstring-type
[2] https://github.com/nim-lang/Nim/blob/d2318d9ccfe6/changelogs/changelog_1_6_0.md#type-system
2022-08-19 15:40:53 -04:00

81 lines
2.4 KiB
Nim

#[
deadcode?
]#
import base64, strutils, json, htmlgen, dom, algorithm
type
TData = object
content {.importc.}: cstring
proc decodeContent(content: string): string =
result = ""
for line in content.splitLines:
if line != "":
result.add decode(line)
proc contains(x: seq[JSonNode], s: string): bool =
for i in x:
assert i.kind == JString
if i.str == s: return true
proc processContent(content: string) =
var jsonDoc = parseJson(content)
assert jsonDoc.kind == JArray
var jsonArr = jsonDoc.elems
jsonArr.sort do (x, y: JsonNode) -> int:
strutils.cmpIgnoreCase(x["name"].str, y["name"].str)
var
officialList = ""
officialCount = 0
unofficialList = ""
unofficialCount = 0
let
endings = {'.', '!'}
for pkg in jsonArr:
assert pkg.kind == JObject
if not pkg.hasKey"url": continue
let pkgWeb =
if pkg.hasKey("web"): pkg["web"].str
else: pkg["url"].str
let
desc = pkg["description"].str
dot = if desc.high > 0 and desc[desc.high] in endings: "" else: "."
listItem = li(a(href=pkgWeb, pkg["name"].str), " ", desc & dot)
if pkg["url"].str.startsWith("https://github.com/nim-lang") or
pkg["url"].str.startsWith("git://github.com/nim-lang") or
"official" in pkg["tags"].elems:
officialCount.inc
officialList.add listItem & "\n"
else:
unofficialCount.inc
unofficialList.add listItem & "\n"
var officialPkgListDiv = document.getElementById("officialPkgList")
officialPkgListDiv.innerHTML =
(p("There are currently " & $officialCount &
" official packages in the Nimble package repository.") &
ul(officialList)).cstring
var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
unofficialPkgListDiv.innerHTML =
(p("There are currently " & $unofficialCount &
" unofficial packages in the Nimble package repository.") &
ul(unofficialList)).cstring
proc gotPackageList(apiReply: TData) {.exportc.} =
let decoded = decodeContent($apiReply.content)
try:
processContent(decoded)
except:
var officialPkgListDiv = document.getElementById("officialPkgList")
var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
let msg = p("Unable to retrieve package list: ",
code(getCurrentExceptionMsg()))
officialPkgListDiv.innerHTML = msg.cstring
unofficialPkgListDiv.innerHTML = msg.cstring