diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..1de6189 --- /dev/null +++ b/build.rs @@ -0,0 +1,32 @@ +use std::{fs::File, path::PathBuf, io::Write}; + +fn main() { + let path: PathBuf = [std::env::var("OUT_DIR").unwrap(), "from_tuple.rs".into()].iter().collect(); + let mut convert = File::create(path).expect("Failed to create file for tuples conversions"); + for i in 1..=32 { + let array = format!("[T; {}]", i); + let tuple = format!("({})", (0..i).map(|_| "T,").collect::()); + let mapping = format!("[{}]", (0..i).map(|index| format!("tuple.{}", index)).collect::>().join(",")); + write!(convert, " + impl FromTuple<{tuple}> for {array} {{ + #[inline] + fn from_tuple(tuple: {tuple}) -> Self {{ + {mapping} + }} + }} + ", array = array, tuple = tuple, mapping = mapping).expect("Failed to write the conversion code"); + let types = (0..i).map(|index| format!("T{}", index)).collect::>().join(","); + write!(convert, " + impl<{types}> FromTuple<({types},)> for ({types},) {{ + #[inline] + fn from_tuple(tuple: ({types},)) -> Self {{ + tuple + }} + }} + #[inline] + pub fn convert_tuple_{count}<{types}, Out: FromTuple<({types},)>>(tuple: ({types},)) -> Out {{ + Out::from_tuple(tuple) + }} + ", types = types, count = i).expect("Failed to write the conversion code"); + } +} \ No newline at end of file diff --git a/src/convert.rs b/src/convert.rs new file mode 100644 index 0000000..528ecce --- /dev/null +++ b/src/convert.rs @@ -0,0 +1,5 @@ +pub trait FromTuple { + fn from_tuple(tuple: T) -> Self; +} + +include!(concat!(env!("OUT_DIR"), "/from_tuple.rs")); \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 7e1ebff..1d1dbb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,4 +36,6 @@ pub mod error; pub mod ser; pub mod funcs; +pub mod convert; + pub use funcs::{to_file, to_out_dir, to_string, write}; \ No newline at end of file diff --git a/src/ser.rs b/src/ser.rs index 735bc65..bd0f6a3 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -201,8 +201,8 @@ impl ser::Serializer for &mut Uneval { Ok(self.start_sub()) } - fn serialize_tuple(self, _len: usize) -> Result { - write!(self.writer, "(")?; + fn serialize_tuple(self, len: usize) -> Result { + write!(self.writer, "::uneval::convert::convert_tuple_{}((", len)?; Ok(self.start_sub()) } @@ -280,7 +280,7 @@ impl ser::SerializeTuple for &mut Uneval { } fn end(self) -> SerResult { - write!(self.writer, ")")?; + write!(self.writer, "))")?; Ok(()) } } @@ -358,7 +358,7 @@ impl ser::SerializeStruct for &mut Uneval { T: serde::Serialize, { self.comma()?; - write!(self.writer, "{}:", key)?; + write!(self.writer, "{}: ", key)?; value.serialize(&mut **self)?; Ok(()) } @@ -381,7 +381,7 @@ impl ser::SerializeStructVariant for &mut Uneval { T: serde::Serialize, { self.comma()?; - write!(self.writer, "{}:", key)?; + write!(self.writer, "{}: ", key)?; value.serialize(&mut **self)?; Ok(()) } diff --git a/test_fixtures/data.toml b/test_fixtures/data.toml index 039413d..7dfdcb9 100644 --- a/test_fixtures/data.toml +++ b/test_fixtures/data.toml @@ -106,4 +106,14 @@ value = """ Enum::Struct{ key: 1, value: "test".into() }, ]) } +""" + +[array] +main_type = "Container" +definition = """ +#[derive(PartialEq, Debug, Serialize)] +pub struct Container(pub [u8; 2]); +""" +value = """ +definition::Container([1, 2]) """ \ No newline at end of file