Skip to content

Commit

Permalink
chore: Switch to ciborium
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Feb 15, 2025
1 parent cd5022e commit a6480b8
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Upon updating Pueue and restarting the daemon, the previous state will be wiped,
- Revisited, fixed and cleaned up CLI help texts.
- Print most of Pueue's info/log messages to `stderr`. Only keep useful stuff like json and task log output on `stdout`.
- **Breaking**: Ported from `anyhow` to `color_eyre` for prettier log output.
- **Breaking**: Switch `cbor` handling library, potentially breaking backwards-compatible communication on a data format level.

### Add

Expand Down
53 changes: 40 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pueue_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ client = ["dep:color-eyre"]
async-trait = "0.1"
byteorder = "1.5"
chrono.workspace = true
ciborium = { version = "0.2", features = ["std"] }
color-eyre = { workspace = true, optional = true }
dirs = "6.0"
rand = "0.8"
Expand All @@ -34,7 +35,6 @@ rustls = { version = "0.23", features = [
], default-features = false }
rustls-pemfile = "2"
serde.workspace = true
serde_cbor = "0.11"
serde_json.workspace = true
serde_yaml = "0.9"
shellexpand = "3.1"
Expand Down
2 changes: 1 addition & 1 deletion pueue_lib/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use serde_cbor::Value;
use ciborium::Value;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
2 changes: 1 addition & 1 deletion pueue_lib/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! These have additional wrappers for [`Request`](crate::Request) and
//! [`Response`](crate::Response) with [`send_request`] and [`receive_response`].
//!
//! The serialization/deserialization format that's used by `pueue_lib` is [`cbor`](::serde_cbor).
//! The serialization/deserialization format that's used by `pueue_lib` is [`cbor`](::ciborium).
//!
//! ## Protocol
//!
Expand Down
17 changes: 10 additions & 7 deletions pueue_lib/src/network/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::Cursor;

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use ciborium::{from_reader, into_writer};
use serde::{de::DeserializeOwned, Serialize};
use serde_cbor::{de::from_slice, ser::to_vec};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

// Reexport all stream/socket related stuff for convenience purposes
Expand Down Expand Up @@ -49,7 +49,9 @@ where
let message: T = message.into();
debug!("Sending message: {message:#?}",);
// Prepare command for transfer and determine message byte size
let payload = to_vec(&message).map_err(|err| Error::MessageSerialization(err.to_string()))?;
let mut payload = Vec::new();
into_writer(&message, &mut payload)
.map_err(|err| Error::MessageSerialization(err.to_string()))?;

send_bytes(&payload, stream).await
}
Expand Down Expand Up @@ -158,11 +160,11 @@ pub async fn receive_message<T: DeserializeOwned + std::fmt::Debug>(
}

