tenderdash_proto/serializers/
from_str.rs

1//! Serialize and deserialize any `T` that implements [[core::str::FromStr]]
2//! and [[core::fmt::Display]] from or into string. Note this can be used for
3//! all primitive data types.
4#[cfg(not(feature = "std"))]
5use core::{fmt::Display, str::FromStr};
6#[cfg(feature = "std")]
7use std::{fmt::Display, str::FromStr};
8
9use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as _};
10
11use crate::prelude::*;
12/// Deserialize string into T
13pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
14where
15    D: Deserializer<'de>,
16    T: FromStr,
17    <T as FromStr>::Err: Display,
18{
19    String::deserialize(deserializer)?
20        .parse::<T>()
21        .map_err(|e| D::Error::custom(format!("{e}")))
22}
23
24/// Serialize from T into string
25pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
26where
27    S: Serializer,
28    T: Display,
29{
30    format!("{value}").serialize(serializer)
31}