Browse Source

feat: convert yaml or toml to json format

dev
Dnomd343 2 years ago
parent
commit
586cf9cfa6
  1. 84
      src/to-json/Cargo.lock
  2. 3
      src/to-json/Cargo.toml
  3. 2
      src/to-json/src/ffi.rs
  4. 32
      src/to-json/src/json.rs
  5. 1
      src/to-json/src/lib.rs

84
src/to-json/Cargo.lock

@ -2,6 +2,90 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "to-json" name = "to-json"
version = "0.1.0" 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"

3
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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde_json = "1.0.87"
serde_yaml = "0.9.14"
toml = "0.5.9"

2
src/to-json/src/ffi.rs

@ -1,4 +1,5 @@
use std::ffi::{c_char, CStr, CString}; use std::ffi::{c_char, CStr, CString};
use crate::json::to_json;
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn free_rust_string(string: *const c_char) { 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); println!("Raw content -> {}", content);
// TODO: convert to JSON format // TODO: convert to JSON format
let content: String = to_json(content); // may return empty string
CString::new(content).unwrap().into_raw() CString::new(content).unwrap().into_raw()
} }

32
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<Format> {
if let Ok(data) = json::from_str::<json::Value>(content) { // try JSON format
return Some(Format::JSON(data));
}
if let Ok(data) = toml::from_str::<toml::Value>(content) { // try TOML format
return Some(Format::TOML(data));
}
if let Ok(data) = yaml::from_str::<yaml::Value>(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
}
}

1
src/to-json/src/lib.rs

@ -1 +1,2 @@
mod ffi; mod ffi;
mod json;

Loading…
Cancel
Save