// Deserialize the message.
let message: T = from_slice(&payload_bytes).map_err(|err| {
let message: T = from_reader(payload_bytes.as_slice()).map_err(|err| {
// In the case of an error, try to deserialize it to a generic cbor Value.
// That way we know whether the payload was corrupted or maybe just unexpected due to
// version differences.
if let Ok(value) = from_slice::<serde_cbor::Value>(&payload_bytes) {
if let Ok(value) = from_reader::<ciborium::Value, _>(payload_bytes.as_slice()) {

Check warning on line 167 in pueue_lib/src/network/protocol.rs

View check run for this annotation

Codecov / codecov/patch

pueue_lib/src/network/protocol.rs#L167

Added line #L167 was not covered by tests
Error::UnexpectedPayload(value)
} else {
Error::MessageDeserialization(err.to_string())
Expand Down Expand Up @@ -212,7 +214,8 @@ mod test {
input: payload,
}
.into();
let original_bytes = to_vec(&request).expect("Failed to serialize message.");
let mut original_bytes = Vec::new();
into_writer(&request, &mut original_bytes).expect("Failed to serialize message.");

let listener: GenericListener = Box::new(listener);

Expand All @@ -224,7 +227,7 @@ mod test {
let mut stream = listener.accept().await.unwrap();
let message_bytes = receive_bytes(&mut stream).await.unwrap();

let message: Request = from_slice(&message_bytes).unwrap();
let message: Request = from_reader(message_bytes.as_slice()).unwrap();

send_request(message, &mut stream).await.unwrap();
});
Expand All @@ -234,7 +237,7 @@ mod test {
// Create a client that sends a message and instantly receives it
send_request(request, &mut client).await?;
let response_bytes = receive_bytes(&mut client).await?;
let _message: Request = from_slice(&response_bytes)
let _message: Request = from_reader(response_bytes.as_slice())
.map_err(|err| Error::MessageDeserialization(err.to_string()))?;

assert_eq!(response_bytes, original_bytes);
Expand Down
9 changes: 5 additions & 4 deletions pueue_lib/tests/tls_socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ciborium::{from_reader, into_writer};
use color_eyre::Result;
use pretty_assertions::assert_eq;
use pueue_lib::network::{certificate::create_certificates, message::*, protocol::*};
use serde_cbor::{de::from_slice, ser::to_vec};
use tokio::task;

mod helper;
Expand All @@ -17,7 +17,8 @@ async fn test_tls_socket() -> Result<()> {

let listener = get_listener(&shared_settings).await.unwrap();
let message = Request::Status;
let original_bytes = to_vec(&message).expect("Failed to serialize message.");
let mut original_bytes = Vec::new();
into_writer(&message, &mut original_bytes).expect("Failed to serialize message.");

// Spawn a sub thread that:
// 1. Accepts a new connection
Expand All @@ -27,7 +28,7 @@ async fn test_tls_socket() -> Result<()> {
let mut stream = listener.accept().await.unwrap();
let message_bytes = receive_bytes(&mut stream).await.unwrap();

let message: Request = from_slice(&message_bytes).unwrap();
let message: Request = from_reader(message_bytes.as_slice()).unwrap();

send_request(message, &mut stream).await.unwrap();
});
Expand All @@ -37,7 +38,7 @@ async fn test_tls_socket() -> Result<()> {
// Create a client that sends a message and instantly receives it
send_request(message, &mut client).await.unwrap();
let response_bytes = receive_bytes(&mut client).await.unwrap();
let _message: Request = from_slice(&response_bytes).unwrap();
let _message: Request = from_reader(response_bytes.as_slice()).unwrap();

assert_eq!(response_bytes, original_bytes);

Expand Down
10 changes: 6 additions & 4 deletions pueue_lib/tests/unix_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod helper;

#[cfg(not(target_os = "windows"))]
mod tests {
use ciborium::{from_reader, into_writer};
use color_eyre::Result;
use pretty_assertions::assert_eq;
use pueue_lib::network::{message::*, protocol::*};
use serde_cbor::{de::from_slice, ser::to_vec};
use tokio::task;

use super::*;
Expand All @@ -19,7 +19,9 @@ mod tests {

let listener = get_listener(&shared_settings).await?;
let message = Request::Status;
let original_bytes = to_vec(&message).expect("Failed to serialize message.");

let mut original_bytes = Vec::new();
into_writer(&message, &mut original_bytes).expect("Failed to serialize message.");

// Spawn a sub thread that:
// 1. Accepts a new connection
Expand All @@ -29,7 +31,7 @@ mod tests {
let mut stream = listener.accept().await.unwrap();
let message_bytes = receive_bytes(&mut stream).await.unwrap();

let message: Request = from_slice(&message_bytes).unwrap();
let message: Request = from_reader(message_bytes.as_slice()).unwrap();

send_request(message, &mut stream).await.unwrap();
});
Expand All @@ -39,7 +41,7 @@ mod tests {
// Create a client that sends a message and instantly receives it
send_request(message, &mut client).await?;
let response_bytes = receive_bytes(&mut client).await?;
let _message: Request = from_slice(&response_bytes)?;
let _message: Request = from_reader(response_bytes.as_slice())?;

assert_eq!(response_bytes, original_bytes);

Expand Down

0 comments on commit a6480b8

Please sign in to comment.