diff --git a/src/to-json/Cargo.lock b/src/to-json/Cargo.lock index 3efbc0d..71b6233 100644 --- a/src/to-json/Cargo.lock +++ b/src/to-json/Cargo.lock @@ -2,6 +2,90 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" + +[[package]] +name = "serde_json" +version = "1.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.9.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "to-json" version = "0.1.0" +dependencies = [ + "serde_json", + "serde_yaml", + "toml", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unsafe-libyaml" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" diff --git a/src/to-json/Cargo.toml b/src/to-json/Cargo.toml index a8c79b7..4e6f0af 100644 --- a/src/to-json/Cargo.toml +++ b/src/to-json/Cargo.toml @@ -9,3 +9,6 @@ crate-type = ["cdylib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde_json = "1.0.87" +serde_yaml = "0.9.14" +toml = "0.5.9" diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs index fef3e3a..fc0b297 100644 --- a/src/to-json/src/ffi.rs +++ b/src/to-json/src/ffi.rs @@ -1,4 +1,5 @@ use std::ffi::{c_char, CStr, CString}; +use crate::json::to_json; #[no_mangle] pub unsafe extern "C" fn free_rust_string(string: *const c_char) { @@ -13,6 +14,7 @@ pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { println!("Raw content -> {}", content); // TODO: convert to JSON format + let content: String = to_json(content); // may return empty string CString::new(content).unwrap().into_raw() } diff --git a/src/to-json/src/json.rs b/src/to-json/src/json.rs new file mode 100644 index 0000000..83efc4c --- /dev/null +++ b/src/to-json/src/json.rs @@ -0,0 +1,32 @@ +use serde_json as json; +use serde_yaml as yaml; + +enum Format { + JSON(json::Value), + YAML(yaml::Value), + TOML(toml::Value), +} + +fn parser(content: &str) -> Option { + if let Ok(data) = json::from_str::(content) { // try JSON format + return Some(Format::JSON(data)); + } + if let Ok(data) = toml::from_str::(content) { // try TOML format + return Some(Format::TOML(data)); + } + if let Ok(data) = yaml::from_str::(content) { // try YAML format + return Some(Format::YAML(data)); + } + return None; // parse failed +} + +pub fn to_json(content: &str) -> String { // convert to JSON format + match parser(content) { + Some(data) => match data { + Format::JSON(dat) => json::to_string(&dat).unwrap(), + Format::YAML(dat) => json::to_string(&dat).unwrap(), + Format::TOML(dat) => json::to_string(&dat).unwrap(), + }, + None => String::from(""), // failed -> empty string + } +} diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 57ae9b9..4f1b9a0 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1 +1,2 @@ mod ffi; +mod json;