-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add wormhole-io * add wormhole-io as dependency --------- Co-authored-by: A5 Pickle <[email protected]>
- Loading branch information
Showing
7 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "wormhole-io" | ||
description = "Traits for Wormhole payload serialization/deserialization" | ||
|
||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
alloy-primitives = { workspace = true, optional = true } | ||
|
||
[features] | ||
alloy = ["dep:alloy-primitives"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod payload; | ||
pub use payload::TypePrefixedPayload; | ||
|
||
mod read_write; | ||
pub use read_write::{Readable, Writeable}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::io; | ||
|
||
use crate::{Readable, Writeable}; | ||
|
||
/// Trait to capture common payload behavior. We do not recommend overwriting | ||
/// any trait methods. Simply set the type constant and implement [`Readable`] | ||
/// and [`Writeable`]. | ||
pub trait TypePrefixedPayload: Readable + Writeable + Clone + std::fmt::Debug { | ||
const TYPE: Option<u8>; | ||
|
||
/// Read the payload, including the type prefix. | ||
fn read_typed<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> { | ||
let payload_type = u8::read(reader)?; | ||
if payload_type == Self::TYPE.expect("Called write_typed on untyped payload") { | ||
Self::read(reader) | ||
} else { | ||
Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"Invalid payload type", | ||
)) | ||
} | ||
} | ||
|
||
/// Write the payload, including the type prefix. | ||
fn write_typed<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
Self::TYPE | ||
.expect("Called write_typed on untyped payload") | ||
.write(writer)?; | ||
Writeable::write(self, writer) | ||
} | ||
|
||
/// Read the payload, including the type prefix if applicable. | ||
fn read_payload<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> { | ||
match Self::TYPE { | ||
Some(_) => Self::read_typed(reader), | ||
None => Readable::read(reader), | ||
} | ||
} | ||
|
||
/// Write the payload, including the type prefix if applicable. | ||
fn write_payload<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
match Self::TYPE { | ||
Some(_) => self.write_typed(writer), | ||
None => Writeable::write(self, writer), | ||
} | ||
} | ||
|
||
/// Returns the size of the payload, including the type prefix. | ||
fn payload_written_size(&self) -> usize { | ||
match Self::TYPE { | ||
Some(_) => self.written_size() + 1, | ||
None => self.written_size(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters