mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
17 changed files with 98 additions and 101 deletions
@ -0,0 +1,12 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
/* Generated with cbindgen:0.23.0 */ |
||||
|
|
||||
|
#include <stdarg.h> |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
#include <stdlib.h> |
||||
|
|
||||
|
void free_rust_string(const char *string); |
||||
|
|
||||
|
const char *to_json_rust(const char *content); |
@ -1,3 +1,4 @@ |
|||||
cmake_minimum_required(VERSION 2.8.12) |
cmake_minimum_required(VERSION 2.8.12) |
||||
|
|
||||
add_library(common json.c sundry.c system.c structure.c) |
add_library(common json.c sundry.c system.c structure.c) |
||||
|
target_link_libraries(common to_json) |
||||
|
@ -0,0 +1,3 @@ |
|||||
|
language = "C" |
||||
|
pragma_once = true |
||||
|
include_version = true |
@ -0,0 +1,14 @@ |
|||||
|
use std::ffi::{c_char, CStr, CString}; |
||||
|
use crate::json::to_json; |
||||
|
|
||||
|
#[no_mangle] |
||||
|
pub unsafe extern "C" fn free_rust_string(string: *const c_char) { |
||||
|
let _ = CString::from_raw(string 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() |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
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<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
|
||||
|
} |
||||
|
|
||||
|
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
|
||||
|
} |
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
mod ffi; |
||||
|
mod json; |
@ -1,57 +0,0 @@ |
|||||
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
|
|
||||
} |
|
Loading…
Reference in new issue