Merge branch 'Cerber-Ursi:master' into master

This commit is contained in:
Aloke Desai
2021-12-08 15:11:49 -05:00
committed by GitHub
5 changed files with 54 additions and 10 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
[package] [package]
name = "uneval" name = "uneval"
version = "0.2.2" version = "0.2.3"
authors = ["Cerber Ursi <dev+rust@cerbe.ru>"] authors = ["Cerber Ursi <dev+rust@cerbe.ru>"]
edition = "2018" edition = "2021"
description = "Serde serializer to embed data as Rust code" description = "Serde serializer to embed data as Rust code"
license = "MIT" license = "MIT"
readme = "README.md" readme = "README.md"
-1
View File
@@ -32,7 +32,6 @@ or the similar construction using [`lazy_static`](http://crates.io/crates/lazy_s
3. Deserializer isn't implemented. This is intentional, since this crate isn't really intended for runtime usage. Well, in fact, the deserializer *is* implemented - it's just the Rust compiler itself. 3. Deserializer isn't implemented. This is intentional, since this crate isn't really intended for runtime usage. Well, in fact, the deserializer *is* implemented - it's just the Rust compiler itself.
4. This serializer is intended for use with derived implementation. It may return bogus results when used with customized `Serialize`. 4. This serializer is intended for use with derived implementation. It may return bogus results when used with customized `Serialize`.
5. It is impossible to serialize the struct with private fields outside from the module it is defined in. In fact, to be able to serialize this type at all, you'll have to distribute two copies of your crate, one of which would only export the definition with derived `Serialize` to be used by this crate during the build-time of the second copy. (Isn't this a bit too complex?) 5. It is impossible to serialize the struct with private fields outside from the module it is defined in. In fact, to be able to serialize this type at all, you'll have to distribute two copies of your crate, one of which would only export the definition with derived `Serialize` to be used by this crate during the build-time of the second copy. (Isn't this a bit too complex?)
6. It is impossible to use empty tuple structs (i.e. `Empty()`). From the Serde's point of view, they are indistinguishable from unit structs (i.e. `Unit`), but the same Rust syntax can't be used for both, and, since ordinary unit structs are much more common, it was decided to abandon the empty tuples.
If you find any other case where this doesn't work, feel free to open an issue - we'll either fix the code or document the newly-found limitation. If you find any other case where this doesn't work, feel free to open an issue - we'll either fix the code or document the newly-found limitation.
-4
View File
@@ -233,10 +233,6 @@
//! In fact, to be able to use this type with `uneval`, you'll have to distribute two copies of your crate, //! In fact, to be able to use this type with `uneval`, you'll have to distribute two copies of your crate,
//! one of which would only export the definition with derived `Serialize` to be used by serializer //! one of which would only export the definition with derived `Serialize` to be used by serializer
//! during the build-time of the second copy. (Isn't this a bit too complex?) //! during the build-time of the second copy. (Isn't this a bit too complex?)
//! 4. It is impossible to use empty tuple structs (i.e. `Empty()`).
//! From the Serde's point of view, they are indistinguishable from unit structs (i.e. `Unit`),
//! but the same Rust syntax can't be used for both, and, since ordinary unit structs are much
//! more common, it was decided to correctly handle them.
//! //!
//! [include]: https://doc.rust-lang.org/stable/std/macro.include.html //! [include]: https://doc.rust-lang.org/stable/std/macro.include.html
+8 -1
View File
@@ -16,7 +16,7 @@ pub struct Uneval<W: Write> {
} }
impl<W: Write> Uneval<W> { impl<W: Write> Uneval<W> {
pub(crate) fn new(target: W) -> Self { pub fn new(target: W) -> Self {
Self { Self {
writer: target, writer: target,
inside: false, inside: false,
@@ -267,6 +267,7 @@ impl<W: Write> ser::SerializeSeq for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "].into_iter().collect()")?; write!(self.writer, "].into_iter().collect()")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -283,6 +284,7 @@ impl<W: Write> ser::SerializeTuple for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")) }}")?; write!(self.writer, ")) }}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -299,6 +301,7 @@ impl<W: Write> ser::SerializeTupleStruct for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")")?; write!(self.writer, ")")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -315,6 +318,7 @@ impl<W: Write> ser::SerializeTupleVariant for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")")?; write!(self.writer, ")")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -344,6 +348,7 @@ impl<W: Write> ser::SerializeMap for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "].into_iter().collect()")?; write!(self.writer, "].into_iter().collect()")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -368,6 +373,7 @@ impl<W: Write> ser::SerializeStruct for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
self.comma()?; self.comma()?;
write!(self.writer, "}}")?; write!(self.writer, "}}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -391,6 +397,7 @@ impl<W: Write> ser::SerializeStructVariant for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "}}")?; write!(self.writer, "}}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
+43 -1
View File
@@ -93,6 +93,8 @@ pub enum Enum {
Unit(Unit), Unit(Unit),
Tuple(i32, String), Tuple(i32, String),
Struct{ key: i32, value: String }, Struct{ key: i32, value: String },
EmptyTuple(),
EmptyStruct {},
} }
#[derive(PartialEq, Debug, Serialize)] #[derive(PartialEq, Debug, Serialize)]
@@ -102,10 +104,13 @@ value = """
{ {
use definition::*; use definition::*;
Container(vec![ Container(vec![
Enum::Empty,
Enum::Unit(Unit), Enum::Unit(Unit),
Enum::Tuple(1, "test".into()), Enum::Tuple(1, "test".into()),
Enum::Struct{ key: 1, value: "test".into() }, Enum::Struct{ key: 1, value: "test".into() },
Enum::EmptyTuple(),
Enum::EmptyStruct{},
// this is intentionally last, to catch an error at previous step
Enum::Empty,
]) ])
} }
""" """
@@ -129,3 +134,40 @@ pub struct ZeroSizeContainer(pub [u8; 0]);
value = """ value = """
definition::ZeroSizeContainer([]) definition::ZeroSizeContainer([])
""" """
[empty_fields_issue_3]
main_type = "Foo"
support_types = "UnitType,UnitStruct,UnitTuple"
definition = """
use std::collections::HashMap;
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitType;
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitStruct {}
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitTuple();
#[derive(PartialEq, Debug, Serialize)]
pub struct Foo {
pub vec: Vec<String>,
pub map: HashMap<String, String>,
pub unit: (),
pub unit_type: UnitType,
pub unit_struct: UnitStruct,
pub unit_tuple: UnitTuple,
pub last: String
}
"""
value = """
{
use definition::*;
Foo {
vec: vec![],
map: std::collections::HashMap::new(),
unit: (),
unit_type: UnitType,
unit_struct: UnitStruct {},
unit_tuple: UnitTuple(),
last: "".into()
}
}
"""