Skip to content

Commit

Permalink
feat: add derived schema implementation for ipaddr types
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Oct 8, 2024
1 parent d33b107 commit a39585d
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 8 deletions.
4 changes: 2 additions & 2 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,12 @@ impl BorshDeserialize for std::net::IpAddr {
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let kind = u8::deserialize_reader(reader)?;
match kind {
0 => {
0u8 => {
// Deserialize an Ipv4Addr and convert it to IpAddr::V4
let ipv4_addr = std::net::Ipv4Addr::deserialize_reader(reader)?;
Ok(std::net::IpAddr::V4(ipv4_addr))
}
1 => {
1u8 => {
// Deserialize an Ipv6Addr and convert it to IpAddr::V6
let ipv6_addr = std::net::Ipv6Addr::deserialize_reader(reader)?;
Ok(std::net::IpAddr::V6(ipv6_addr))
Expand Down
63 changes: 63 additions & 0 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,66 @@ impl_tuple!(
impl_tuple!(
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20
);

#[cfg(feature = "std")]
mod id_addr_std_derive_impl {
use crate::BorshSchema as BorshSchemaMacro;

#[derive(BorshSchemaMacro)]
#[borsh(crate = "crate")]
pub struct Ipv4Addr {
octets: [u8; 4],
}

#[derive(BorshSchemaMacro)]
#[borsh(crate = "crate")]
pub struct Ipv6Addr {
octets: [u8; 16],
}

#[derive(BorshSchemaMacro)]
#[borsh(crate = "crate")]
pub enum IpAddr {
/// An IPv4 address.
V4(std::net::Ipv4Addr),
/// An IPv6 address.
V6(std::net::Ipv6Addr),
}
}

#[cfg(feature = "std")]
impl BorshSchema for std::net::Ipv4Addr
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
<id_addr_std_derive_impl::Ipv4Addr>::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
id_addr_std_derive_impl::Ipv4Addr::declaration()
}
}

#[cfg(feature = "std")]
impl BorshSchema for std::net::Ipv6Addr
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
<id_addr_std_derive_impl::Ipv6Addr>::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
id_addr_std_derive_impl::Ipv6Addr::declaration()
}
}


#[cfg(feature = "std")]
impl BorshSchema for std::net::IpAddr
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
<id_addr_std_derive_impl::IpAddr>::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
id_addr_std_derive_impl::IpAddr::declaration()
}
}
4 changes: 2 additions & 2 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ impl BorshSerialize for std::net::IpAddr {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
match self {
std::net::IpAddr::V4(ipv4) => {
writer.write_all(&[0u8])?;
writer.write_all(&0u8.to_le_bytes())?;
ipv4.serialize(writer)
}
std::net::IpAddr::V6(ipv6) => {
writer.write_all(&[0u8])?;
writer.write_all(&1u8.to_le_bytes())?;
ipv6.serialize(writer)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
source: borsh/tests/schema/test_ip_addr.rs
expression: "format!(\"{:#?}\", defs)"
---
{
"IpAddr": Enum {
tag_width: 1,
variants: [
(
0,
"V4",
"IpAddrV4",
),
(
1,
"V6",
"IpAddrV6",
),
],
},
"IpAddrV4": Struct {
fields: UnnamedFields(
[
"Ipv4Addr",
],
),
},
"IpAddrV6": Struct {
fields: UnnamedFields(
[
"Ipv6Addr",
],
),
},
"Ipv4Addr": Struct {
fields: NamedFields(
[
(
"octets",
"[u8; 4]",
),
],
),
},
"Ipv6Addr": Struct {
fields: NamedFields(
[
(
"octets",
"[u8; 16]",
),
],
),
},
"[u8; 16]": Sequence {
length_width: 0,
length_range: 16..=16,
elements: "u8",
},
"[u8; 4]": Sequence {
length_width: 0,
length_range: 4..=4,
elements: "u8",
},
"u8": Primitive(
1,
),
}
5 changes: 3 additions & 2 deletions borsh/tests/schema/test_ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::net::IpAddr;
#[test]
fn ip_addr_schema() {
let actual_name = IpAddr::declaration();
let mut actual_defs = insta::assert_snapshot!(format!("{:#?}", defs));
IpAddr::add_definitions_recursively(&mut actual_defs);
assert_eq!("IpAddr", actual_name);
let mut defs = Default::default();
IpAddr::add_definitions_recursively(&mut defs);
insta::assert_snapshot!(format!("{:#?}", defs));
}
4 changes: 2 additions & 2 deletions borsh/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ mod schema {
mod test_vecs;
mod test_tuple;
mod test_primitives;
#[cfg(feature = "std")]
mod test_ip_addr;
// mod test_nonzero_integers; // NOTE: there's nothing corresponding to `roundtrip::test_nonzero_integers`
mod test_range;
mod test_phantom_data;
Expand All @@ -93,8 +95,6 @@ mod schema {
mod test_cells;
#[cfg(feature = "rc")]
mod test_rc;
#[cfg(feature = "std")]
mod test_ip_addr;
mod test_simple_structs;
mod test_generic_structs;
mod test_simple_enums;
Expand Down

0 comments on commit a39585d

Please sign in to comment.