Skip to content

Commit

Permalink
Fix js build (#158)
Browse files Browse the repository at this point in the history
* Fix js build

* Error conversion fix
  • Loading branch information
gostkin committed Dec 9, 2022
1 parent f5f49b1 commit 690951c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
22 changes: 8 additions & 14 deletions rust/src/json_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ pub enum Value {
Object(BTreeMap<String, Value>),
}

impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.serialize_str(self.clone().to_string().map_err(|err| std::fmt::Error::custom(format!("Can't serialize {:?}", err)))?.as_str())
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
enum JsonToken {
ArrayStart,
Expand Down Expand Up @@ -81,7 +75,7 @@ impl JsonToken {

impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let string = self.clone().to_string().map_err(|err| serde::ser::Error::custom(format!("serialize: {:?}", err)))?;
let string = self.clone().to_string().map_err(|err| serde::ser::Error::custom(&format!("{:?}", err)))?;

serializer.serialize_str(string.as_str())
}
Expand All @@ -90,7 +84,7 @@ impl Serialize for Value {
impl<'de> Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
Value::from_string(s).map_err(|err| serde::de::Error::custom(format!("Can't deserialize json string: {}", err)))
Value::from_string(s).map_err(|err| serde::de::Error::custom(&format!("{:?}", err)))
}
}

Expand Down Expand Up @@ -523,16 +517,16 @@ impl Value {
pub fn to_string(self) -> Result<String, JsError> {
let result = match self {
Value::Null => {
serde_json::to_string(&serde_json::Value::Null).map_err(|err| JsError::from_str(format!("Can't convert null to string: {:?}", err).as_str()))?
serde_json::to_string(&serde_json::Value::Null).map_err(|err| JsError::from_str(&format!("Can't convert null to string: {:?}", err)))?
}
Value::Bool(b) => {
serde_json::to_string(&serde_json::Value::Bool(b)).map_err(|err| JsError::from_str(format!("Can't convert bool to string: {:?}", err).as_str()))?
serde_json::to_string(&serde_json::Value::Bool(b)).map_err(|err| JsError::from_str(&format!("Can't convert bool to string: {:?}", err)))?
}
Value::Number(bigint) => {
bigint.to_str()
}
Value::String(str) => {
serde_json::to_string(&serde_json::Value::String(str)).map_err(|err| JsError::from_str(format!("Can't convert string to string: {:?}", err).as_str()))?
serde_json::to_string(&serde_json::Value::String(str)).map_err(|err| JsError::from_str(&format!("Can't convert string to string: {:?}", err)))?
}
Value::Array(arr) => {
let mut arr_serialized = vec![String::new(); arr.len()];
Expand All @@ -554,7 +548,7 @@ impl Value {

pub fn from_string(from: String) -> Result<Self, JsError> {
let tokens = tokenize_string(from);
parse_json(tokens).map_err(|err| JsError::from_str(format!("Can't parse json: {}", err).as_str()))
parse_json(tokens).map_err(|err| JsError::from_str(&format!("Can't parse json: {}", err)))
}
}

Expand Down Expand Up @@ -866,8 +860,8 @@ mod tests {

let count = cases.len();
for (number, (test, tokens, parsed)) in cases.into_iter().enumerate() {
test_string += format!("\"{}\":", number).as_str();
test_string += test.as_str();
test_string += &format!("\"{}\":", number);
test_string += &test;
correct_tokens.extend(vec![JsonToken::Quote, JsonToken::String { raw: number.to_string() }, JsonToken::Quote, JsonToken::Colon]);
correct_tokens.extend(tokens);
correct_value.insert(number.to_string(), parsed);
Expand Down
4 changes: 2 additions & 2 deletions rust/src/plutus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,15 +1242,15 @@ pub fn encode_json_value_to_plutus_datum(value: json_serialize::Value, schema: P
Ok(PlutusData::new_constr_plutus_data(&ConstrPlutusData::new(&variant, &fields)))
}
},
_ => Err(JsError::from_str(&format!("DetailedSchema requires ALL JSON to be tagged objects, found: {}", value))),
_ => Err(JsError::from_str(&format!("DetailedSchema requires ALL JSON to be tagged objects, found: {:?}", value))),
},
}
}

#[wasm_bindgen]
pub fn decode_plutus_datum_to_json_str(datum: &PlutusData, schema: PlutusDatumSchema) -> Result<String, JsError> {
let value = decode_plutus_datum_to_json_value(datum, schema)?;
value.to_string().map_err(|e| JsError::from_str(&e.to_string()))
value.to_string()
}

pub fn decode_plutus_datum_to_json_value(datum: &PlutusData, schema: PlutusDatumSchema) -> Result<json_serialize::Value, JsError> {
Expand Down

0 comments on commit 690951c

Please sign in to comment.