mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
4 changed files with 62 additions and 69 deletions
@ -1,104 +1,79 @@ |
|||||
|
use std::fs::{read, read_to_string}; |
||||
|
use std::io::Read; |
||||
use serde_json as json; |
use serde_json as json; |
||||
use serde_yaml as yaml; |
use serde_yaml as yaml; |
||||
|
|
||||
const JSON_STR: &str = r#" |
const FILE_ERROR: i32 = 1; // file open error
|
||||
{ |
const PARSE_ERROR: i32 = 2; // parser error
|
||||
"demo": "key_1", |
|
||||
"author": "dnomd343", |
|
||||
"test": [ |
|
||||
"123", |
|
||||
"234", |
|
||||
"345" |
|
||||
] |
|
||||
} |
|
||||
"#; |
|
||||
|
|
||||
const YAML_STR: &str = r#" |
|
||||
demo: key_1 |
|
||||
author: dnomd343 |
|
||||
test: |
|
||||
- 123 |
|
||||
- 234 |
|
||||
- 345 |
|
||||
"#; |
|
||||
|
|
||||
const TOML_STR: &str = r#" |
|
||||
demo = "key_1" |
|
||||
author = "dnomd343" |
|
||||
test = [ 123, 234, 345 ] |
|
||||
"#; |
|
||||
|
|
||||
#[derive(Debug)] |
|
||||
enum Format { |
enum Format { |
||||
JSON(json::Value), |
JSON(json::Value), |
||||
YAML(yaml::Value), |
YAML(yaml::Value), |
||||
TOML(toml::Value), |
TOML(toml::Value), |
||||
} |
} |
||||
|
|
||||
fn parser(raw: &str) -> Option<Format> { |
fn parser(content: &str) -> Option<Format> { |
||||
if let Ok(data) = json::from_str::<json::Value>(raw) { // try JSON format
|
if let Ok(data) = json::from_str::<json::Value>(content) { // try JSON format
|
||||
return Some(Format::JSON(data)); |
return Some(Format::JSON(data)); |
||||
} |
} |
||||
if let Ok(data) = toml::from_str::<toml::Value>(raw) { // try TOML format
|
if let Ok(data) = toml::from_str::<toml::Value>(content) { // try TOML format
|
||||
return Some(Format::TOML(data)); |
return Some(Format::TOML(data)); |
||||
} |
} |
||||
if let Ok(data) = yaml::from_str::<yaml::Value>(raw) { // try YAML format
|
if let Ok(data) = yaml::from_str::<yaml::Value>(content) { // try YAML format
|
||||
return Some(Format::YAML(data)); |
return Some(Format::YAML(data)); |
||||
} |
} |
||||
return None; |
return None; // parse failed
|
||||
} |
} |
||||
|
|
||||
fn to_json(raw: &str) -> String { |
fn to_json(content: &str) -> String { // convert to JSON format
|
||||
match parser(raw) { |
match parser(content) { |
||||
Some(data) => match data { |
Some(data) => match data { |
||||
Format::JSON(dat) => json::to_string(&dat).unwrap(), |
Format::JSON(dat) => json::to_string(&dat).unwrap(), |
||||
Format::YAML(dat) => json::to_string(&dat).unwrap(), |
Format::YAML(dat) => json::to_string(&dat).unwrap(), |
||||
Format::TOML(dat) => json::to_string(&dat).unwrap(), |
Format::TOML(dat) => json::to_string(&dat).unwrap(), |
||||
}, |
}, |
||||
None => std::process::exit(1), |
None => std::process::exit(PARSE_ERROR), // error exit
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn read_file(path: &str) -> String { // read file content
|
||||
|
match std::fs::File::open(path) { |
||||
|
Ok(mut file) => { // file open success
|
||||
|
let mut content = String::new(); |
||||
|
file.read_to_string(&mut content).unwrap(); // get file content
|
||||
|
content |
||||
|
}, |
||||
|
Err(_) => std::process::exit(FILE_ERROR), // read failed
|
||||
} |
} |
||||
} |
} |
||||
|
|
||||
fn main() { |
fn main() { |
||||
println!("---------------------------------"); |
|
||||
println!("JSON raw content:\n{}", JSON_STR); |
|
||||
println!("---------------------------------"); |
|
||||
println!("YAML raw content:\n{}", YAML_STR); |
|
||||
println!("---------------------------------"); |
|
||||
println!("TOML raw content:\n{}", TOML_STR); |
|
||||
println!("---------------------------------"); |
|
||||
|
|
||||
let temp = to_json(JSON_STR); |
let file_name = "test.json_"; |
||||
println!("{}", temp); |
|
||||
|
|
||||
// let a = String::from("ok");
|
let content = read_file(file_name); |
||||
// match parser(JSON_STR) {
|
|
||||
// Some(data) => {
|
|
||||
// // println!("{:#?}", data);
|
|
||||
// match data {
|
|
||||
// Format::JSON(dat) => {
|
|
||||
// return json::to_string(&data).unwrap()
|
|
||||
// },
|
|
||||
// _ => println!("123"),
|
|
||||
// // Format::JSON(dat) => println!("{:#?}", dat),
|
|
||||
// // Format::YAML(dat) => println!("{:#?}", dat),
|
|
||||
// // Format::TOML(dat) => println!("{:#?}", dat),
|
|
||||
// }
|
|
||||
// // let temp = data;
|
|
||||
// // let ret = json::to_string(&data).unwrap();
|
|
||||
// // println!("JSON output ->\n{}", ret);
|
|
||||
// },
|
|
||||
// None => println!("parser error"),
|
|
||||
// }
|
|
||||
|
|
||||
// let data = parser(JSON_STR);
|
println!("{}", content); |
||||
// println!("{:#?}", data);
|
|
||||
|
|
||||
// decode(YAML_STR);
|
// match std::fs::File::open(file_name) {
|
||||
// decode(TOML_STR);
|
// Ok(file) => {
|
||||
|
// read_to_string()
|
||||
|
// },
|
||||
|
// Err(_) => std::process::exit(FILE_ERROR),
|
||||
|
// }
|
||||
|
|
||||
|
// let mut file = std::fs::File::open(file_name).unwrap();
|
||||
|
// let mut contents = String::new();
|
||||
|
// file.read_to_string(&mut contents).unwrap();
|
||||
|
// println!("{}", contents);
|
||||
|
|
||||
|
// match std::fs::File::open(file_name) {
|
||||
|
// Ok(mut file) => {
|
||||
|
// let mut contents = String::new();
|
||||
|
// file.read_to_string(&mut contents).unwrap();
|
||||
|
// println!("{}", contents);
|
||||
|
// },
|
||||
|
// Err(_) => std::process::exit(FILE_ERROR),
|
||||
|
// }
|
||||
|
|
||||
// let ret = serde_json::to_string(&data).unwrap();
|
|
||||
// println!("JSON output ->\n{}", ret);
|
|
||||
} |
} |
||||
|
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"demo": "key_1", |
||||
|
"author": "dnomd343", |
||||
|
"test": [ |
||||
|
123, |
||||
|
234, |
||||
|
345 |
||||
|
] |
||||
|
} |
@ -0,0 +1,3 @@ |
|||||
|
demo = "key_1" |
||||
|
author = "dnomd343" |
||||
|
test = [ 123, 234, 345 ] |
@ -0,0 +1,6 @@ |
|||||
|
demo: key_1 |
||||
|
author: dnomd343 |
||||
|
test: |
||||
|
- 123 |
||||
|
- 234 |
||||
|
- 345 |
Loading…
Reference in new issue