file read, json parse
This commit is contained in:
parent
7ef189e57a
commit
a1f7e0befb
11
.idea/ArduinoHABridge.iml
generated
Normal file
11
.idea/ArduinoHABridge.iml
generated
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/ArduinoHABridge.iml" filepath="$PROJECT_DIR$/.idea/ArduinoHABridge.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
4
.idea/vcs.xml
generated
4
.idea/vcs.xml
generated
@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -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"
|
||||
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
json = "0.12.4"
|
45
src/main.rs
45
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<String, String> {
|
||||
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 ..."),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user