feat: yaml support

This commit is contained in:
Artemy 2022-08-06 17:18:15 +03:00
parent 1b34caa43b
commit 29113c9ff6
3 changed files with 32 additions and 3 deletions

20
Cargo.lock generated
View file

@ -198,6 +198,7 @@ dependencies = [
"json5",
"serde",
"serde_json",
"serde_yaml",
]
[[package]]
@ -335,6 +336,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79b7c9017c64a49806c6e8df8ef99b92446d09c92457f85f91835b01a8064ae0"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "sha-1"
version = "0.10.0"
@ -416,6 +430,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7"
[[package]]
name = "unsafe-libyaml"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "931179334a56395bcf64ba5e0ff56781381c1a5832178280c7d7f91d1679aeb0"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -9,5 +9,6 @@ edition = "2021"
serde = { version = "1.0", features = ["derive"] }
json5 = "0.4.1"
serde_json = "1.0"
serde_yaml = "0.9"
clap = { version = "3.2", features = ["derive"] }
colored = "2"
colored = "2"

View file

@ -20,8 +20,16 @@ impl Interpreter {
}
pub fn run(&mut self) {
let obj = json5::from_str::<Value>(&self.input).expect("Your json is invalid!");
let arr = obj.as_array().expect("Json must be an array!");
let obj: serde_json::Value = json5::from_str::<Value>(&self.input).unwrap_or_else(|_| {
serde_yaml::from_str(&self.input)
.expect("Your file format is invalid! (supported: json, json5 or yaml)")
});
let arr = obj.as_array().unwrap_or_else(|| {
obj.get("main")
.expect("Each program must contain a `{main: [..commands]}` object or be a command array ([..commands])")
.as_array()
.expect("The program must be an array of commands")
});
for command in arr {
self.eval_node(command);