diff --git a/Cargo.lock b/Cargo.lock index 0c3adad..2129e4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index c31388b..acb6e48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file +colored = "2" diff --git a/src/interpreter.rs b/src/interpreter.rs index 46e357a..5b9f0d8 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -20,8 +20,16 @@ impl Interpreter { } pub fn run(&mut self) { - let obj = json5::from_str::(&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::(&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);