diff --git a/.idea/ArduinoHABridge.iml b/.idea/ArduinoHABridge.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/ArduinoHABridge.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..af00acb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d843f34..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 86d6563..ee3af52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,12 @@ version = 4 [[package]] name = "ArduinoHABridge" version = "0.1.0" +dependencies = [ + "json", +] + +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" diff --git a/Cargo.toml b/Cargo.toml index 3adb404..3867779 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +json = "0.12.4" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..4da1f12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,46 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::{ErrorKind, Read}; +use std::path::Path; +use std::process::exit; + fn main() { - println!("Hello, world!"); + let data_file = Path::new("data.json"); + get_json(data_file); + } + +fn get_json(path: &Path) -> HashMap { + let mut data = HashMap::new(); + let json_data = read_file(path); + + // fixme + let parsed_data = json::parse(json_data.as_str()).unwrap(); + println!("{}", parsed_data); + + data +} + +fn read_file(path: &Path) -> String { + let read_result = File::open(path); + let mut result= String::new(); + + match read_result { + Ok(mut t) => { + // file exists + println!("[INFO] JSON file located, reading ..."); + match t.read_to_string(&mut result) { + // handling the reading + Ok(_r) => result, + Err(e) => panic!("[ERROR] While reading the file: {e}"), + } + }, + Err(err) => match err.kind() { + ErrorKind::NotFound => { + println!("[ERROR] {} does not exist", path.display()); + exit(1); + }, + _ => panic!("[ERROR] Unexpected error, closing ..."), + } + } +} \ No newline at end of file