diff --git a/.gitignore b/.gitignore index 4fffb2f..87b1bec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +*/output.json \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 4d7059e..c1d0ace 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,14 @@ edition = "2021" [dependencies.rand] version = "0.8.5" + +[dependencies.serde] +version = "1.0.200" +features = ["derive"] + +[dependencies.axum] +version = "0.7.5" +features = ["json"] + +[dependencies.serde_json] +version = "1.0.116" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b9c474a..bbb28a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use std::fs::File; +use std::io; +use std::io::Write; use crate::bounds::Bounds; use crate::function::Function; use crate::linear_bounds::LinearBounds; @@ -15,14 +18,14 @@ fn main() { let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64)); let func: Function = Function::new(); - let sample_limit: usize = 1_000; + let sample_limit: usize = 1_000_000_000; let sub_samples: Vec = vec![1, 2, 5]; - let samples_per_iteration: usize = 10; + let samples_per_iteration: usize = 1; let mut result: Plot = Plot::new(); let mut sample_cnt: usize = 1; - while sample_cnt <= sample_limit { + while sample_cnt < sample_limit { for sub_sample in sub_samples.clone() { let samples: usize = sub_sample * sample_cnt; for _ in 0..samples_per_iteration { @@ -32,4 +35,11 @@ fn main() { sample_cnt *= 10; } result.print(); + + let mut file: io::Result = File::create("output.json"); + if file.is_err() { + panic!("Error creating output file") + } + + if let Err(_) = file.unwrap().write(result.to_json().to_string().as_bytes()) { panic!("Error writing to file") }; } diff --git a/src/plot.rs b/src/plot.rs index 82ab5c2..5c67498 100644 --- a/src/plot.rs +++ b/src/plot.rs @@ -1,6 +1,9 @@ use std::iter::zip; +use axum::extract::FromRef; +use serde::Serialize; +use serde_json::{json, Value}; -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct Plot { x: Vec, y: Vec>, @@ -21,9 +24,20 @@ impl Plot { } } - pub fn print(self) -> () { - for (x, y) in zip(self.x, self.y) { + pub fn print(&self) -> () { + let plot: Plot = Plot::from_ref(self); + for (x, y) in zip(plot.x, plot.y) { println!("{}: {:?}", x, y); } } + + pub fn to_json(&self) -> Value { + return json!(self); + } +} + +impl Clone for Plot { + fn clone(&self) -> Self { + return Plot {x: self.x.clone(), y: self.y.clone()} + } } \ No newline at end of file