Skip to content

Commit

Permalink
Format code.
Browse files Browse the repository at this point in the history
Signed-off-by: LebJe <[email protected]>
  • Loading branch information
LebJe committed Nov 19, 2024
1 parent 30598e4 commit f9ee400
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.4.1](https://github.com/LebJe/toml.lua/releases/tag/0.4.1) - 2024-11-19

### Fixed
- The formatting options that were passed as a parameter to `toml.encode`, `toml.encodeToFile`, `toml.toJSON`, and `toml.toYAML` previously had no effect when overriding values.
- Resolved CMake `FetchContent_Populate` warning.

- The formatting options that were passed as a parameter to `toml.encode`, `toml.encodeToFile`, `toml.toJSON`, and `toml.toYAML` previously had no effect when overriding values.
- Resolved CMake `FetchContent_Populate` warning.

### Changed

- Updated to magic_enum v0.9.7.

### Added
- Added tests for encoding options.

- Added tests for encoding options.

## [0.4.0](https://github.com/LebJe/toml.lua/releases/tag/0.4.0) - 2024-01-02

Expand Down
13 changes: 7 additions & 6 deletions src/utilities/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,27 @@ toml::format_flags tableToFormatFlags(sol::optional<sol::table> t) {

// User passed an empty table to clear all flags.
if (table.empty()) return flags;

// Set default flags, and allow user to override
std::map<format_flags, bool> userFlags = defaultFlags;

for (auto [flag, enabled] : userFlags) {
std::string camelCaseFlagName = camelCase(magic_enum::enum_name(flag));
if (table[camelCaseFlagName].valid()) {
userFlags[flag] = table[camelCaseFlagName].get<bool>();
}
}

// `format_flags::indentation` is returned as an empty string from `magic_enum::enum_name`, so we must handle it separately.

// `format_flags::indentation` is returned as an empty string from `magic_enum::enum_name`, so
// we must handle it separately.
if (table["indentation"].valid()) {
userFlags[toml::format_flags::indentation] = table["indentation"].get<bool>();
}

for (auto [flag, enabled] : userFlags) {
if (enabled) flags |= flag;
}

return flags;
}

Expand Down
10 changes: 5 additions & 5 deletions src/utilities/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ std::string parseErrorToString(toml::parse_error e);
/// Inserts the values in `e` into `table`.
void parseErrorToTable(toml::parse_error e, sol::table & table);

/// Takes a Lua table, with keys representing flag names, and values, and converts it to `toml::format_flags`.
/// If the table is nil, all format flags are set to their default value, if the table is empty, `toml::format_flags` is set to none.
/// Takes a Lua table, with keys representing flag names, and values, and converts it to
/// `toml::format_flags`. If the table is nil, all format flags are set to their default value, if
/// the table is empty, `toml::format_flags` is set to none.
toml::format_flags tableToFormatFlags(sol::optional<sol::table> t);

Options tableToOptions(sol::optional<sol::table> t);
Expand All @@ -56,9 +57,8 @@ std::optional<std::string> keyToString(sol::object key);
/// If a string is not on the stack, then an integer from `luaL_argerror` is returned.
std::variant<int, toml::table *> getTableFromStringInState(sol::state_view state, int index = 1);

template <>
struct magic_enum::customize::enum_range<toml::format_flags> {
static constexpr bool is_flags = true;
template <> struct magic_enum::customize::enum_range<toml::format_flags> {
static constexpr bool is_flags = true;
};

#endif /* UTILITIES_H */
6 changes: 4 additions & 2 deletions tests/tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ end
function TestEncoder:testEncodingOptions()
local terseKeysToml = read("tests/test-data/encoding/terseKeys+qoutedTimestamps.toml")

lu.assertEquals(toml.encode(toml.decode(terseKeysToml), { terseKeyValuePairs = true, quoteDatesAndTimes = true }), terseKeysToml)
lu.assertEquals(
toml.encode(toml.decode(terseKeysToml), { terseKeyValuePairs = true, quoteDatesAndTimes = true }),
terseKeysToml
)

local noIndentationJSON = read("tests/test-data/encoding/noIndentation.json")

lu.assertEquals(toml.toJSON(toml.decode(terseKeysToml), { indentation = false }), noIndentationJSON)

end

function TestDecoder:testDecodeSamples()
Expand Down

0 comments on commit f9ee400

Please sign in to comment.