From 60434be50577b8d91407a3fd903b814f58e4953e Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 7 Feb 2024 15:18:10 +0100 Subject: [PATCH] parsing numbers from strings --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 27031bf..36966d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,15 @@ async fn main() -> Result<(), Box> { 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::() { + Ok(val) => val, + Err(_) => panic!("Conversion error on {}", sth) + }; + } + let system = value["tags"]["system"].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(); @@ -110,7 +118,7 @@ async fn main() -> Result<(), Box> { let write_result = client .query(write_queries) .await; - assert!(write_result.is_ok(), "Write result was not okay"); + assert!(write_result.is_ok(), "{}", write_result.err().unwrap().to_string()); Ok(()) }