Skip to content

Commit

Permalink
Add TOML feature (#75)
Browse files Browse the repository at this point in the history
Adds rustg_read_toml_file, which takes a filepath and spits out the output after decoding (a list, probably).

Uses the rust-toml lib
  • Loading branch information
Mothblocks authored Sep 20, 2021
1 parent 03c0c37 commit fb02e59
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ dashmap = { version = "4.0", optional = true }
zip = { version = "0.5.8", optional = true }
rand = {version = "0.8", optional = true}
dmsort = {version = "1.0.0", optional = true}
toml-dep = { version = "0.5.8", package="toml", optional = true }

[features]
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "url"]
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "toml", "url"]

# default features
cellularnoise = ["rand"]
Expand All @@ -55,6 +56,7 @@ http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
json = ["serde", "serde_json"]
log = ["chrono"]
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
toml = ["serde", "serde_json", "toml-dep"]
url = ["url-dep", "percent-encoding"]

# non-default features
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ The default features are:
* log: Faster log output.
* sql: Asynchronous MySQL/MariaDB client library.
* noise: 2d Perlin noise.
* toml: TOML parser.

Additional features are:
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.
Expand Down
1 change: 1 addition & 0 deletions dmsrc/toml.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define rustg_read_toml_file(path) json_decode(call(RUST_G, "toml_file_to_json")(path) || "null")
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub enum Error {
#[cfg(feature = "http")]
#[error(transparent)]
RequestError(#[from] reqwest::Error),
#[cfg(feature = "toml")]
#[error(transparent)]
TomlDeserializationError(#[from] toml_dep::de::Error),
#[cfg(feature = "http")]
#[error(transparent)]
SerializationError(#[from] serde_json::Error),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod log;
pub mod noise_gen;
#[cfg(feature = "sql")]
pub mod sql;
#[cfg(feature = "toml")]
pub mod toml;
#[cfg(feature = "unzip")]
pub mod unzip;
#[cfg(feature = "url")]
Expand Down
13 changes: 13 additions & 0 deletions src/toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::error::Result;

byond_fn! { toml_file_to_json(path) {
toml_file_to_json_impl(path).ok()
} }

fn toml_file_to_json_impl(path: &str) -> Result<String> {
Ok(serde_json::to_string(&toml_dep::from_str::<
toml_dep::Value,
>(&std::fs::read_to_string(
path,
)?)?)?)
}
6 changes: 6 additions & 0 deletions tests/dm-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ fn git() {
run_dm_tests("git");
}

#[cfg(feature = "toml")]
#[test]
fn toml() {
run_dm_tests("toml");
}

#[cfg(feature = "url")]
#[test]
fn url() {
Expand Down
23 changes: 23 additions & 0 deletions tests/dm/toml.dme
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "common.dm"

var/test_toml = @{"
[database]
enabled = true
ports = [ 8000, 25565 ]
data = [ ["delta", "phi"] ]
temp_targets = { cpu = 79, case = 72 }
"}

var/test_json = @{"
{"database":{"data":[["delta","phi"]],"enabled":true,"ports":[8000,25565],"temp_targets":{"case":72,"cpu":79}}}
"}

/test/proc/check_toml_file2json()
rustg_file_write(test_toml, "test.toml")

var/toml_output = json_encode(rustg_read_toml_file("test.toml"))
var/test_output = json_encode(json_decode(test_json)) // Double-encode so true becomes 1

// ~= checks for structural equality
if (toml_output != test_output)
CRASH("test:\n[test_toml]\n \nexpected:\n[test_output]\n \nrustg:\n[toml_output]")

0 comments on commit fb02e59

Please sign in to comment.