Skip to content

Commit

Permalink
Merge branch 'feat/authz' of https://github.com/pubky/pubky into feat…
Browse files Browse the repository at this point in the history
…/authz
  • Loading branch information
Nuhvi committed Aug 27, 2024
2 parents aaf01cf + ab7c005 commit 2312a30
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pubky-common/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use std::{
sync::Mutex,
};

use serde::{
de::{SeqAccess, Visitor},
ser::SerializeTuple,
Deserialize, Deserializer, Serialize, Serializer,
};

use once_cell::sync::Lazy;
use rand::Rng;

Expand Down Expand Up @@ -200,6 +206,58 @@ pub fn system_time() -> u64 {
* 1000
}

// === Serde ===

impl Serialize for Timestamp {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Convert u64 to 8 bytes in Big-Endian format
let mut tup = serializer.serialize_tuple(8)?;

for byte in self.to_bytes() {
tup.serialize_element(&byte)?;
}

tup.end()
}
}

impl<'de> Deserialize<'de> for Timestamp {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct TimestampVisitor;

impl<'de> Visitor<'de> for TimestampVisitor {
type Value = Timestamp;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a tuple of 8 bytes")
}

fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
where
V: SeqAccess<'de>,
{
let mut bytes = [0u8; 8];

for i in 0..8 {
bytes[i] = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(i, &self))?;
}

Ok(Timestamp::from(bytes))
}
}

deserializer.deserialize_tuple(8, TimestampVisitor)
}
}

#[derive(thiserror::Error, Debug)]
pub enum TimestampError {
#[error("Invalid bytes length, Timestamp should be encoded as 8 bytes, got {0}")]
Expand Down

0 comments on commit 2312a30

Please sign in to comment.