mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
10 changed files with 191 additions and 84 deletions
@ -1,6 +1,7 @@ |
|||
/bin/ |
|||
/build/ |
|||
**/.idea/ |
|||
/.idea/ |
|||
/assets/*.txt |
|||
/to-json/target/ |
|||
/cmake-build-debug/ |
|||
/cmake-build-release/ |
|||
|
@ -0,0 +1,91 @@ |
|||
# This file is automatically @generated by Cargo. |
|||
# It is not intended for manual editing. |
|||
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]] |
|||
name = "to-json" |
|||
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" |
@ -0,0 +1,11 @@ |
|||
[package] |
|||
name = "to-json" |
|||
version = "0.1.0" |
|||
edition = "2021" |
|||
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
|||
|
|||
[dependencies] |
|||
serde_json = "1.0.86" |
|||
serde_yaml = "0.9.14" |
|||
toml = "0.5.9" |
@ -0,0 +1,57 @@ |
|||
use std::io::Read; |
|||
use serde_json as json; |
|||
use serde_yaml as yaml; |
|||
|
|||
const FILE_ERROR: i32 = 1; // file open error
|
|||
const PARSE_ERROR: i32 = 2; // parser error
|
|||
|
|||
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
|
|||
} |
|||
|
|||
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(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() { |
|||
let args: Vec<String> = std::env::args().collect(); // input arguments
|
|||
if args.len() < 2 { // missing arguments
|
|||
println!("usage: toJSON [file]"); |
|||
std::process::exit(0); |
|||
} |
|||
let content = read_file(&args[1].clone()[..]); // read file content
|
|||
println!("{}", to_json(&content[..])); // convert to JSON format
|
|||
} |
@ -1,8 +0,0 @@ |
|||
module toJSON |
|||
|
|||
go 1.18 |
|||
|
|||
require ( |
|||
github.com/BurntSushi/toml v1.2.0 |
|||
gopkg.in/yaml.v3 v3.0.1 |
|||
) |
@ -1,6 +0,0 @@ |
|||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= |
|||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
@ -1,40 +0,0 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"github.com/BurntSushi/toml" |
|||
"gopkg.in/yaml.v3" |
|||
"os" |
|||
) |
|||
|
|||
func parser(raw []byte) (interface{}, interface{}) { |
|||
var object interface{} |
|||
if err := json.Unmarshal(raw, &object); err == nil { // try json
|
|||
return object, nil // json format
|
|||
} |
|||
if err := toml.Unmarshal(raw, &object); err == nil { // try toml
|
|||
return object, nil // toml format
|
|||
} |
|||
if err := yaml.Unmarshal(raw, &object); err == nil { // try yaml
|
|||
return object, nil // yaml format
|
|||
} |
|||
return nil, nil // parser error
|
|||
} |
|||
|
|||
func main() { |
|||
if len(os.Args) < 2 { // without argument
|
|||
fmt.Printf("usage: toJSON [file]\n") |
|||
os.Exit(0) |
|||
} |
|||
raw, err := os.ReadFile(os.Args[1]) |
|||
if err != nil { |
|||
os.Exit(2) // file open failed
|
|||
} |
|||
if object, err := parser(raw); err == nil { |
|||
ret, _ := json.Marshal(object) |
|||
fmt.Println(string(ret)) |
|||
os.Exit(0) |
|||
} |
|||
os.Exit(1) // unmarshal failed
|
|||
} |
Loading…
Reference in new issue