Code cleanup & refactoring

This commit is contained in:
2019-09-03 16:05:50 +02:00
parent 8e8ec73395
commit 4578d7ad06
2 changed files with 32 additions and 55 deletions

View File

@@ -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<Shape>, id: String) -> Self {
SpatialObject {
shapes,
properties: Properties {
type_name: "Feature".to_string(),
id,
},
}
}
}
fn store<T>(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<f64>) -> 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<f64>) {
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() {

View File

@@ -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<Shape>, id: String) -> Self {
SpatialObject {
shapes,
properties: Properties {
type_name: "Feature".to_string(),
id,
},
}
}
}
mod json {
use super::*;
pub fn store<T>(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<S, T>(name: S, data: T)
where
S: Into<String>,
T: Serialize,
{
let name = name.into();
/*
// Convert Reference Space definitions
let fn_out = format!("{}.spaces.json", name);
json::store::<Vec<json::Space>>(data, &fn_out);
*/
// Convert Spatial Objects
let fn_out = format!("{}.objects.json", name);
json::store(data, &fn_out);
}