Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/serde ipv6 #195

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ pub mod ipv4 {
}
}

pub mod ipv6 {
use std::net::Ipv6Addr;

use super::*;

option!(
Ipv6Addr,
"Ser/de `Option<Ipv6Addr>` to/from `Nullable(IPv6)`."
);

pub fn serialize<S>(ipv6: &Ipv6Addr, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = ipv6.octets();
let value = u128::from_le_bytes(bytes);
value.serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Ipv6Addr, D::Error>
where
D: Deserializer<'de>,
{
let value: u128 = Deserialize::deserialize(deserializer)?;
let bytes = value.to_le_bytes();
Ok(Ipv6Addr::from(bytes))
}
}

/// Ser/de [`::uuid::Uuid`] to/from `UUID`.
#[cfg(feature = "uuid")]
pub mod uuid {
Expand Down
2 changes: 2 additions & 0 deletions tests/it/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ async fn smoke() {
struct MyRow {
#[serde(with = "clickhouse::serde::ipv4")]
ipv4: Ipv4Addr,
#[serde(with = "clickhouse::serde::ipv6")]
ipv6: Ipv6Addr, // requires no annotations.
#[serde(with = "clickhouse::serde::ipv4::option")]
ipv4_opt: Option<Ipv4Addr>,
#[serde(with = "clickhouse::serde::ipv6::option")]
ipv6_opt: Option<Ipv6Addr>, // requires no annotations.
}

Expand Down
Loading