-
I have some struct that need to travel with the form: #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Clock {
/// An Id that identifies the partition (user, device, etc) that request the change
pub actor: String,
/// The version is a monotonical mark of the changes applied to the data
pub version: usize,
/// A timestamp in UTC used for last-updated-win semantics.
pub t: DateTime<Utc>,
}
Clock {
actor: "system",
version: 0,
t: 2022-03-04T19:59:01.970994Z,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LineForm {
pub dot: Clock,
} Now the problem is that the field is not decoded back: <input type="hidden" name="dot" value ="{"actor":"system","version":0,"t":"2022-03-07T23:41:35.823679Z"}" />` |
Beta Was this translation helpful? Give feedback.
Answered by
mamcx
Mar 9, 2022
Replies: 1 comment
-
For the posterity: In reddit I get a solution, and using: //Serde with FromStr/Display
pub mod from {
pub mod serde_str {
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
String::deserialize(deserializer)?
.parse::<T>()
.map_err(|e| D::Error::custom(format!("{}", e)))
}
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: std::fmt::Display,
{
format!("{}", value).serialize(serializer)
}
}
}
...
#[serde(with = "from::serde_str")]
pub dot: Clock, It works. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
robjtede
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the posterity: In reddit I get a solution, and using: