diff --git a/src/main.rs b/src/main.rs index 883094a..41c5b1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,42 @@ extern crate serde_derive; mod storage; -use rand::distributions::{Distribution, Uniform}; - +use rand::distributions::Distribution; +use rand::distributions::Uniform; use rand::prelude::ThreadRng; +use std::fs::File; +use std::io::BufWriter; + +use serde::Serialize; + use storage::*; const POSITIONS_PER_SHAPE: usize = 1000; +impl SpatialObject { + pub fn new(shapes: Vec, id: String) -> Self { + SpatialObject { + shapes, + properties: Properties { + type_name: "Feature".to_string(), + id, + }, + } + } +} + +fn store(name: &str, data: T) +where + T: Serialize, +{ + let to = format!("{}.objects.json", name); + let file_out = + File::create(&to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); + let writer = BufWriter::new(&file_out); + + serde_json::to_writer(writer, &data).unwrap(); +} + fn get_point(space_name: &str, rng: &mut ThreadRng, die: &Uniform) -> SpatialObject { let mut shapes = Vec::with_capacity(POSITIONS_PER_SHAPE); @@ -33,7 +62,7 @@ fn get_space(nb_points: usize, rng: &mut ThreadRng, die: &Uniform) { objects.push(get_point(&space_name, rng, &die)); } - storage::store(format!("{}k", nb_points).as_str(), objects); + store(format!("{}k", nb_points).as_str(), objects); } fn main() { diff --git a/src/storage.rs b/src/storage.rs index cd993e4..a950d2c 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,8 +1,3 @@ -use std::fs::File; -use std::io::BufWriter; - -use serde::Serialize; - #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Space { pub name: String, @@ -48,50 +43,3 @@ pub struct Properties { pub type_name: String, pub id: String, } - -impl SpatialObject { - pub fn new(shapes: Vec, id: String) -> Self { - SpatialObject { - shapes, - properties: Properties { - type_name: "Feature".to_string(), - id, - }, - } - } -} - -mod json { - use super::*; - - pub fn store(data: T, to: &str) - where - T: Serialize, - { - let file_out = - File::create(to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); - - // We create a buffered writer from the file we get - let writer = BufWriter::new(&file_out); - - serde_json::to_writer(writer, &data).unwrap(); - } -} - -pub fn store(name: S, data: T) -where - S: Into, - T: Serialize, -{ - let name = name.into(); - /* - // Convert Reference Space definitions - let fn_out = format!("{}.spaces.json", name); - - json::store::>(data, &fn_out); - */ - // Convert Spatial Objects - let fn_out = format!("{}.objects.json", name); - - json::store(data, &fn_out); -}