Skip to content

Commit 6694a00

Browse files
authored
Update to Toml++ 3.0.0 (#1)
- Update to toml++ v3 - Improve code in src/utilites.cpp - Add magic_enum as a dependency - Update CHANGELOG - move some headers to `src/include` - Fix tests
1 parent 5c39cf8 commit 6694a00

23 files changed

Lines changed: 12879 additions & 8208 deletions

.gitmodules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[submodule "tomlplusplus"]
22
path = tomlplusplus
33
url = https://github.com/marzer/tomlplusplus
4+
fetchRecurseSubmodules = false
45
[submodule "sol2"]
56
path = sol2
67
url = https://github.com/ThePhD/sol2

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
rev: "62302476d0da01515660132d76902359bed0f782"
1212
hooks:
1313
- id: "clang-format"
14-
exclude: "sol2/|src/toml.hpp|src/sol/"
14+
exclude: "sol2/tomlplusplus/|src/include"
1515
- repo: "https://github.com/LebJe/PreCommitStyLua.git"
1616
rev: "c7f96e8ba459f1c6c27cb21cc6a9dc05e14745ca"
1717
hooks:

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.0](https://github.com/LebJe/toml.lua/releases/tag/0.0.4) - 2022-04-08
9+
10+
### Added
11+
12+
- Upgrade to [toml++ 3.0.1](https://github.com/marzer/tomlplusplus/releases/tag/v3.0.1)
13+
- TOML documents can be converted to JSON or YAML.
14+
- Formatting options can be passed to the `encode`, `tomlToJSON`, and `tomlToYAML` functions.
15+
16+
### Removed
17+
18+
- Removed `formattedReason` from decoding error messages.
19+
820
## [0.0.4](https://github.com/LebJe/toml.lua/releases/tag/0.0.4) - 2021-11-24
921

1022
### Added

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ project(toml.lua VERSION ${TOML_LUA_VERSION})
2626
set(CMAKE_CXX_STANDARD 17)
2727
set(CMAKE_CXX_STANDARD_REQUIRED True)
2828

29-
# compile in release mode by default
29+
# compile in release mode by default
3030
if(NOT CMAKE_BUILD_TYPE)
3131
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
3232
endif()
@@ -59,7 +59,7 @@ if(NOT LUA_INCLUDE_DIR OR (WIN32 AND NOT LUA_LIBRARIES))
5959
find_package(Lua REQUIRED)
6060
endif()
6161

62-
include_directories(${LUA_INCLUDE_DIR} src)
62+
include_directories(${LUA_INCLUDE_DIR} src src/include)
6363

6464
set(SOURCES
6565
src/toml.cpp

README.md

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github.
2020
- [Decoding](#decoding)
2121
- [Encoding](#encoding)
2222
- [Error Handling](#error-handling)
23-
- [TOML To JSON](#toml-to-json)
23+
- [TOML Conversion](#toml-conversion)
24+
- [JSON](#json)
25+
- [YAML](#yaml)
26+
- [Output Formatting](#output-formatting)
2427
- [Dependencies](#dependencies)
2528
- [Licenses](#licenses)
2629
- [Contributing](#contributing)
2730

28-
<!-- Added by: lebje, at: Wed Nov 24 13:59:16 EST 2021 -->
31+
<!-- Added by: lebje, at: Thu Dec 2 22:19:10 EST 2021 -->
2932

3033
<!--te-->
3134

@@ -48,10 +51,10 @@ luarocks install toml
4851

4952
### Manual Compilation
5053

51-
1. Run `cmake -H. -Bbuild -G<generator-name>` to generate the required files.
54+
1. Run `cmake -S . -B build -G <generator-name>` to generate the required files.
5255

5356
> If you have a non standard Lua install location, add the environment variable `LUA_DIR` and have it point to the directory containing the `include` and `lib` folders for your Lua installation. For example:
54-
> `LUA_DIR=/usr/local/openresty/luajit cmake -H. -Bbuild -G<generator-name>`
57+
> `LUA_DIR=/usr/local/openresty/luajit cmake -S . -B build -G <generator-name>`
5558
5659
2. Run `cmake --build build --config Release` to build the project.
5760
3. You will find the `toml.so` (or `toml.dll`) dynamic library in the `build` folder.
@@ -190,16 +193,17 @@ else
190193
column = 9,
191194
line = 4
192195
},
193-
formattedReason = "Error while parsing floating-point: expected decimal digit, saw '\\n' (at line 4, column 9)",
194196
reason = "Error while parsing floating-point: expected decimal digit, saw '\\n'"
195197
}
196198
--]]
197199
end
198200
```
199201

200-
### TOML To JSON
202+
### TOML Conversion
201203

202204
```lua
205+
local toml = require("toml")
206+
203207
local tomlStr = [[
204208
a = 1275892
205209
b = 'Hello, World!'
@@ -212,45 +216,86 @@ g = 1979-05-27
212216
h = 07:32:00
213217
i = 1979-05-27T07:32:00-07:00
214218
]]
219+
```
215220

216-
local toml = require("toml")
217-
local json = toml.tomlToJSON(tomlStr)
221+
#### JSON
218222

223+
```lua
224+
local json = toml.tomlToJSON(tomlStr)
219225
print(json)
226+
```
220227

221-
--[[
228+
#### YAML
229+
230+
```lua
231+
local yaml = toml.tomlToYAML(tomlStr)
232+
print(yaml)
233+
```
234+
235+
### Output Formatting
236+
237+
`toml.encode`, `toml.tomlToJSON`, and `toml.tomlToYAML` all take an optional second parameter: a table containing keys that disable or enable different formatting options.
238+
Passing an empty table removes all options, while not providing a table will use the default options.
239+
240+
```lua
222241
{
223-
"a" : 1275892,
224-
"b" : "Hello, World!",
225-
"c" : true,
226-
"d" : 124.2548,
227-
"e" : {
228-
"f" : [
229-
1,
230-
2,
231-
3,
232-
"4",
233-
5.142
234-
],
235-
"g" : "1979-05-27",
236-
"h" : "07:32:00",
237-
"i" : "1979-05-27T07:32:00-07:00"
238-
}
242+
--- Dates and times will be emitted as quoted strings.
243+
quoteDatesAndTimes = true,
244+
245+
--- Infinities and NaNs will be emitted as quoted strings.
246+
quoteInfinitesAndNaNs = false,
247+
248+
--- Strings will be emitted as single-quoted literal strings where possible.
249+
allowLiteralStrings = false,
250+
251+
--- Strings containing newlines will be emitted as triple-quoted 'multi-line' strings where possible.
252+
allowMultiLineStrings = false,
253+
254+
--- Allow real tab characters in string literals (as opposed to the escaped form `\t`).
255+
allowRealTabsInStrings = false,
256+
257+
--- Allow non-ASCII characters in strings (as opposed to their escaped form, e.g. `\u00DA`).
258+
allow_unicode_strings = true,
259+
260+
--- Allow integers with #value_flags::format_as_binary to be emitted as binary.
261+
allowBinaryIntegers = true,
262+
263+
--- Allow integers with #value_flags::format_as_octal to be emitted as octal.
264+
allowOctalIntegers = true,
265+
266+
--- Allow integers with #value_flags::format_as_hexadecimal to be emitted as hexadecimal.
267+
allowHexadecimalIntegers = true,
268+
269+
--- Apply indentation to tables nested within other tables/arrays.
270+
indentSubTables = true,
271+
272+
--- Apply indentation to array elements when the array is forced to wrap over multiple lines.
273+
indentArrayElements = true,
274+
275+
--- Combination of `indentSubTables` and `indentArrayElements`.
276+
indentation = true,
277+
278+
--- Emit floating-point values with relaxed (human-friendly) precision.
279+
relaxedFloatPrecision = false
239280
}
240-
--]]
241281
```
242282

283+
> The comments for the options are from [the tomlplusplus documentation](https://marzer.github.io/tomlplusplus/namespacetoml.html#a2102aa80bc57783d96180f36e1f64f6a)
284+
243285
## Dependencies
244286

245287
- [toml++](https://github.com/marzer/tomlplusplus/)
246288
- [sol2](https://github.com/ThePhD/sol2)
289+
- [magic_enum](https://github.com/Neargye/magic_enum)
247290

248291
## Licenses
249292

250293
The [toml++](https://github.com/marzer/tomlplusplus/) license is available in the `tomlplusplus` directory in the `LICENSE` file.
251294

252295
The [sol2](https://github.com/ThePhD/sol2) license is available in the `sol2` directory in the `LICENSE.txt` file.
253296

297+
The [magic_enum](https://github.com/Neargye/magic_enum) license is available in [its repository](https://github.com/Neargye/magic_enum/blob/master/LICENSE).
298+
254299
## Contributing
255300

256301
Before committing, please install [pre-commit](https://pre-commit.com), [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [StyLua](https://github.com/JohnnyMorganz/StyLua), and [Prettier](https://prettier.io), then install the pre-commit hook:

examples/example.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ end
4242

4343
print("\n\nEncoding:\n")
4444

45-
print(toml.encode(data))
45+
print(toml.encode(data, { indentation = true }))
4646

4747
print("\n\nTOML to JSON:\n")
48-
print(toml.tomlToJSON(tomlStr))
48+
print(toml.tomlToJSON(tomlStr, { indentation = true }))
49+
50+
print("\n\nTOML to YAML:\n")
51+
print(toml.tomlToYAML(tomlStr, { indentation = true }))

spec/decoding_spec.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ describe("toml.decode()", function()
4545
line = 7,
4646
column = 1,
4747
},
48-
formattedReason = "Error while parsing table header: cannot redefine existing table 'fruit' as array-of-tables (at line 7, column 1)",
4948
reason = "Error while parsing table header: cannot redefine existing table 'fruit' as array-of-tables",
5049
}
5150

@@ -60,14 +59,13 @@ describe("toml.decode()", function()
6059
local expectedError2 = {
6160
begin = {
6261
line = 10,
63-
column = 1,
62+
column = 7,
6463
},
6564
["end"] = {
6665
line = 10,
67-
column = 1,
66+
column = 7,
6867
},
69-
formattedReason = "Error while parsing key-value pair: cannot redefine existing integer as dotted key-value pair (at line 10, column 1)",
70-
reason = "Error while parsing key-value pair: cannot redefine existing integer as dotted key-value pair",
68+
reason = "Error while parsing key-value pair: cannot redefine existing table as dotted key-value pair",
7169
}
7270

7371
assert.has_error(function()

spec/encoding_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ describe("toml.encode()", function()
77
local expectedTOML = fh:read("*all")
88
fh:close()
99

10-
assert.are.same(toml.encode(data.tableForTestToml), expectedTOML)
10+
assert.are.same(toml.encode(data.tableForTestToml, {}), expectedTOML)
1111
end)
1212

1313
it("can encode another sample TOML document", function()
1414
local fh = io.open("spec/test-data/test2.toml")
1515
local expectedTOML = fh:read("*all")
1616
fh:close()
1717

18-
assert.are.same(toml.encode(data.tableForTest2Toml), expectedTOML)
18+
assert.are.same(toml.encode(data.tableForTest2Toml, {}), expectedTOML)
1919
end)
2020

2121
it("can encode massive table", function()
22-
local returnedTOML = toml.encode(data.tableForMassiveToml)
22+
local returnedTOML = toml.encode(data.tableForMassiveToml, {})
2323

2424
local fh = io.open("spec/test-data/massive.toml")
2525
local expectedTOML = fh:read("*all")

0 commit comments

Comments
 (0)