From ad1c17d112acde151721d27eb5d674d3d83f5bb8 Mon Sep 17 00:00:00 2001 From: AffectedArc07 <25063394+AffectedArc07@users.noreply.github.com> Date: Mon, 3 May 2021 17:29:16 +0100 Subject: [PATCH] Adds toml2json helper --- Cargo.lock | 12 +++++++++++- Cargo.toml | 6 ++++-- build.rs | 8 ++++++++ src/lib.rs | 2 ++ src/toml2json.rs | 9 +++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/toml2json.rs diff --git a/Cargo.lock b/Cargo.lock index efe1f951..468b7eac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1575,7 +1575,7 @@ dependencies = [ [[package]] name = "rust-g" -version = "0.4.5-P2" +version = "0.4.5-P3" dependencies = [ "chrono", "crypto-hash", @@ -1592,6 +1592,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "toml", "url", ] @@ -2000,6 +2001,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 44d04c41..150a2129 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rust-g" edition = "2018" -version = "0.4.5-P2" +version = "0.4.5-P3" authors = ["Bjorn Neergaard "] repository = "https://github.com/ParadiseSS13/rust-g" license-file = "LICENSE" @@ -32,6 +32,7 @@ serde_derive = { version = "1.0", optional = true } once_cell = { version = "1.4", optional = true } mysql = { version = "19.0", optional = true } dashmap = { version = "3.11", optional = true } +toml = { version = "0.5.8", optional = true } [features] default = ["dmi", "log", "git", "http", "sql", "noise"] @@ -42,4 +43,5 @@ log = ["chrono"] url = ["url-dep", "percent-encoding"] git = ["git2", "chrono"] http = ["reqwest", "serde", "serde_json", "serde_derive", "once_cell"] -sql = ["mysql", "serde", "serde_derive", "serde_json", "once_cell", "dashmap"] \ No newline at end of file +sql = ["mysql", "serde", "serde_derive", "serde_json", "once_cell", "dashmap"] +toml2json = ["toml", "serde_json"] \ No newline at end of file diff --git a/build.rs b/build.rs index e9ece6e9..628986cd 100644 --- a/build.rs +++ b/build.rs @@ -159,6 +159,14 @@ fn main() { "#).unwrap(); } + // module: sql + if enabled!("TOML2JSON") { + write!(f, r#" +// toml2json stuff // +#define rustg_toml2json(tomlfile) call(RUST_G, "toml2json")(tomlfile) +"#).unwrap(); + } + // Version footer write!(f, " // RUSTG Version // diff --git a/src/lib.rs b/src/lib.rs index bde552c1..7040157a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,8 @@ pub mod noise_gen; pub mod sql; #[cfg(feature = "url")] pub mod url; +#[cfg(feature = "toml2json")] +pub mod toml2json; #[cfg(not(target_pointer_width = "32"))] compile_error!("rust-g must be compiled for a 32-bit target"); diff --git a/src/toml2json.rs b/src/toml2json.rs new file mode 100644 index 00000000..78fa5782 --- /dev/null +++ b/src/toml2json.rs @@ -0,0 +1,9 @@ +use std::fs; + +byond_fn! { toml2json(tomlfile) { + // This error handling may look weird, but trust me + let content = fs::read_to_string(tomlfile).unwrap_or("".to_string()); // If the file doesnt exist, just report back empty toml + let val: toml::Value = toml::from_str(&content).unwrap_or(toml::from_str("").unwrap()); // If the toml is malformed, just report back empty json + let json_data = serde_json::to_string(&val).unwrap(); + Some(json_data) +} }