curl setup

This commit is contained in:
2025-01-15 15:39:52 +01:00
parent a1f7e0befb
commit 6ca98da28b
5 changed files with 268 additions and 40 deletions

View File

@@ -1,46 +1,13 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{ErrorKind, Read};
use std::path::Path;
use std::process::exit;
mod json;
mod curl;
fn main() {
let data_file = Path::new("data.json");
get_json(data_file);
let data = json::get_json(data_file);
// some loop for getting data from arduino
// send curl request
let res = curl::curl_request(data);
}
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 ..."),
}
}
}