From 1ec94a51b240b734173b8223017d3ab82ab60737 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 22:57:16 +0800 Subject: [PATCH] feat: read target file content --- to-json/src/main.rs | 113 +++++++++++++++++--------------------------- to-json/test.json | 9 ++++ to-json/test.toml | 3 ++ to-json/test.yaml | 6 +++ 4 files changed, 62 insertions(+), 69 deletions(-) create mode 100644 to-json/test.json create mode 100644 to-json/test.toml create mode 100644 to-json/test.yaml diff --git a/to-json/src/main.rs b/to-json/src/main.rs index c6a2e85..4f6e808 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,104 +1,79 @@ +use std::fs::{read, read_to_string}; +use std::io::Read; use serde_json as json; use serde_yaml as yaml; -const JSON_STR: &str = r#" -{ - "demo": "key_1", - "author": "dnomd343", - "test": [ - "123", - "234", - "345" - ] -} -"#; - -const YAML_STR: &str = r#" -demo: key_1 -author: dnomd343 -test: - - 123 - - 234 - - 345 -"#; +const FILE_ERROR: i32 = 1; // file open error +const PARSE_ERROR: i32 = 2; // parser error -const TOML_STR: &str = r#" -demo = "key_1" -author = "dnomd343" -test = [ 123, 234, 345 ] -"#; - -#[derive(Debug)] enum Format { JSON(json::Value), YAML(yaml::Value), TOML(toml::Value), } -fn parser(raw: &str) -> Option { - if let Ok(data) = json::from_str::(raw) { // try JSON format +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::(raw) { // try TOML format + if let Ok(data) = toml::from_str::(content) { // try TOML format return Some(Format::TOML(data)); } - if let Ok(data) = yaml::from_str::(raw) { // try YAML format + if let Ok(data) = yaml::from_str::(content) { // try YAML format return Some(Format::YAML(data)); } - return None; + return None; // parse failed } -fn to_json(raw: &str) -> String { - match parser(raw) { +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 => 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() { - 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); - println!("{}", temp); + let file_name = "test.json_"; - // let a = String::from("ok"); - // 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 content = read_file(file_name); - // let data = parser(JSON_STR); - // println!("{:#?}", data); + println!("{}", content); - // decode(YAML_STR); - // decode(TOML_STR); + // match std::fs::File::open(file_name) { + // 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); } diff --git a/to-json/test.json b/to-json/test.json new file mode 100644 index 0000000..0873928 --- /dev/null +++ b/to-json/test.json @@ -0,0 +1,9 @@ +{ + "demo": "key_1", + "author": "dnomd343", + "test": [ + 123, + 234, + 345 + ] +} diff --git a/to-json/test.toml b/to-json/test.toml new file mode 100644 index 0000000..5e929d6 --- /dev/null +++ b/to-json/test.toml @@ -0,0 +1,3 @@ +demo = "key_1" +author = "dnomd343" +test = [ 123, 234, 345 ] diff --git a/to-json/test.yaml b/to-json/test.yaml new file mode 100644 index 0000000..09fa3e6 --- /dev/null +++ b/to-json/test.yaml @@ -0,0 +1,6 @@ +demo: key_1 +author: dnomd343 +test: + - 123 + - 234 + - 345