Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 2f3d4db

Browse files
authored
Add rustg_toml_encode (tgstation#116)
Add rustg_toml_encode
1 parent 578a52d commit 2f3d4db

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

dmsrc/toml.dm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@
66
return json_decode(output["content"])
77
else
88
CRASH(output["content"])
9+
10+
#define rustg_raw_toml_encode(value) json_decode(call(RUST_G, "toml_encode")(json_encode(value)))
11+
12+
/proc/rustg_toml_encode(value)
13+
var/list/output = rustg_raw_toml_encode(value)
14+
if (output["success"])
15+
return output["content"]
16+
else
17+
CRASH(output["content"])

src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub enum Error {
3434
#[cfg(feature = "png")]
3535
#[error(transparent)]
3636
ImageEncoding(#[from] EncodingError),
37+
#[cfg(feature = "http")]
38+
#[error(transparent)]
39+
JsonSerialization(#[from] serde_json::Error),
3740
#[error(transparent)]
3841
ParseInt(#[from] ParseIntError),
3942
#[error(transparent)]
@@ -49,9 +52,9 @@ pub enum Error {
4952
#[cfg(feature = "toml")]
5053
#[error(transparent)]
5154
TomlDeserialization(#[from] toml_dep::de::Error),
52-
#[cfg(feature = "http")]
55+
#[cfg(feature = "toml")]
5356
#[error(transparent)]
54-
Serialization(#[from] serde_json::Error),
57+
TomlSerialization(#[from] toml_dep::ser::Error),
5558
#[cfg(feature = "unzip")]
5659
#[error(transparent)]
5760
Unzip(#[from] ZipError),

src/toml.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ fn toml_file_to_json_impl(path: &str) -> Result<String> {
2020
path,
2121
)?)?)?)
2222
}
23+
24+
byond_fn!(fn toml_encode(value) {
25+
serde_json::to_string(
26+
&match toml_encode_impl(value) {
27+
Ok(value) => serde_json::json!({
28+
"success": true, "content": value
29+
}),
30+
31+
Err(error) => serde_json::json!({
32+
"success": false, "content": error.to_string()
33+
}),
34+
}
35+
).ok()
36+
});
37+
38+
fn toml_encode_impl(value: &str) -> Result<String> {
39+
Ok(toml_dep::to_string_pretty(&serde_json::from_str::<
40+
toml_dep::Value,
41+
>(value)?)?)
42+
}

tests/dm/toml.dme

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ var/test_json = @{"
1919

2020
if (toml_output ~! json_decode(test_json))
2121
CRASH("test:\n[test_toml]\n \nexpected:\n[test_json]\n \nrustg:\n[json_encode(toml_output)]")
22+
23+
/test/proc/check_rustg_toml_encode()
24+
var/json_value = json_decode(test_json)
25+
rustg_file_write(rustg_toml_encode(json_value), "test_encode.toml")
26+
var/toml_output = rustg_read_toml_file("test_encode.toml")
27+
28+
if (toml_output ~! json_value)
29+
CRASH("test:\n[test_toml]\n \nexpected:\n[test_json]\n \nrustg:\n[json_encode(toml_output)]")

0 commit comments

Comments
 (0)