Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse_as_tbl function to parse tables that can be looped over #13

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
110 changes: 64 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "toml-edit-lua"
version = "0.2.1"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -17,9 +17,10 @@ name = "toml_edit"
crate-type = ["cdylib"]

[dependencies]
mlua = { version = "0.9.1", features = ["module", "serialize"] }
mlua = { version = "0.9.6", features = ["module", "serialize"] }
serde = "1.0"
toml_edit = "0.19.14"
toml_edit = "0.22.9"
toml = "0.8.12"

[target.x86_64-apple-darwin]
rustflags = [
Expand Down
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,46 @@ Edit toml files while preserving whitespace and formatting from Lua.

## Usage

The `parse` function creates a table with metatable,
which can be converted back to toml (preserving comments)
using `tostring`:

```lua
local toml_content = [[
[rocks]
# Some commment
"toml-edit" = "1.0.0"
[rocks]
# Some comment
"toml-edit" = "1.0.0"
]]
local toml_edit = require("toml-edit")
local toml_tbl = toml_edit.parse(toml_content)
toml_tbl.rocks["toml-edit"] = "2.0.0"
local new_content = tostring(toml_tbl)
print(tostring(toml_tbl))
-- outputs:
-- [rocks]
-- # Some comment
-- "toml-edit" = "2.0.0"
```

The `parse_as_tbl` function parses toml as a regular lua table:

```lua
local toml_content = [[
[rocks]
"toml-edit" = "1.0.0"
]]
local toml_edit = require("toml-edit")
local lua_tbl = toml_edit.parse_as_tbl(toml_content)
print(tostring(toml_tbl))
-- outputs: table: 0x7ff975807668
```

> [!TIP]
>
> - Use `parse` when you need to modify the toml, and you are accessing or
> setting fields by name.
> - Use `parse_as_tbl` when you need to perform operations that don't access
> fields by name (e.g. iterating over key/value pairs).

## Development

### To run tests:
Expand Down
5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@
buildInputs =
(with pkgs; [
rust-analyzer
rustc
])
++ (with pre-commit-hooks.packages.${system}; [
alejandra
rustfmt
stylua
clippy
])
++ oa.buildInputs;
++ oa.buildInputs
++ oa.nativeBuildInputs;

shellHook = ''
${oa.shellHook}
Expand Down
41 changes: 41 additions & 0 deletions spec/parse_as_tbl_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe("parse_as_tbl", function()
local toml_edit = require("toml_edit")
it("Can parse table", function()
local toml_content = [[
[rocks."toml-edit"]
version = "1.0.0"
opt = true
[rocks."foo"]
version = "2.0.0"
opt = false
]]
local result = toml_edit.parse_as_tbl(toml_content)
assert.are.same({
rocks = {
["toml-edit"] = {
version = "1.0.0",
opt = true,
},
foo = {
version = "2.0.0",
opt = false,
},
},
}, result)
end)
it("Can loop over fields", function()
local toml_content = [[
[rocks."toml-edit"]
version = "1.0.0"
opt = true
[rocks."foo"]
version = "2.0.0"
opt = false
]]
local result = toml_edit.parse_as_tbl(toml_content)
for k, v in pairs(result.rocks) do
assert.is_not_nil(k)
assert.is_not_nil(v)
end
end)
end)
2 changes: 1 addition & 1 deletion spec/toml_edit_spec.lua → spec/parse_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe("toml-edit", function()
describe("parse", function()
local toml_edit = require("toml_edit")
it("Can read from key", function()
local toml_content = [[
Expand Down
Loading
Loading