Skip to content

Commit

Permalink
Update to Toml++ 3.0.0 (#1)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
LebJe authored Apr 8, 2022
1 parent 5c39cf8 commit 6694a00
Show file tree
Hide file tree
Showing 23 changed files with 12,879 additions and 8,208 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[submodule "tomlplusplus"]
path = tomlplusplus
url = https://github.com/marzer/tomlplusplus
fetchRecurseSubmodules = false
[submodule "sol2"]
path = sol2
url = https://github.com/ThePhD/sol2
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
rev: "62302476d0da01515660132d76902359bed0f782"
hooks:
- id: "clang-format"
exclude: "sol2/|src/toml.hpp|src/sol/"
exclude: "sol2/tomlplusplus/|src/include"
- repo: "https://github.com/LebJe/PreCommitStyLua.git"
rev: "c7f96e8ba459f1c6c27cb21cc6a9dc05e14745ca"
hooks:
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0](https://github.com/LebJe/toml.lua/releases/tag/0.0.4) - 2022-04-08

### Added

- Upgrade to [toml++ 3.0.1](https://github.com/marzer/tomlplusplus/releases/tag/v3.0.1)
- TOML documents can be converted to JSON or YAML.
- Formatting options can be passed to the `encode`, `tomlToJSON`, and `tomlToYAML` functions.

### Removed

- Removed `formattedReason` from decoding error messages.

## [0.0.4](https://github.com/LebJe/toml.lua/releases/tag/0.0.4) - 2021-11-24

### Added
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ project(toml.lua VERSION ${TOML_LUA_VERSION})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

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

include_directories(${LUA_INCLUDE_DIR} src)
include_directories(${LUA_INCLUDE_DIR} src src/include)

set(SOURCES
src/toml.cpp
Expand Down
97 changes: 71 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github.
- [Decoding](#decoding)
- [Encoding](#encoding)
- [Error Handling](#error-handling)
- [TOML To JSON](#toml-to-json)
- [TOML Conversion](#toml-conversion)
- [JSON](#json)
- [YAML](#yaml)
- [Output Formatting](#output-formatting)
- [Dependencies](#dependencies)
- [Licenses](#licenses)
- [Contributing](#contributing)

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

<!--te-->

Expand All @@ -48,10 +51,10 @@ luarocks install toml

### Manual Compilation

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

> 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:
> `LUA_DIR=/usr/local/openresty/luajit cmake -H. -Bbuild -G<generator-name>`
> `LUA_DIR=/usr/local/openresty/luajit cmake -S . -B build -G <generator-name>`
2. Run `cmake --build build --config Release` to build the project.
3. You will find the `toml.so` (or `toml.dll`) dynamic library in the `build` folder.
Expand Down Expand Up @@ -190,16 +193,17 @@ else
column = 9,
line = 4
},
formattedReason = "Error while parsing floating-point: expected decimal digit, saw '\\n' (at line 4, column 9)",
reason = "Error while parsing floating-point: expected decimal digit, saw '\\n'"
}
--]]
end
```

### TOML To JSON
### TOML Conversion

```lua
local toml = require("toml")

local tomlStr = [[
a = 1275892
b = 'Hello, World!'
Expand All @@ -212,45 +216,86 @@ g = 1979-05-27
h = 07:32:00
i = 1979-05-27T07:32:00-07:00
]]
```

local toml = require("toml")
local json = toml.tomlToJSON(tomlStr)
#### JSON

```lua
local json = toml.tomlToJSON(tomlStr)
print(json)
```

--[[
#### YAML

```lua
local yaml = toml.tomlToYAML(tomlStr)
print(yaml)
```

### Output Formatting

`toml.encode`, `toml.tomlToJSON`, and `toml.tomlToYAML` all take an optional second parameter: a table containing keys that disable or enable different formatting options.
Passing an empty table removes all options, while not providing a table will use the default options.

```lua
{
"a" : 1275892,
"b" : "Hello, World!",
"c" : true,
"d" : 124.2548,
"e" : {
"f" : [
1,
2,
3,
"4",
5.142
],
"g" : "1979-05-27",
"h" : "07:32:00",
"i" : "1979-05-27T07:32:00-07:00"
}
--- Dates and times will be emitted as quoted strings.
quoteDatesAndTimes = true,

--- Infinities and NaNs will be emitted as quoted strings.
quoteInfinitesAndNaNs = false,

--- Strings will be emitted as single-quoted literal strings where possible.
allowLiteralStrings = false,

--- Strings containing newlines will be emitted as triple-quoted 'multi-line' strings where possible.
allowMultiLineStrings = false,

--- Allow real tab characters in string literals (as opposed to the escaped form `\t`).
allowRealTabsInStrings = false,

--- Allow non-ASCII characters in strings (as opposed to their escaped form, e.g. `\u00DA`).
allow_unicode_strings = true,

--- Allow integers with #value_flags::format_as_binary to be emitted as binary.
allowBinaryIntegers = true,

--- Allow integers with #value_flags::format_as_octal to be emitted as octal.
allowOctalIntegers = true,

--- Allow integers with #value_flags::format_as_hexadecimal to be emitted as hexadecimal.
allowHexadecimalIntegers = true,

--- Apply indentation to tables nested within other tables/arrays.
indentSubTables = true,

--- Apply indentation to array elements when the array is forced to wrap over multiple lines.
indentArrayElements = true,

--- Combination of `indentSubTables` and `indentArrayElements`.
indentation = true,

--- Emit floating-point values with relaxed (human-friendly) precision.
relaxedFloatPrecision = false
}
--]]
```

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

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

## Licenses

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

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

The [magic_enum](https://github.com/Neargye/magic_enum) license is available in [its repository](https://github.com/Neargye/magic_enum/blob/master/LICENSE).

## Contributing

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:
Expand Down
7 changes: 5 additions & 2 deletions examples/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ end

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

print(toml.encode(data))
print(toml.encode(data, { indentation = true }))

print("\n\nTOML to JSON:\n")
print(toml.tomlToJSON(tomlStr))
print(toml.tomlToJSON(tomlStr, { indentation = true }))

print("\n\nTOML to YAML:\n")
print(toml.tomlToYAML(tomlStr, { indentation = true }))
8 changes: 3 additions & 5 deletions spec/decoding_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe("toml.decode()", function()
line = 7,
column = 1,
},
formattedReason = "Error while parsing table header: cannot redefine existing table 'fruit' as array-of-tables (at line 7, column 1)",
reason = "Error while parsing table header: cannot redefine existing table 'fruit' as array-of-tables",
}

Expand All @@ -60,14 +59,13 @@ describe("toml.decode()", function()
local expectedError2 = {
begin = {
line = 10,
column = 1,
column = 7,
},
["end"] = {
line = 10,
column = 1,
column = 7,
},
formattedReason = "Error while parsing key-value pair: cannot redefine existing integer as dotted key-value pair (at line 10, column 1)",
reason = "Error while parsing key-value pair: cannot redefine existing integer as dotted key-value pair",
reason = "Error while parsing key-value pair: cannot redefine existing table as dotted key-value pair",
}

assert.has_error(function()
Expand Down
6 changes: 3 additions & 3 deletions spec/encoding_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ describe("toml.encode()", function()
local expectedTOML = fh:read("*all")
fh:close()

assert.are.same(toml.encode(data.tableForTestToml), expectedTOML)
assert.are.same(toml.encode(data.tableForTestToml, {}), expectedTOML)
end)

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

assert.are.same(toml.encode(data.tableForTest2Toml), expectedTOML)
assert.are.same(toml.encode(data.tableForTest2Toml, {}), expectedTOML)
end)

it("can encode massive table", function()
local returnedTOML = toml.encode(data.tableForMassiveToml)
local returnedTOML = toml.encode(data.tableForMassiveToml, {})

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

0 comments on commit 6694a00

Please sign in to comment.