diff --git a/src/to-json/src/parser.rs b/src/to-json/src/parser.rs new file mode 100644 index 0000000..1d84d18 --- /dev/null +++ b/src/to-json/src/parser.rs @@ -0,0 +1,43 @@ +use serde_json as json; +use serde_yaml as yaml; + +#[derive(Debug)] +pub enum Value { + JSON(json::Value), + YAML(yaml::Value), + TOML(toml::Value), +} + +fn json_parser(content: &str) -> Option { // parse json content + match json::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +fn yaml_parser(content: &str) -> Option { // parse yaml content + match yaml::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +fn toml_parser(content: &str) -> Option { // parse toml content + match toml::from_str::(content) { + Ok(result) => Some(result), + Err(_) => None, + } +} + +pub fn parser(content: &str) -> Result { + match json_parser(content) { // try JSON format + Some(data) => Ok(Value::JSON(data)), + None => match toml_parser(content) { // try TOML format + Some(data) => Ok(Value::TOML(data)), + None => match yaml_parser(content) { // try YAML format + Some(data) => Ok(Value::YAML(data)), + None => Err(String::from("unknown input format")), + } + } + } +}