parsing numbers from strings

This commit is contained in:
Joachim Lusiardi 2024-02-07 15:18:10 +01:00
parent 4a4e254759
commit 60434be505
1 changed files with 10 additions and 2 deletions

View File

@ -79,7 +79,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sth = &sth[segment]; sth = &sth[segment];
} }
} }
let res_value = sth.as_f64().unwrap_or(0.0);
let mut res_value = sth.as_f64().unwrap_or(0.0);
if sth.is_string() {
res_value = match sth.as_str().unwrap().parse::<f64>() {
Ok(val) => val,
Err(_) => panic!("Conversion error on {}", sth)
};
}
let system = value["tags"]["system"].as_str().unwrap().to_string(); let system = value["tags"]["system"].as_str().unwrap().to_string();
let metric = value["tags"]["metric"].as_str().unwrap().to_string(); let metric = value["tags"]["metric"].as_str().unwrap().to_string();
let unit = value["tags"]["unit"].as_str().unwrap().to_string(); let unit = value["tags"]["unit"].as_str().unwrap().to_string();
@ -110,7 +118,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let write_result = client let write_result = client
.query(write_queries) .query(write_queries)
.await; .await;
assert!(write_result.is_ok(), "Write result was not okay"); assert!(write_result.is_ok(), "{}", write_result.err().unwrap().to_string());
Ok(()) Ok(())
} }