Skip to content

Commit 37608fb

Browse files
committed
update Fraction deserialization
1 parent 90d1d22 commit 37608fb

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/vit-servicing-station/vit-servicing-station-lib/src/utils/serde.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::db::models::vote_options::VoteOptions;
22
use crate::utils::datetime::unix_timestamp_to_datetime;
33
use serde::de::Visitor;
44
use serde::{ser::Error, Deserialize, Deserializer, Serializer};
5+
use snapshot_lib::Fraction;
56
use std::fmt;
7+
use std::str::FromStr;
68
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
79

810
// this warning should be disable here since the interface for this function requires
@@ -113,7 +115,7 @@ pub fn deserialize_vote_options_from_string<'de, D>(
113115
where
114116
D: Deserializer<'de>,
115117
{
116-
struct VoteOptionsDeserializer();
118+
struct VoteOptionsDeserializer;
117119

118120
impl<'de> Visitor<'de> for VoteOptionsDeserializer {
119121
type Value = VoteOptions;
@@ -130,7 +132,7 @@ where
130132
}
131133
}
132134

133-
deserializer.deserialize_str(VoteOptionsDeserializer())
135+
deserializer.deserialize_str(VoteOptionsDeserializer)
134136
}
135137

136138
pub fn serialize_vote_options_to_string<S: Serializer>(
@@ -150,3 +152,39 @@ where
150152
"x" | "1" | "true"
151153
))
152154
}
155+
156+
pub fn serialize_fraction_to_string<S: Serializer>(
157+
fraction: &Fraction,
158+
serializer: S,
159+
) -> Result<S::Ok, S::Error> {
160+
serializer.serialize_str(&fraction.to_string())
161+
}
162+
163+
pub fn deserialize_fraction_from_string<'de, D>(deserializer: D) -> Result<Fraction, D::Error>
164+
where
165+
D: Deserializer<'de>,
166+
{
167+
struct FractionDeserializer;
168+
169+
impl<'de> Visitor<'de> for FractionDeserializer {
170+
type Value = Fraction;
171+
172+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
173+
formatter.write_str("A fraction value e.g. 1.0, 0.56")
174+
}
175+
176+
fn visit_str<E>(self, value: &str) -> Result<Fraction, E>
177+
where
178+
E: serde::de::Error,
179+
{
180+
match value {
181+
"NaN" => Err(E::custom(
182+
"Invalid value, should be 1.0, or 0.56".to_string(),
183+
)),
184+
_ => Ok(Fraction::from_str(value).map_err(|e| E::custom(e.to_string()))?),
185+
}
186+
}
187+
}
188+
189+
deserializer.deserialize_str(FractionDeserializer)
190+
}

src/vit-servicing-station/vit-servicing-station-lib/src/v0/endpoints/snapshot/handlers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub struct RawSnapshotInput {
4949
#[serde(serialize_with = "crate::utils::serde::serialize_unix_timestamp_as_rfc3339")]
5050
pub update_timestamp: i64,
5151
pub min_stake_threshold: Value,
52+
#[serde(deserialize_with = "crate::utils::serde::deserialize_fraction_from_string")]
53+
#[serde(serialize_with = "crate::utils::serde::serialize_fraction_to_string")]
5254
pub voting_power_cap: Fraction,
5355
pub direct_voters_group: Option<String>,
5456
pub representatives_group: Option<String>,

src/vit-servicing-station/vit-servicing-station-lib/src/v0/endpoints/snapshot/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ mod test {
971971
.reply(&put_filter)
972972
.await
973973
.status(),
974-
StatusCode::OK
974+
StatusCode::BAD_REQUEST
975975
);
976976
}
977977
}

0 commit comments

Comments
 (0)