Skip to content

Commit 4d803ed

Browse files
kriskras99Kriskras99
authored andcommitted
State machine example
1 parent eabc70f commit 4d803ed

24 files changed

+3580
-1480
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

avro/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ categories.workspace = true
2929
documentation.workspace = true
3030

3131
[features]
32+
default = ["futures", "sync"]
3233
bzip = ["dep:bzip2"]
3334
derive = ["dep:apache-avro-derive"]
3435
snappy = ["dep:crc32fast", "dep:snap"]
3536
xz = ["dep:xz2"]
3637
zstandard = ["dep:zstd"]
38+
futures = []
39+
sync = []
3740

3841
[lib]
3942
# disable benchmarks to allow passing criterion arguments to `cargo bench`
@@ -73,6 +76,10 @@ thiserror = { default-features = false, version = "2.0.12" }
7376
uuid = { default-features = false, version = "1.17.0", features = ["serde", "std"] }
7477
xz2 = { default-features = false, version = "0.1.7", optional = true }
7578
zstd = { default-features = false, version = "0.13.3", optional = true }
79+
oval = { version = "2.0.0", features = ["bytes"] }
80+
futures = "0.3.31"
81+
async-stream = "0.3.6"
82+
colored-hexdump = "0.2.2"
7683

7784

7885
[target.'cfg(target_arch = "wasm32")'.dependencies]

avro/src/bigdecimal.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
use crate::{
1919
AvroResult,
20-
decode::{decode_len, decode_long},
2120
encode::{encode_bytes, encode_long},
2221
error::Details,
23-
types::Value,
22+
util::{decode_len_simple, decode_variable},
2423
};
2524
pub use bigdecimal::BigDecimal;
2625
use num_bigint::BigInt;
@@ -47,19 +46,21 @@ pub(crate) fn serialize_big_decimal(decimal: &BigDecimal) -> AvroResult<Vec<u8>>
4746
Ok(final_buffer)
4847
}
4948

50-
pub(crate) fn deserialize_big_decimal(bytes: &Vec<u8>) -> AvroResult<BigDecimal> {
51-
let mut bytes: &[u8] = bytes.as_slice();
52-
let mut big_decimal_buffer = match decode_len(&mut bytes) {
53-
Ok(size) => vec![0u8; size],
49+
pub(crate) fn deserialize_big_decimal(mut bytes: &[u8]) -> AvroResult<BigDecimal> {
50+
let mut big_decimal_buffer = match decode_len_simple(bytes) {
51+
Ok((size, bytes_read)) => {
52+
bytes = &bytes[bytes_read..];
53+
vec![0u8; size]
54+
}
5455
Err(err) => return Err(Details::BigDecimalLen(Box::new(err)).into()),
5556
};
5657

5758
bytes
5859
.read_exact(&mut big_decimal_buffer[..])
5960
.map_err(Details::ReadDouble)?;
6061

61-
match decode_long(&mut bytes) {
62-
Ok(Value::Long(scale_value)) => {
62+
match decode_variable(bytes) {
63+
Ok(Some((scale_value, _))) => {
6364
let big_int: BigInt = BigInt::from_signed_bytes_be(&big_decimal_buffer);
6465
let decimal = BigDecimal::new(big_int, scale_value);
6566
Ok(decimal)
@@ -71,7 +72,11 @@ pub(crate) fn deserialize_big_decimal(bytes: &Vec<u8>) -> AvroResult<BigDecimal>
7172
#[cfg(test)]
7273
mod tests {
7374
use super::*;
74-
use crate::{Codec, Reader, Schema, Writer, error::Error, types::Record};
75+
use crate::{
76+
Codec, Reader, Schema, Writer,
77+
error::Error,
78+
types::{Record, Value},
79+
};
7580
use apache_avro_test_helper::TestResult;
7681
use bigdecimal::{One, Zero};
7782
use pretty_assertions::assert_eq;
@@ -92,7 +97,8 @@ mod tests {
9297
let buffer: Vec<u8> = serialize_big_decimal(&current)?;
9398

9499
let mut as_slice = buffer.as_slice();
95-
decode_long(&mut as_slice)?;
100+
let (_, bytes_read) = decode_variable(as_slice)?.unwrap();
101+
as_slice = &as_slice[bytes_read..];
96102

97103
let mut result: Vec<u8> = Vec::new();
98104
result.extend_from_slice(as_slice);
@@ -109,7 +115,8 @@ mod tests {
109115

110116
let buffer: Vec<u8> = serialize_big_decimal(&BigDecimal::zero())?;
111117
let mut as_slice = buffer.as_slice();
112-
decode_long(&mut as_slice)?;
118+
let (_, bytes_read) = decode_variable(as_slice)?.unwrap();
119+
as_slice = &as_slice[bytes_read..];
113120

114121
let mut result: Vec<u8> = Vec::new();
115122
result.extend_from_slice(as_slice);

0 commit comments

Comments
 (0)