Skip to content

Commit

Permalink
Add to_lazy_value (#293)
Browse files Browse the repository at this point in the history
* add to_lazy_value

* suppress clippy::result_large_err due to breaking change required

* suppress clippy::result_large_err

* cargo fmt

* remove unused unit test
  • Loading branch information
minghuaw authored Oct 29, 2024
1 parent 4518adb commit ead642a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
10 changes: 1 addition & 9 deletions fe2o3-amqp-management/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(missing_docs, missing_debug_implementations)]
#![allow(clippy::result_large_err)] // TODO: refactor in 0.14.0

//! An experimental implementation of AMQP 1.0 management working draft with `fe2o3-amqp`
//!
Expand All @@ -25,12 +26,3 @@ pub const DEFAULT_CLIENT_NODE_ADDRESS: &str = "mgmt-client";
pub use client::MgmtClient;
pub use request::Request;
pub use response::Response;

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
1 change: 1 addition & 0 deletions fe2o3-amqp-ws/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ fn map_amqp_websocket_request(req: impl IntoClientRequest) -> Result<Request, tu
Ok(request)
}

#[allow(clippy::result_large_err)] // TODO: refactor
fn verify_response(response: Response) -> Result<Response, Error> {
use http::StatusCode;

Expand Down
1 change: 1 addition & 0 deletions fe2o3-amqp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(missing_docs, missing_debug_implementations)]
#![warn(clippy::unused_async)]
#![allow(clippy::result_large_err)] // TODO: refactor in 0.14.0

//! A rust implementation of AMQP 1.0 protocol based on serde and tokio.
//!
Expand Down
2 changes: 1 addition & 1 deletion serde_amqp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_amqp"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
description = "A serde implementation of AMQP1.0 protocol."
license = "MIT/Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions serde_amqp/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.13.1

1. Added `to_lazy_value`

## 0.13.0

### Breaking
Expand Down
9 changes: 9 additions & 0 deletions serde_amqp/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ use crate::{
__constants::LAZY_VALUE,
};

/// Serialize a value into a [`LazyValue`].
pub fn to_lazy_value<T>(value: &T) -> Result<LazyValue, Error>
where
T: Serialize,
{
let bytes = crate::to_vec(value)?;
Ok(LazyValue(Bytes::from(bytes)))
}

/// A lazy value.
///
/// This is a thin wrapper around a [`Bytes`] value that can be accessed via
Expand Down
23 changes: 23 additions & 0 deletions serde_amqp/src/size_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ mod tests {
use serde_bytes::ByteBuf;

use crate::{
lazy::to_lazy_value,
primitives::{Array, Dec128, Dec32, Dec64, Symbol, Timestamp, Uuid},
to_vec,
};
Expand Down Expand Up @@ -1311,4 +1312,26 @@ mod tests {
let size_result = serialized_size(&value);
assert!(size_result.is_ok());
}

#[test]
fn serialized_size_of_lazy_value_match_with_value() {
use crate::{described::Described, descriptor::Descriptor, primitives::*, Value};

let timestamp = Timestamp::from_milliseconds(12345);
let mut list = List::new();
list.push(Value::Timestamp(timestamp));

let described = Described {
descriptor: Descriptor::Code(0x73),
value: Value::List(list),
};

let value = Value::Described(Box::new(described));
let lazy_value = to_lazy_value(&value).unwrap();

let value_size = serialized_size(&value).unwrap();
let lazy_value_size = serialized_size(&lazy_value).unwrap();

assert_eq!(value_size, lazy_value_size);
}
}

0 comments on commit ead642a

Please sign in to comment.