From fbc568099c4580fcd9981324dba2d1cf55d09777 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 19:57:31 +0800 Subject: [PATCH 01/11] refactor: parse process --- src/to-json/src/parser.rs | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/to-json/src/parser.rs diff --git a/src/to-json/src/parser.rs b/src/to-json/src/parser.rs new file mode 100644 index 0000000..1d84d18 --- /dev/null +++ b/src/to-json/src/parser.rs @@ -0,0 +1,43 @@ +use serde_json as json; +use serde_yaml as yaml; + +#[derive(Debug)] +pub enum Value { + JSON(json::Value), + YAML(yaml::Value), + TOML(toml::Value), +} + +fn json_parser(content: &str) -> Option { // parse json content + match json::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +fn yaml_parser(content: &str) -> Option { // parse yaml content + match yaml::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +fn toml_parser(content: &str) -> Option { // parse toml content + match toml::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +pub fn parser(content: &str) -> Result { + match json_parser(content) { // try JSON format + Some(data) => Ok(Value::JSON(data)), + None => match toml_parser(content) { // try TOML format + Some(data) => Ok(Value::TOML(data)), + None => match yaml_parser(content) { // try YAML format + Some(data) => Ok(Value::YAML(data)), + None => Err(String::from("unknown input format")), + } + } + } +} From 026a460af7f383ee488dd8bccde7216a1715bae2 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 20:07:24 +0800 Subject: [PATCH 02/11] feat: json convert interface --- src/to-json/src/convert.rs | 21 +++++++++++++++++++++ src/to-json/src/lib.rs | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 src/to-json/src/convert.rs diff --git a/src/to-json/src/convert.rs b/src/to-json/src/convert.rs new file mode 100644 index 0000000..55f9e91 --- /dev/null +++ b/src/to-json/src/convert.rs @@ -0,0 +1,21 @@ +use serde_json as json; +use crate::parser::{parser, Value}; + +fn json_convert(content: &str) -> Result { // convert to JSON format + let data = match parser(content)? { + Value::JSON(_json) => json::to_string(&_json), + Value::YAML(_yaml) => json::to_string(&_yaml), + Value::TOML(_toml) => json::to_string(&_toml), + }; + match data { + Ok(data) => Ok(data), + Err(err) => Err(err.to_string()), + } +} + +pub fn to_json(content: &str) -> Option { // to JSON string + match json_convert(content) { + Ok(data) => Some(data), + Err(_) => None, + } +} diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 4f1b9a0..ce4d1e6 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,2 +1,4 @@ mod ffi; mod json; +mod parser; +mod convert; From 3a66ce2533cc46131ed965dd97f1488806367e91 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 20:24:55 +0800 Subject: [PATCH 03/11] update: to-json ffi --- src/to-json/src/convert.rs | 7 +++---- src/to-json/src/ffi.rs | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/to-json/src/convert.rs b/src/to-json/src/convert.rs index 55f9e91..72d7dd0 100644 --- a/src/to-json/src/convert.rs +++ b/src/to-json/src/convert.rs @@ -1,11 +1,10 @@ -use serde_json as json; use crate::parser::{parser, Value}; fn json_convert(content: &str) -> Result { // convert to JSON format let data = match parser(content)? { - Value::JSON(_json) => json::to_string(&_json), - Value::YAML(_yaml) => json::to_string(&_yaml), - Value::TOML(_toml) => json::to_string(&_toml), + Value::JSON(_json) => serde_json::to_string(&_json), + Value::YAML(_yaml) => serde_json::to_string(&_yaml), + Value::TOML(_toml) => serde_json::to_string(&_toml), }; match data { Ok(data) => Ok(data), diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs index c9569be..466bdc4 100644 --- a/src/to-json/src/ffi.rs +++ b/src/to-json/src/ffi.rs @@ -1,14 +1,27 @@ +use crate::convert::to_json; use std::ffi::{c_char, CStr, CString}; -use crate::json::to_json; + +fn to_c_string(string: String) -> *const c_char { // fetch c-style ptr of string + CString::new(string).unwrap().into_raw() +} + +unsafe fn load_c_string(ptr: *const c_char) -> String { // load string from c-style ptr + String::from( + CStr::from_ptr(ptr).to_str().unwrap() + ) +} #[no_mangle] -pub unsafe extern "C" fn free_rust_string(string: *const c_char) { - let _ = CString::from_raw(string as *mut _); +pub unsafe extern "C" fn free_rust_string(ptr: *const c_char) { + let _ = CString::from_raw(ptr as *mut _); } #[no_mangle] -pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { - let content: &str = CStr::from_ptr(content).to_str().unwrap(); - let content: String = to_json(content); // may return empty string - CString::new(content).unwrap().into_raw() +pub unsafe extern "C" fn to_json_ffi(content: *const c_char) -> *const c_char { + let content = load_c_string(content); + let result = match to_json(&content) { // convert to JSON format + Some(data) => data, + None => String::new(), // convert failed -> empty string + }; + to_c_string(result) // return c-style ptr } From d995aec200dd4177ffd4b5779a65474592bac8fb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 20:27:42 +0800 Subject: [PATCH 04/11] perf: parser function --- src/to-json/src/convert.rs | 6 +++--- src/to-json/src/ffi.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/to-json/src/convert.rs b/src/to-json/src/convert.rs index 72d7dd0..9393fad 100644 --- a/src/to-json/src/convert.rs +++ b/src/to-json/src/convert.rs @@ -2,9 +2,9 @@ use crate::parser::{parser, Value}; fn json_convert(content: &str) -> Result { // convert to JSON format let data = match parser(content)? { - Value::JSON(_json) => serde_json::to_string(&_json), - Value::YAML(_yaml) => serde_json::to_string(&_yaml), - Value::TOML(_toml) => serde_json::to_string(&_toml), + Value::JSON(json) => serde_json::to_string(&json), + Value::YAML(yaml) => serde_json::to_string(&yaml), + Value::TOML(toml) => serde_json::to_string(&toml), }; match data { Ok(data) => Ok(data), diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs index 466bdc4..dcaf66d 100644 --- a/src/to-json/src/ffi.rs +++ b/src/to-json/src/ffi.rs @@ -12,7 +12,7 @@ unsafe fn load_c_string(ptr: *const c_char) -> String { // load string from c-st } #[no_mangle] -pub unsafe extern "C" fn free_rust_string(ptr: *const c_char) { +pub unsafe extern "C" fn free_rust_string(ptr: *const c_char) { // free string memory let _ = CString::from_raw(ptr as *mut _); } From 4113507b16b403bb9a022271a4a3e458c4f2d19d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 20:30:22 +0800 Subject: [PATCH 05/11] remove: legacy to-json code --- src/to-json/src/json.rs | 32 -------------------------------- src/to-json/src/lib.rs | 1 - 2 files changed, 33 deletions(-) delete mode 100644 src/to-json/src/json.rs diff --git a/src/to-json/src/json.rs b/src/to-json/src/json.rs deleted file mode 100644 index 83efc4c..0000000 --- a/src/to-json/src/json.rs +++ /dev/null @@ -1,32 +0,0 @@ -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 ce4d1e6..768c005 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,4 +1,3 @@ mod ffi; -mod json; mod parser; mod convert; From 9f8c484572f9b13b77e5b2a564fcfc699706231a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 20:37:40 +0800 Subject: [PATCH 06/11] test: tests module of to-json --- src/to-json/tests/convert.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/to-json/tests/convert.rs diff --git a/src/to-json/tests/convert.rs b/src/to-json/tests/convert.rs new file mode 100644 index 0000000..59da73e --- /dev/null +++ b/src/to-json/tests/convert.rs @@ -0,0 +1,23 @@ +const JSON_TEST_CONTENT: &str = r#" +{ + "int": 123, + "float": 2.3333, + "string": "dnomd343", + "array": [1, 2, 3, 4, 5], + "dict": { + "test": "demo" + } +} +"#; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn json_to_json() { + println!("{}", JSON_TEST_CONTENT); + assert_eq!(1, 2); + } + +} From 7e19cb1ae5d2d44c729e423e94c09ef29c7f42d7 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 21:42:56 +0800 Subject: [PATCH 07/11] test: complete tests process --- src/to-json/src/lib.rs | 1 + src/to-json/src/tests.rs | 71 ++++++++++++++++++++++++++++++++++++ src/to-json/tests/convert.rs | 23 ------------ 3 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 src/to-json/src/tests.rs delete mode 100644 src/to-json/tests/convert.rs diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 768c005..f04b9c3 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,3 +1,4 @@ mod ffi; +mod tests; mod parser; mod convert; diff --git a/src/to-json/src/tests.rs b/src/to-json/src/tests.rs new file mode 100644 index 0000000..3a63c23 --- /dev/null +++ b/src/to-json/src/tests.rs @@ -0,0 +1,71 @@ +use crate::convert::to_json; + +const JSON_TEST_CONTENT: &str = r#" +{ + "int": 123, + "bool": true, + "float": 3.141592, + "string": "json test", + "array": [1, 2, 3, 4, 5], + "object": { + "sub": "test" + } +} +"#; + +const YAML_TEST_CONTENT: &str = r#" +int: 123 +bool: true +float: 3.141592 +string: "json test" +array: + - 1 + - 2 + - 3 + - 4 + - 5 +object: + sub: test +"#; + +const TOML_TEST_CONTENT: &str = r#" +int = 123 +bool = true +float = 3.141592 +string = "json test" +array = [ 1, 2, 3, 4, 5 ] + +[object] +sub = "test" +"#; + +fn format_json(raw: &str) -> String { + match to_json(raw) { + Some(data) => data, + None => panic!("JSON format error"), + } +} + +#[test] +fn json_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(JSON_TEST_CONTENT).unwrap()), + ); +} + +#[test] +fn yaml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(YAML_TEST_CONTENT).unwrap()), + ); +} + +#[test] +fn toml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(TOML_TEST_CONTENT).unwrap()), + ); +} diff --git a/src/to-json/tests/convert.rs b/src/to-json/tests/convert.rs deleted file mode 100644 index 59da73e..0000000 --- a/src/to-json/tests/convert.rs +++ /dev/null @@ -1,23 +0,0 @@ -const JSON_TEST_CONTENT: &str = r#" -{ - "int": 123, - "float": 2.3333, - "string": "dnomd343", - "array": [1, 2, 3, 4, 5], - "dict": { - "test": "demo" - } -} -"#; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn json_to_json() { - println!("{}", JSON_TEST_CONTENT); - assert_eq!(1, 2); - } - -} From c13a208c2a13d1b927d622fd427d7c0e9506a2b6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 21:47:02 +0800 Subject: [PATCH 08/11] update: tests module --- src/to-json/src/tests.rs | 151 +++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 71 deletions(-) diff --git a/src/to-json/src/tests.rs b/src/to-json/src/tests.rs index 3a63c23..ee74871 100644 --- a/src/to-json/src/tests.rs +++ b/src/to-json/src/tests.rs @@ -1,71 +1,80 @@ -use crate::convert::to_json; - -const JSON_TEST_CONTENT: &str = r#" -{ - "int": 123, - "bool": true, - "float": 3.141592, - "string": "json test", - "array": [1, 2, 3, 4, 5], - "object": { - "sub": "test" - } -} -"#; - -const YAML_TEST_CONTENT: &str = r#" -int: 123 -bool: true -float: 3.141592 -string: "json test" -array: - - 1 - - 2 - - 3 - - 4 - - 5 -object: - sub: test -"#; - -const TOML_TEST_CONTENT: &str = r#" -int = 123 -bool = true -float = 3.141592 -string = "json test" -array = [ 1, 2, 3, 4, 5 ] - -[object] -sub = "test" -"#; - -fn format_json(raw: &str) -> String { - match to_json(raw) { - Some(data) => data, - None => panic!("JSON format error"), - } -} - -#[test] -fn json_to_json() { - assert_eq!( - format_json(JSON_TEST_CONTENT), - format_json(&to_json(JSON_TEST_CONTENT).unwrap()), - ); -} - -#[test] -fn yaml_to_json() { - assert_eq!( - format_json(JSON_TEST_CONTENT), - format_json(&to_json(YAML_TEST_CONTENT).unwrap()), - ); -} - -#[test] -fn toml_to_json() { - assert_eq!( - format_json(JSON_TEST_CONTENT), - format_json(&to_json(TOML_TEST_CONTENT).unwrap()), - ); -} +use crate::convert::to_json; + +#[allow(dead_code)] +const JSON_TEST_CONTENT: &str = r#" +{ + "int": 123, + "bool": true, + "float": 3.141592, + "string": "json test", + "array": [1, 2, 3, 4, 5], + "object": { + "sub": "test" + } +} +"#; + +#[allow(dead_code)] +const YAML_TEST_CONTENT: &str = r#" +int: 123 +bool: true +float: 3.141592 +string: "json test" +array: + - 1 + - 2 + - 3 + - 4 + - 5 +object: + sub: test +"#; + +#[allow(dead_code)] +const TOML_TEST_CONTENT: &str = r#" +int = 123 +bool = true +float = 3.141592 +string = "json test" +array = [ 1, 2, 3, 4, 5 ] + +[object] +sub = "test" +"#; + + +mod tests { + use super::*; + + #[allow(dead_code)] + fn format_json(raw: &str) -> String { + match to_json(raw) { + Some(data) => data, + None => panic!("JSON format error"), + } + } + + #[test] + fn json_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(JSON_TEST_CONTENT).unwrap()), + ); + } + + #[test] + fn yaml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(YAML_TEST_CONTENT).unwrap()), + ); + } + + #[test] + fn toml_to_json() { + assert_eq!( + format_json(JSON_TEST_CONTENT), + format_json(&to_json(TOML_TEST_CONTENT).unwrap()), + ); + } +} From 5222a2541fc11027f760bda699734887d6642cbb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 21:49:39 +0800 Subject: [PATCH 09/11] build: update serde deps --- src/to-json/Cargo.lock | 12 ++++++------ src/to-json/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/to-json/Cargo.lock b/src/to-json/Cargo.lock index 71b6233..7b043cc 100644 --- a/src/to-json/Cargo.lock +++ b/src/to-json/Cargo.lock @@ -16,9 +16,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "indexmap" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown", @@ -38,15 +38,15 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "serde" -version = "1.0.147" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" +checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055" [[package]] name = "serde_json" -version = "1.0.87" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" +checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" dependencies = [ "itoa", "ryu", diff --git a/src/to-json/Cargo.toml b/src/to-json/Cargo.toml index 70e5be9..d4202a8 100644 --- a/src/to-json/Cargo.toml +++ b/src/to-json/Cargo.toml @@ -9,6 +9,6 @@ crate-type = ["staticlib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde_json = "1.0.87" +serde_json = "1.0.89" serde_yaml = "0.9.14" toml = "0.5.9" From f9b41602d56c48541e7a9a811fc07f74c7e20a1b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 6 Dec 2022 21:55:31 +0800 Subject: [PATCH 10/11] update: to_json header file --- include/to_json.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/to_json.h b/include/to_json.h index c93c353..a728d81 100644 --- a/include/to_json.h +++ b/include/to_json.h @@ -7,6 +7,6 @@ #include #include -void free_rust_string(const char *string); +void free_rust_string(const char *ptr); -const char *to_json_rust(const char *content); +const char *to_json_ffi(const char *content); From 2678e06c64b1af4c7262db0fdc7b2093d1bd675d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 8 Dec 2022 15:04:46 +0800 Subject: [PATCH 11/11] fix: update to_json ffi --- src/common/json.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/json.c b/src/common/json.c index 1b303dd..4264824 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -17,8 +17,8 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `. return FALSE; } -char* to_json(const char *content) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) - const char *json_string = to_json_rust(content); // convert to json format +char* to_json(const char *content) { // convert JSON / TOML / YAML to json format (failed -> NULL) + const char *json_string = to_json_ffi(content); // convert to json format char *json_content = strdup(json_string); // load string into owner heap free_rust_string(json_string); // free rust string if (strlen(json_content) == 0) { // empty string -> convert error @@ -30,6 +30,7 @@ char* to_json(const char *content) { // convert JSON / TOML / YAML to json forma return json_content; } + cJSON* json_field_get(cJSON *entry, const char *key) { // fetch key from json map (create when key not exist) cJSON *sub = entry->child; while (sub != NULL) { // traverse all keys