Skip to content

Commit

Permalink
Merge pull request #171 from sval-rs/fix/flatten-enum-end
Browse files Browse the repository at this point in the history
Fix a bug ending enums in flattened values
  • Loading branch information
KodrAus authored Sep 29, 2023
2 parents 68b9d29 + e1d9130 commit 7f83a76
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
3 changes: 2 additions & 1 deletion flatten/src/flattener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) struct Flattener<'sval, S> {
state: FlattenerState<'sval>,
}

#[derive(Debug)]
struct FlattenerState<'sval> {
index_alloc: IndexAllocator,
key_buf: LabelBuf<'sval>,
Expand Down Expand Up @@ -379,7 +380,7 @@ impl<'sval, S: Flatten<'sval>> Stream<'sval> for Flattener<'sval, S> {
self.value_at_root(
|_, _| Ok(()),
|buf| buf.enum_end(tag, label, index),
|stream| stream.enum_begin(tag, label, index),
|stream| stream.enum_end(tag, label, index),
)
}

Expand Down
1 change: 1 addition & 0 deletions flatten/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sval::Index;

#[derive(Debug)]
pub(crate) struct IndexAllocator {
initial_offset: isize,
current_offset: isize,
Expand Down
1 change: 1 addition & 0 deletions flatten/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'a, 'sval, S: LabelStream<'sval> + ?Sized> LabelStream<'sval> for &'a mut S
}
}

#[derive(Debug)]
pub(crate) enum LabelBuf<'sval> {
Empty,
Text(TextBuf<'sval>),
Expand Down
67 changes: 67 additions & 0 deletions flatten/src/record_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,73 @@ mod tests {
);
}

#[test]
fn flatten_nested_enum() {
#[derive(Value)]
struct Inner {
b: ReallyInner,
}

#[derive(Value)]
struct ReallyInner {
b1: ReallyReallyInner,
}

#[derive(Value)]
#[allow(dead_code)]
enum ReallyReallyInner {
#[sval(label = "b")]
A(i32),
B {
b: i32,
c: i32,
},
C(i32, i32),
}

sval_test::assert_tokens(
&Outer {
a: 1,
i: Inner {
b: ReallyInner {
b1: ReallyReallyInner::B { b: 42, c: 43 },
},
},
d: 4,
},
{
use sval_test::Token::*;

&[
RecordTupleBegin(None, Some(Label::new("Outer")), None, None),
RecordTupleValueBegin(None, Label::new("a"), Index::new(0)),
I32(1),
RecordTupleValueEnd(None, Label::new("a"), Index::new(0)),
RecordTupleValueBegin(None, Label::new("b"), Index::new(1)),
RecordTupleBegin(None, Some(Label::new("ReallyInner")), None, Some(1)),
RecordTupleValueBegin(None, Label::new("b1"), Index::new(0)),
EnumBegin(None, Some(Label::new("ReallyReallyInner")), None),
RecordTupleBegin(None, Some(Label::new("B")), Some(Index::new(1)), Some(2)),
RecordTupleValueBegin(None, Label::new("b"), Index::new(0)),
I32(42),
RecordTupleValueEnd(None, Label::new("b"), Index::new(0)),
RecordTupleValueBegin(None, Label::new("c"), Index::new(1)),
I32(43),
RecordTupleValueEnd(None, Label::new("c"), Index::new(1)),
RecordTupleEnd(None, Some(Label::new("B")), Some(Index::new(1))),
EnumEnd(None, Some(Label::new("ReallyReallyInner")), None),
RecordTupleValueEnd(None, Label::new("b1"), Index::new(0)),
RecordTupleEnd(None, Some(Label::new("ReallyInner")), None),
RecordTupleValueEnd(None, Label::new("b"), Index::new(1)),
RecordTupleValueBegin(None, Label::new("d"), Index::new(2)),
I32(4),
RecordTupleValueEnd(None, Label::new("d"), Index::new(2)),
RecordTupleEnd(None, Some(Label::new("Outer")), None),
]
},
);
}

#[test]
fn flatten_nested() {
#[derive(Value)]
Expand Down
2 changes: 1 addition & 1 deletion src/data/binary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{tags, Result, Stream, Value, std::fmt};
use crate::{std::fmt, tags, Result, Stream, Value};

/**
An adapter that streams a slice of 8bit unsigned integers as binary.
Expand Down
2 changes: 1 addition & 1 deletion src/data/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Result, Stream, Value, std::fmt};
use crate::{std::fmt, Result, Stream, Value};

/**
An adapter that streams a slice of key-value pairs as a map.
Expand Down

0 comments on commit 7f83a76

Please sign in to comment.