sdk/rust: Drop references to `de::Unexpected`

The `de::Unexpected` enum from serde has a `Float(f64)` variant.
Referencing this enum anywhere in the code will cause the compiler to
emit its `fmt::Display` impl, which includes an `f64.load` instruction
on wasm targets.  Even if this instruction is never executed, its mere
existence will cause cosmos chains to reject any cosmwasm contract that
has it.

Fix this by removing all references to `de::Unexpected`.
This commit is contained in:
Chirantan Ekbote 2022-12-09 16:35:21 +09:00 committed by Chirantan Ekbote
parent d157c7bf67
commit b3d68b0db6
4 changed files with 27 additions and 28 deletions

View File

@ -41,7 +41,7 @@ mod governance_packet_impl {
use std::fmt;
use serde::{
de::{Error, MapAccess, SeqAccess, Unexpected, Visitor},
de::{Error, MapAccess, SeqAccess, Visitor},
ser::SerializeStruct,
Deserialize, Deserializer, Serialize, Serializer,
};
@ -79,8 +79,9 @@ mod governance_packet_impl {
if arr == MODULE {
Ok(Module)
} else {
let expected = format!("{MODULE:?}");
Err(Error::invalid_value(Unexpected::Bytes(&arr), &&*expected))
Err(Error::custom(
"invalid governance module, expected \"Core\"",
))
}
}
}
@ -215,10 +216,9 @@ mod governance_packet_impl {
Action::TransferFee { amount, recipient }
}
v => {
return Err(Error::invalid_value(
Unexpected::Unsigned(v.into()),
&"one of 1, 2, 3, 4",
))
return Err(Error::custom(format_args!(
"invaliid value {v}, expected one of 1, 2, 3, 4"
)))
}
};

View File

@ -84,7 +84,7 @@ mod governance_packet_impl {
use std::fmt;
use serde::{
de::{Error, MapAccess, SeqAccess, Unexpected, Visitor},
de::{Error, MapAccess, SeqAccess, Visitor},
ser::SerializeStruct,
Deserialize, Deserializer, Serialize, Serializer,
};
@ -122,8 +122,9 @@ mod governance_packet_impl {
if arr == MODULE {
Ok(Module)
} else {
let expected = format!("{MODULE:?}");
Err(Error::invalid_value(Unexpected::Bytes(&arr), &&*expected))
Err(Error::custom(
"invalid governance module, expected \"NFTBridge\"",
))
}
}
}
@ -223,10 +224,9 @@ mod governance_packet_impl {
Action::ContractUpgrade { new_contract }
}
v => {
return Err(Error::invalid_value(
Unexpected::Unsigned(v.into()),
&"one of 1, 2",
))
return Err(Error::custom(format_args!(
"invalid value {v}, expected one of 1, 2"
)))
}
};

View File

@ -209,7 +209,7 @@ mod governance_packet_impl {
use std::fmt;
use serde::{
de::{Error, MapAccess, SeqAccess, Unexpected, Visitor},
de::{Error, MapAccess, SeqAccess, Visitor},
ser::SerializeStruct,
Deserialize, Deserializer, Serialize, Serializer,
};
@ -247,8 +247,9 @@ mod governance_packet_impl {
if arr == MODULE {
Ok(Module)
} else {
let expected = format!("{MODULE:?}");
Err(Error::invalid_value(Unexpected::Bytes(&arr), &&*expected))
Err(Error::custom(
"invalid governance module, expected \"TokenBridge\"",
))
}
}
}
@ -348,10 +349,9 @@ mod governance_packet_impl {
Action::ContractUpgrade { new_contract }
}
v => {
return Err(Error::invalid_value(
Unexpected::Unsigned(v.into()),
&"one of 1, 2",
))
return Err(Error::custom(format_args!(
"invalid value: {v}, expected one of 1, 2"
)))
}
};

View File

@ -2,7 +2,7 @@ use std::{convert::TryFrom, mem::size_of};
use serde::de::{
self, DeserializeSeed, EnumAccess, Error as DeError, IntoDeserializer, MapAccess, SeqAccess,
Unexpected, VariantAccess, Visitor,
VariantAccess, Visitor,
};
use crate::error::Error;
@ -68,10 +68,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
match v {
0 => visitor.visit_bool(false),
1 => visitor.visit_bool(true),
v => Err(Error::invalid_value(
Unexpected::Unsigned(v.into()),
&"a 0 or 1",
)),
v => Err(Error::custom(format_args!(
"invalid value: {v}, expected a 0 or 1"
))),
}
}
@ -165,7 +164,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
let v = deserialize_be_number!(self, u32);
char::try_from(v)
.map_err(|_| Error::invalid_value(Unexpected::Unsigned(v.into()), &"a `char`"))
.map_err(|e| Error::custom(format_args!("invalid value {v}: {e}")))
.and_then(|v| visitor.visit_char(v))
}
@ -183,7 +182,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.input = rem;
std::str::from_utf8(data)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(data), &"a UTF-8 string"))
.map_err(Error::custom)
.and_then(|s| visitor.visit_borrowed_str(s))
}