Skip to content

Commit 7e3b697

Browse files
committed
Disable comments for NewType structs and Unit Variants
See cfrantz#6 Signed-off-by: Chris Frantz <[email protected]>
1 parent fdc273f commit 7e3b697

File tree

4 files changed

+39
-54
lines changed

4 files changed

+39
-54
lines changed

src/json.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,15 @@ impl JsonEmitter {
210210
Document::Null => self.emit_null(w),
211211
Document::Compact(d) => self.emit_compact(w, d),
212212
Document::Fragment(ds) => {
213-
match &ds[..] {
214-
// Currently, an enum unit-variant is the only place in the serializer where a
215-
// Fragment is constructed placing the comment after the node. For this case,
216-
// we want to emit the variant name followed by the comment on the same line.
217-
[n, Document::Comment(c, f)] => {
218-
self.emit_node(w, n)?;
219-
if !self.compact && !self.comment.is_empty() {
220-
write!(w, " ")?;
221-
self.emit_comment(w, c, f)?;
222-
}
223-
}
224-
_ => {
225-
let mut prior_val = false;
226-
for d in ds {
227-
if prior_val {
228-
self.writeln(w, "")?;
229-
self.emit_indent(w)?;
230-
}
231-
self.emit_node(w, d)?;
232-
prior_val = d.has_value();
233-
}
213+
let mut prior_val = false;
214+
for d in ds {
215+
if prior_val {
216+
self.writeln(w, "")?;
217+
self.emit_indent(w)?;
234218
}
235-
};
219+
self.emit_node(w, d)?;
220+
prior_val = d.has_value();
221+
}
236222
Ok(())
237223
}
238224
}

src/ser.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,15 @@ impl<'s, 'a> ser::Serializer for &'s mut AnnotatedSerializer<'a> {
204204
variant: &'static str,
205205
) -> Result<Self::Ok, Self::Error> {
206206
let node = self.serialize_str(variant)?;
207-
if let Some(c) = self.comment(Some(variant), &MemberId::Variant) {
208-
Ok(Document::Fragment(vec![node, c]))
209-
} else {
210-
Ok(node)
211-
}
207+
// TODO(serde-annotate#6): currently, placing a comment on a unit variant results in
208+
// ugly (json) or bad (yaml) documents. For now, omit comments on
209+
// unit variants until we refactor comment emitting.
210+
//if let Some(c) = self.comment(Some(variant), &MemberId::Variant) {
211+
// Ok(Document::Fragment(vec![c, node]))
212+
//} else {
213+
// Ok(node)
214+
//}
215+
Ok(node)
212216
}
213217

214218
fn serialize_newtype_struct<T>(
@@ -221,11 +225,15 @@ impl<'s, 'a> ser::Serializer for &'s mut AnnotatedSerializer<'a> {
221225
{
222226
let field = MemberId::Index(0);
223227
let node = self.serialize(value, self.annotate(None, &field))?;
224-
if let Some(c) = self.comment(None, &field) {
225-
Ok(Document::Fragment(vec![c, node]))
226-
} else {
227-
Ok(node)
228-
}
228+
// TODO(serde-annotate#6): currently, placing a comment on a newtype structs results in
229+
// ugly (json) or bad (yaml) documents. For now, omit comments on
230+
// unit variants until we refactor comment emitting.
231+
//if let Some(c) = self.comment(None, &field) {
232+
// Ok(Document::Fragment(vec![c, node]))
233+
//} else {
234+
// Ok(node)
235+
//}
236+
Ok(node)
229237
}
230238

231239
fn serialize_newtype_variant<T>(

src/yaml.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,15 @@ impl YamlEmitter {
9797
Document::Null => self.emit_null(w),
9898
Document::Compact(d) => self.emit_compact(w, d),
9999
Document::Fragment(ds) => {
100-
match &ds[..] {
101-
[n, Document::Comment(c, f)] => {
102-
self.emit_node(w, n)?;
103-
if !self.compact {
104-
write!(w, " ")?;
105-
self.emit_comment(w, c, f)?;
106-
}
107-
}
108-
_ => {
109-
let mut prior_val = false;
110-
for d in ds {
111-
if prior_val {
112-
self.writeln(w, "")?;
113-
self.emit_indent(w)?;
114-
}
115-
self.emit_node(w, d)?;
116-
prior_val = d.has_value();
117-
}
100+
let mut prior_val = false;
101+
for d in ds {
102+
if prior_val {
103+
self.writeln(w, "")?;
104+
self.emit_indent(w)?;
118105
}
119-
};
106+
self.emit_node(w, d)?;
107+
prior_val = d.has_value();
108+
}
120109
Ok(())
121110
}
122111
}

tests/test_format.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,14 @@ enum NesAddress {
263263
Prg(#[annotate(format=hex)] u8, #[annotate(format=hex)] u16),
264264
#[annotate(format=compact, comment="NES CHR bank:address")]
265265
Chr(#[annotate(format=hex)] u8, #[annotate(format=hex)] u16),
266+
// TODO(serde-annotate#6): Currently, we do not emit comments for unit variants.
266267
#[annotate(comment = "Bad Address")]
267268
Invalid,
268269
}
269270

271+
// TODO(serde-annotate#6): Currently, we do not emit comments for newtype structs.
270272
#[derive(Serialize, Deserialize, Annotate, Debug, PartialEq)]
271-
struct CpuAddress(#[annotate(format=hex)] u16);
273+
struct CpuAddress(#[annotate(format=hex, comment="CPU Address")] u16);
272274

273275
#[derive(Serialize, Deserialize, Debug, PartialEq)]
274276
struct Addresses {
@@ -339,7 +341,7 @@ fn test_nes_addresses() -> Result<()> {
339341
0xFFFC,
340342
0xFFFE
341343
],
342-
inv: "Invalid" // Bad Address
344+
inv: "Invalid"
343345
}"#
344346
);
345347

@@ -362,7 +364,7 @@ fn test_nes_addresses() -> Result<()> {
362364
- 0xFFFA
363365
- 0xFFFC
364366
- 0xFFFE
365-
inv: Invalid # Bad Address"#
367+
inv: Invalid"#
366368
);
367369

368370
Ok(())

0 commit comments

Comments
 (0)