Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ package-lock.json
nimble.develop
nimble.paths
vendor
tests/test_all
4 changes: 2 additions & 2 deletions toml_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ template tomlDecodeImpl*(input: untyped,
# from the fact that the dynamic dispatch mechanisms used in
# faststreams may be reading from a file or a network device.
try:
let stream = memInputStream(input)
let stream = unsafeMemoryInput(input)
var reader = unpackArgs(init, [TomlReader, stream, tomlCase, params])
when RecordType is (seq or array) and uTypeIsRecord(RecordType):
reader.readTableArray(RecordType, key, tomlCase)
Expand Down Expand Up @@ -143,7 +143,7 @@ template tomlLoadImpl*(filename: string,
var stream: InputStream
when nimvm:
let input = staticRead(filename)
stream = VMInputStream(pos: 0, data: toVMString(input))
stream = unsafeMemoryInput(input)
else:
stream = memFileInput(filename)
try:
Expand Down
1 change: 1 addition & 0 deletions toml_serialization.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "Flexible TOML serialization [not] relying on run-time type info
license = "Apache License 2.0"
skipDirs = @["tests", "assets"]

requires "https://github.com/nitely/nim-faststreams#768bbbdc7db4125afbd5d7da07e067903f29942b"
requires "nim >= 1.6.0",
"serialization",
"stew"
Expand Down
17 changes: 4 additions & 13 deletions toml_serialization/lexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,13 @@ const
invalidCommentChar = {'\x00'..'\x08', '\x0A'..'\x1F', '\x7F'}

template readChar*(lex: TomlLexer): char =
when nimvm:
read(VMInputStream(lex.stream))
else:
char inputs.read(lex.stream)
char inputs.read(lex.stream)

template readable*(lex: TomlLexer): bool =
when nimvm:
readable(VMInputStream(lex.stream))
else:
lex.stream.readable()
lex.stream.readable()

template peekChar*(lex: TomlLexer): char =
when nimvm:
peekChar(VMInputStream(lex.stream))
else:
lex.stream.peek().char
lex.stream.peek().char

proc lineInfo(lex: TomlLexer): (int, int) {.inline.} =
(lex.line, lex.col)
Expand Down Expand Up @@ -1930,7 +1921,7 @@ proc parseKeyValue*(lex: var TomlLexer,
checkEol(lex, line)

proc parseKey*(key: string, tomlCase: TomlCase): seq[string] =
let stream = memInputStream(key)
let stream = unsafeMemoryInput(key)
var lex = init(TomlLexer, stream)
lex.scanKey(result)

Expand Down
29 changes: 6 additions & 23 deletions toml_serialization/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -245,33 +245,16 @@ proc toUgly(result: var string, p: TomlValueRef) =
proc `$`*(p: TomlValueRef): string =
toUgly(result, p)

type
VMInputStream* = ref object of InputStream
pos*: int
data*: string

proc read*(s: VMInputStream): char =
result = s.data[s.pos]
inc s.pos

proc readable*(s: VMInputStream): bool =
s.pos < s.data.len
proc peekChar*(s: InputStream): char {.deprecated: "use s.peek().char".} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toml is text based so it stands to reason to have a dedicated char peeker - that said, I don't know that utf8 is being validated correctly when using it so one could also argue we should not have this .. hm .. cc @jangko

Copy link
Contributor Author

@nitely nitely Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original required peekChar(VMInputStream(s)), but VMInputStream got removed, so I think it's fine to remove peekChar too. If anything peekChar(InputStream) should be defined in faststreams textio module I think. There is also peekChar(TomlLexer) already.

s.peek().char

proc peekChar*(s: VMInputStream): char =
s.data[s.pos]

template toVMString*(x: openArray[byte]): string =
template toVMString*(x: openArray[byte]): string {.deprecated.} =
var z = newString(x.len)
for i, c in x: z[i] = char(c)
z

template toVMString*(x: string): string =
template toVMString*(x: string): string {.deprecated.} =
x

template memInputStream*(input: untyped): auto =
var stream: InputStream
when nimvm:
stream = VMInputStream(pos: 0, data: toVMString(input))
else:
stream = unsafeMemoryInput(input)
stream
template memInputStream*(input: untyped): auto {.deprecated: "use unsafeMemoryInput".} =
unsafeMemoryInput(input)
Loading