Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for flatten and fix an indexing issue #160

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions derive_macros/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# `sval_derive`
# `sval_derive_macros`

[![Rust](https://github.com/sval-rs/sval/workflows/derive/badge.svg)](https://github.com/sval-rs/sval/actions)
[![Latest version](https://img.shields.io/crates/v/sval.svg)](https://crates.io/crates/sval_derive)
[![Documentation Latest](https://docs.rs/sval_derive/badge.svg)](https://docs.rs/sval_derive)

Automatically derive `sval::Value`.
Automatically derive `sval::Value`. This library is an internal implementation detail of `sval`
and shouldn't be depended on directly.
9 changes: 8 additions & 1 deletion flatten/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
[package]
name = "sval_flatten"
version = "2.6.1"
description = "Value flattening for sval"
authors = ["Ashley Mannix <ashleymannix@live.com.au>"]
edition = "2021"
license = "Apache-2.0 OR MIT"
documentation = "https://docs.rs/sval_flatten"
description = "Value flattening for sval"
repository = "https://github.com/sval-rs/sval"
readme = "README.md"
keywords = ["serialization", "no_std"]
categories = ["encoding", "no-std"]

[features]
alloc = ["sval/alloc", "sval_buffer/alloc"]
Expand Down
1 change: 1 addition & 0 deletions flatten/LICENSE-APACHE
1 change: 1 addition & 0 deletions flatten/LICENSE-MIT
7 changes: 7 additions & 0 deletions flatten/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `sval_flatten`

[![Rust](https://github.com/sval-rs/sval/workflows/flatten/badge.svg)](https://github.com/sval-rs/sval/actions)
[![Latest version](https://img.shields.io/crates/v/sval.svg)](https://crates.io/crates/sval_flatten)
[![Documentation Latest](https://docs.rs/sval_flatten/badge.svg)](https://docs.rs/sval_flatten)

Flatten nested `sval::Value`s onto their parent.
4 changes: 2 additions & 2 deletions flatten/src/flattener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) trait Flatten<'sval> {
}

impl<'sval, S: Flatten<'sval>> Flattener<'sval, S> {
pub(crate) fn begin(stream: S, start_from: usize) -> Self {
pub(crate) fn begin(stream: S, start_from: isize) -> Self {
Flattener {
stream,
state: FlattenerState {
Expand All @@ -54,7 +54,7 @@ impl<'sval, S: Flatten<'sval>> Flattener<'sval, S> {
}
}

pub(crate) fn end(self) -> usize {
pub(crate) fn end(self) -> isize {
self.state.index_alloc.current_offset()
}

Expand Down
17 changes: 9 additions & 8 deletions flatten/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use sval::Index;

pub(crate) struct IndexAllocator {
initial_offset: usize,
current_offset: usize,
initial_offset: isize,
current_offset: isize,
}

impl IndexAllocator {
pub(crate) fn start_from(offset: usize) -> Self {
pub(crate) fn start_from(offset: isize) -> Self {
IndexAllocator {
initial_offset: offset,
current_offset: offset,
Expand All @@ -16,27 +16,28 @@ impl IndexAllocator {
pub(crate) fn next_begin(&mut self, incoming: Option<&Index>) -> Index {
match incoming {
// If there's an incoming tag then merge it into the current set
Some(incoming) => match (incoming.tag(), incoming.to_usize()) {
Some(incoming) => match (incoming.tag(), incoming.to_isize()) {
// If the incoming tag is a value offset then increment it by our starting point
(Some(&sval::tags::VALUE_OFFSET), Some(incoming)) => {
Index::new(incoming + self.initial_offset).with_tag(&sval::tags::VALUE_OFFSET)
Index::new_isize(incoming + self.initial_offset)
.with_tag(&sval::tags::VALUE_OFFSET)
}
// If the incoming tag is not a value offset then just use it directly
_ => incoming.clone(),
},
// If there's no incoming tag then construct one
None => Index::new(self.current_offset).with_tag(&sval::tags::VALUE_OFFSET),
None => Index::new_isize(self.current_offset).with_tag(&sval::tags::VALUE_OFFSET),
}
}

pub(crate) fn next_end(&mut self, incoming: Option<&Index>) -> Index {
let index = self.next_begin(incoming);
self.current_offset += 1;
self.current_offset = index.to_isize().unwrap_or(self.current_offset) + 1;

index
}

pub(crate) fn current_offset(&self) -> usize {
pub(crate) fn current_offset(&self) -> isize {
self.current_offset
}
}
4 changes: 2 additions & 2 deletions flatten/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ with the length of the map after flattening the value.
pub fn flatten_to_map<'sval>(
stream: &mut (impl Stream<'sval> + ?Sized),
value: &'sval (impl sval::Value + ?Sized),
offset: usize,
) -> sval::Result<usize> {
offset: isize,
) -> sval::Result<isize> {
let stream = PassThru::new(stream);

let mut stream = Flattener::begin(MapFlatten { stream }, offset);
Expand Down
4 changes: 2 additions & 2 deletions flatten/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ with the length of the record after flattening the value.
pub fn flatten_to_record<'sval>(
stream: &mut (impl Stream<'sval> + ?Sized),
value: &'sval (impl sval::Value + ?Sized),
offset: usize,
) -> sval::Result<usize> {
offset: isize,
) -> sval::Result<isize> {
let label_stream = LabelBuf::default();

let mut stream = Flattener::begin(
Expand Down
Loading