Fixed floats serializing; tests config using Toml
This commit is contained in:
+2
-1
@@ -11,4 +11,5 @@ serde = "1.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
[dev_dependencies]
|
||||
batch_run = "1.2"
|
||||
batch_run = "1.2"
|
||||
toml = "0.5"
|
||||
+12
-12
@@ -55,62 +55,62 @@ impl<W: Write> ser::Serializer for &mut Corona<W> {
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}i8", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}i16", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}i32", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}i64", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i128(self, v: i128) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}i128", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}u8", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}u16", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}u32", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}u64", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u128(self, v: u128) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}u128", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}f32", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> SerResult {
|
||||
write!(self.writer, "{}", v)?;
|
||||
write!(self.writer, "{}f64", v)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,28 @@
|
||||
simple
|
||||
|
||||
Simple
|
||||
|
||||
[simple]
|
||||
types = "Simple"
|
||||
definition = """
|
||||
#[derive(PartialEq, Debug, Serialize)]
|
||||
pub struct Simple {
|
||||
pub integer: i32,
|
||||
pub character: char,
|
||||
pub float: f32,
|
||||
pub string: String,
|
||||
pub boolean: bool,
|
||||
}
|
||||
|
||||
"""
|
||||
value = """
|
||||
definition::Simple {
|
||||
integer: 123,
|
||||
character: 'c',
|
||||
float: std::f32::consts::PI,
|
||||
string: "text string".into(),
|
||||
boolean: false
|
||||
}
|
||||
"""
|
||||
|
||||
nested
|
||||
|
||||
Nested,Inner
|
||||
|
||||
[nested]
|
||||
types = "Nested,Inner"
|
||||
definition = """
|
||||
#[derive(PartialEq, Debug, Serialize)]
|
||||
pub struct Nested {
|
||||
pub inner: Inner,
|
||||
@@ -29,17 +31,17 @@ pub struct Nested {
|
||||
pub struct Inner {
|
||||
pub value: i32,
|
||||
}
|
||||
|
||||
"""
|
||||
value = """
|
||||
definition::Nested {
|
||||
inner: definition::Inner {
|
||||
value: 123
|
||||
}
|
||||
}
|
||||
|
||||
complex
|
||||
|
||||
Complex
|
||||
|
||||
"""
|
||||
[complex]
|
||||
types = "Complex"
|
||||
definition = """
|
||||
use std::collections::HashMap;
|
||||
#[derive(PartialEq, Debug, Serialize)]
|
||||
pub struct Complex {
|
||||
@@ -47,7 +49,8 @@ pub struct Complex {
|
||||
pub vector: Vec<i32>,
|
||||
pub map: HashMap<i128, String>,
|
||||
}
|
||||
|
||||
"""
|
||||
value = """
|
||||
{
|
||||
let mut map = std::collections::HashMap::new();
|
||||
map.insert(0, "test".into());
|
||||
@@ -58,16 +61,17 @@ pub struct Complex {
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
floats
|
||||
|
||||
Floats
|
||||
|
||||
"""
|
||||
[floats]
|
||||
types = "Floats"
|
||||
definition = """
|
||||
#[derive(PartialEq, Debug, Serialize)]
|
||||
pub struct Floats(pub Vec<f32>);
|
||||
|
||||
"""
|
||||
value = """
|
||||
definition::Floats(vec![
|
||||
123456789e30,
|
||||
123456789e-30,
|
||||
12345.6789
|
||||
])
|
||||
])
|
||||
"""
|
||||
+18
-62
@@ -1,30 +1,31 @@
|
||||
use batch_run::Batch;
|
||||
use std::{
|
||||
fs::{create_dir, File},
|
||||
io::BufRead,
|
||||
fs::{create_dir, read_to_string, File},
|
||||
io::Write,
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf}, collections::HashMap,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use toml::from_str;
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Deserialize, Default)]
|
||||
struct Data {
|
||||
name: String,
|
||||
types: String,
|
||||
definition: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
fn write(&mut self, path: &mut PathBuf) {
|
||||
path.push(&self.name);
|
||||
fn write(&self, name: &str, path: impl AsRef<Path>) {
|
||||
let mut path = path.as_ref().to_owned();
|
||||
path.push(name);
|
||||
if !path.exists() {
|
||||
create_dir(&path).unwrap();
|
||||
}
|
||||
path.push("dummy"); // a hack, so that folder isn't overwritten with file name
|
||||
write!(
|
||||
File::create(&path.with_file_name(format!("{}-main.rs", self.name))).unwrap(),
|
||||
File::create(&path.with_file_name(format!("{}-main.rs", name))).unwrap(),
|
||||
include_str!("main.tpl"),
|
||||
name = self.name,
|
||||
name = name,
|
||||
value = self.value
|
||||
)
|
||||
.unwrap();
|
||||
@@ -35,75 +36,30 @@ impl Data {
|
||||
)
|
||||
.unwrap();
|
||||
write!(
|
||||
File::create(&path.with_file_name(format!("{}-user.rs", self.name))).unwrap(),
|
||||
File::create(&path.with_file_name(format!("{}-user.rs", name))).unwrap(),
|
||||
include_str!("user.tpl"),
|
||||
types = self.types,
|
||||
ser_type = self.types.split(",").next().unwrap(),
|
||||
value = self.value
|
||||
).unwrap();
|
||||
write!(
|
||||
File::create(&path.with_file_name(format!("{}-main.snapshot", self.name))).unwrap(),
|
||||
File::create(&path.with_file_name(format!("{}-main.snapshot", name))).unwrap(),
|
||||
include_str!("main.snapshot.tpl"),
|
||||
name = self.name
|
||||
name = name
|
||||
).unwrap();
|
||||
write!(
|
||||
File::create(&path.with_file_name(format!("{}-user.snapshot", self.name))).unwrap(),
|
||||
File::create(&path.with_file_name(format!("{}-user.snapshot", name))).unwrap(),
|
||||
include_str!("user.snapshot.tpl"),
|
||||
).unwrap();
|
||||
path.pop();
|
||||
path.pop();
|
||||
|
||||
self.name.clear();
|
||||
self.types.clear();
|
||||
self.definition.clear();
|
||||
self.value.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum State {
|
||||
Name,
|
||||
Types,
|
||||
Definition,
|
||||
Value,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn forward(&mut self) {
|
||||
use State::*;
|
||||
*self = match self {
|
||||
Name => Self::Types,
|
||||
Types => Self::Definition,
|
||||
Definition => Self::Value,
|
||||
Value => Self::Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let data = File::open("test_fixtures/source.data").unwrap();
|
||||
let mut out = Data::default();
|
||||
let mut state: State = State::Name;
|
||||
let mut path: PathBuf = ["test_fixtures"].iter().collect();
|
||||
for line in std::io::BufReader::new(data).lines() {
|
||||
let line = line.unwrap();
|
||||
if line.trim().is_empty() {
|
||||
state.forward();
|
||||
if state == State::Name {
|
||||
out.write(&mut path);
|
||||
}
|
||||
} else {
|
||||
match state {
|
||||
State::Name => out.name = line.trim().to_string(),
|
||||
State::Types => out.types = line.trim().to_string(),
|
||||
State::Definition => out.definition += &(line + "\n"),
|
||||
State::Value => out.value += &(line + "\n"),
|
||||
}
|
||||
}
|
||||
}
|
||||
// flush anything already here
|
||||
out.write(&mut path);
|
||||
let toml = read_to_string("test_fixtures/data.toml").unwrap();
|
||||
let data: HashMap<String, Data> = from_str(&toml).unwrap();
|
||||
let path: PathBuf = "test_fixtures".into();
|
||||
data.into_iter().for_each(|(key, value)| value.write(&key, &path));
|
||||
|
||||
let b = Batch::new();
|
||||
b.run_match("test_fixtures/**/*-main.rs");
|
||||
|
||||
Reference in New Issue
Block a user