Skip to content

Commit

Permalink
Fix jer integer encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicceboy committed Aug 22, 2024
1 parent 9bbe220 commit 7abefe0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/jer/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
bits::to_vec,
enc::Error,
error::{EncodeError, JerEncodeErrorKind},
types::{fields::Fields, variants, Integer, IntegerType},
types::{fields::Fields, variants, IntegerType},
};

pub struct Encoder {
Expand Down Expand Up @@ -133,15 +133,16 @@ impl crate::Encoder for Encoder {
&mut self,
_t: crate::Tag,
_c: crate::types::Constraints,
value: &Integer<I>,
value: &I,
) -> Result<Self::Ok, Self::Error> {
let as_i64: i64 =
value
.try_into()
.map_err(|_| JerEncodeErrorKind::ExceedsSupportedIntSize {
value: value.to_bigint().unwrap_or_default().into(),
})?;
self.update_root_or_constructed(JsonValue::Number(as_i64.into()))
if let Some(as_i64) = value.to_i64() {
self.update_root_or_constructed(JsonValue::Number(as_i64.into()))
} else {
Err(JerEncodeErrorKind::ExceedsSupportedIntSize {
value: value.to_bigint().unwrap_or_default().into(),
}
.into())
}
}

fn encode_null(&mut self, _: crate::Tag) -> Result<Self::Ok, Self::Error> {
Expand Down
13 changes: 5 additions & 8 deletions src/types/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,14 @@ impl num_traits::CheckedAdd for Integer {
}
}
}
// impl<I: IntegerType> core::ops::Add<I> for Integer {
// type Output = Self;
// fn add(self, rhs: I) -> Self::Output {
// <Self as CheckedAdd>::checked_add(&self, &rhs.into()).unwrap_or_default()
// }
// }

impl core::ops::Add for Integer {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
<Self as CheckedAdd>::checked_add(&self, &rhs).unwrap_or_default()
}
}

macro_rules! impl_ops_integer {
($($t:ty),*) => {
$(
Expand Down Expand Up @@ -284,7 +279,7 @@ impl alloc::fmt::Display for TryFromIntegerError {
self.__description().fmt(f)
}
}
macro_rules! impl_try_into_integer {
macro_rules! impl_try_from_integer {
($($t:ty),*) => {
$(
impl core::convert::TryFrom<Integer> for $t {
Expand All @@ -305,7 +300,7 @@ macro_rules! impl_try_into_integer {
)*
};
}
impl_try_into_integer!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
impl_try_from_integer!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

/// An integer which has encoded constraint range between `START` and `END`.
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -341,8 +336,10 @@ pub trait IntegerType:
+ core::fmt::Debug
+ core::fmt::Display
+ Default
+ TryInto<i64>
+ TryInto<i128>
+ TryInto<isize>
+ TryFrom<i64>
+ TryFrom<i128>
+ TryFrom<isize>
+ TryFrom<BigInt>
Expand Down

0 comments on commit 7abefe0

Please sign in to comment.