Skip to content

Commit

Permalink
feat(exporter): serde: represent u128/i128 as strings
Browse files Browse the repository at this point in the history
See serde-rs/json#625. This patch is highly
inspired by txpipe/oura#712.
  • Loading branch information
Lucas Franceschino committed Jun 29, 2024
1 parent c711b19 commit 6082e09
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
14 changes: 12 additions & 2 deletions frontend/exporter/src/constant_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ use rustc_middle::{mir, ty};
Clone, Debug, Serialize, Deserialize, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord,
)]
pub enum ConstantInt {
Int(i128, IntTy),
Uint(u128, UintTy),
Int(
#[serde(with = "serialize_int::signed")]
#[schemars(with = "String")]
i128,
IntTy,
),
Uint(
#[serde(with = "serialize_int::unsigned")]
#[schemars(with = "String")]
u128,
UintTy,
),
}

#[derive(
Expand Down
7 changes: 6 additions & 1 deletion frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,12 @@ pub enum LitKind {
CStr(Vec<u8>, StrStyle),
Byte(u8),
Char(char),
Int(u128, LitIntType),
Int(
#[serde(with = "serialize_int::unsigned")]
#[schemars(with = "String")]
u128,
LitIntType,
),
Float(Symbol, LitFloatType),
Bool(bool),
Err(ErrorGuaranteed),
Expand Down
1 change: 1 addition & 0 deletions frontend/exporter/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod mir;
mod mir_traits;
mod new;
mod replaced;
pub(crate) mod serialize_int;
mod todo;

pub use copied::*;
Expand Down
91 changes: 91 additions & 0 deletions frontend/exporter/src/types/serialize_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use serde::{de::Visitor, ser::Serialize, Deserializer, Serializer};

pub mod unsigned {
use super::*;
pub fn serialize<S>(value: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value.to_string().serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(IntScalarVisitor)
}

#[derive(Debug)]
struct IntScalarVisitor;
impl<'de> Visitor<'de> for IntScalarVisitor {
type Value = u128;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
dbg!(self);
formatter.write_str("expect to receive integer")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.parse().map_err(serde::de::Error::custom)?)
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v as u128)
}
}
}
pub mod signed {
use super::*;
pub fn serialize<S>(value: &i128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value.to_string().serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<i128, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(IntScalarVisitor)
}

#[derive(Debug)]
struct IntScalarVisitor;
impl<'de> Visitor<'de> for IntScalarVisitor {
type Value = i128;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
dbg!(self);
formatter.write_str("expect to receive integer")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.parse().map_err(serde::de::Error::custom)?)
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v as i128)
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v as i128)
}
}
}

0 comments on commit 6082e09

Please sign in to comment.