This code asserts the json data from a discord repo to a function i wrote, to see if results match based on input. How do I pass a function as an argument so i can test the function with just a url and function as arguments?
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
struct Test<A, R> {
args: Vec<A>,
#[serde(rename = "return")]
result: R
}
#[tokio::main]
async fn main() {
run_test().await.unwrap()
}
async fn run_test() -> Result<(), reqwest::Error> {
let tests: Vec<Test<String, f64>> = reqwest::Client::new()
.get("https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/test_cases_622.json")
.send()
.await?
.json()
.await?;
for test in tests.iter() {
assert!(test.result == shortest_distance(&test.args[0]));
println!("{}",test.result)
}
println!("TEST PASSED");
Ok(())
}
fn shortest_distance(points: &str) -> f64 {
let p = points.split(",").map(|point| point.parse::<f64>().unwrap()).collect::<Vec<_>>();
return format!("{:.2}",f64::sqrt((f64::powi((p[2]) - (p[0]), 2)) + (f64::powi((p[3]) - (p[1]), 2) ))).parse().unwrap();
}