Skip to content

Commit

Permalink
add conditional feature for arbitrary-precision serde-json to avoid b…
Browse files Browse the repository at this point in the history
…uild conflicts
  • Loading branch information
lisicky committed Sep 10, 2024
1 parent c60ca35 commit f0e7093
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
5 changes: 3 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ exclude = [
]

[features]
default = []
default = ["arbitrary-precision-json"]
arbitrary-precision-json = ["serde_json/arbitrary_precision"]
#TODO: need to review the features and delete legacy ones. List is defined to avoid warnings.
property-test-api = []
generic-serialization = []
Expand All @@ -32,7 +33,7 @@ bech32 = "0.7.2"
hex = "0.4.0"
cfg-if = "1"
hashlink = "0.9.1"
serde_json = { version = "1.0.114", features = ["arbitrary_precision"] }
serde_json = { version = "1.0.114"}
num-bigint = "0.4.0"
num-integer = "0.1.45"
# The default can't be compiled to wasm, so it's necessary to use either the 'nightly'
Expand Down
35 changes: 32 additions & 3 deletions rust/src/protocol_types/plutus/plutus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use cbor_event::{
};

use schemars::JsonSchema;
use serde_json::Number;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
Expand Down Expand Up @@ -611,13 +610,27 @@ pub fn encode_json_value_to_plutus_datum(
schema: PlutusDatumSchema,
) -> Result<PlutusData, JsError> {
use serde_json::Value;
fn encode_number(x: Number) -> Result<PlutusData, JsError> {

#[cfg(feature = "arbitrary-precision-json")]
fn encode_number(x: serde_json::Number) -> Result<PlutusData, JsError> {
if let Ok(big_int) = BigInt::from_str(x.as_str()) {
Ok(PlutusData::new_integer(&big_int))
} else {
Err(JsError::from_str(&format!("Expected an integer value but got \"{}\"", x)))
}
}

#[cfg(not(feature = "arbitrary-precision-json"))]
fn encode_number(x: serde_json::Number) -> Result<PlutusData, JsError> {
if let Some(x) = x.as_u64() {
Ok(PlutusData::new_integer(&BigInt::from(x)))
} else if let Some(x) = x.as_i64() {
Ok(PlutusData::new_integer(&BigInt::from(x)))
} else {
Err(JsError::from_str("floats not allowed in plutus datums"))
}
}

fn encode_string(
s: &str,
schema: PlutusDatumSchema,
Expand Down Expand Up @@ -829,7 +842,7 @@ pub fn decode_plutus_datum_to_json_value(
},
PlutusDataEnum::Integer(bigint) => (
Some("int"),
Value::Number(Number::from_string_unchecked(bigint.to_str()))
bigint_to_serde_value(bigint)?
),
PlutusDataEnum::Bytes(bytes) => (Some("bytes"), Value::from(match schema {
PlutusDatumSchema::BasicConversions => {
Expand All @@ -851,3 +864,19 @@ pub fn decode_plutus_datum_to_json_value(
Ok(Value::from(wrapper))
}
}

#[cfg(not(feature = "arbitrary-precision-json"))]
fn bigint_to_serde_value(bigint: &BigInt) -> Result<serde_json::Value, JsError> {
bigint
.as_int()
.as_ref()
.map(|int| if int.0 >= 0 { serde_json::Value::from(int.0 as u64) } else {serde_json:: Value::from(int.0 as i64) })
.ok_or_else(|| JsError::from_str(&format!("Integer {} too big for our JSON support", bigint.to_str())))
}

#[cfg(feature = "arbitrary-precision-json")]
fn bigint_to_serde_value(bigint: &BigInt) -> Result<serde_json::Value, JsError> {
use serde_json::Number;
Ok(serde_json::Value::Number(Number::from_string_unchecked(bigint.to_str())))
}

0 comments on commit f0e7093

Please sign in to comment.