Browse Source

feat: json convert interface

dev
Dnomd343 2 years ago
parent
commit
026a460af7
  1. 21
      src/to-json/src/convert.rs
  2. 2
      src/to-json/src/lib.rs

21
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<String, String> { // 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<String> { // to JSON string
match json_convert(content) {
Ok(data) => Some(data),
Err(_) => None,
}
}

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

@ -1,2 +1,4 @@
mod ffi;
mod json;
mod parser;
mod convert;

Loading…
Cancel
Save