-
I have the following enumeration: #[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, SerializeDisplay)]
pub enum ProjectIdentifier {
Id(#[serde_as(as = "Base62Encoded<u64>")] u64),
Slug(String),
} When running a
When I use the following, the compiler does not complain: #[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, SerializeDisplay)]
pub enum ProjectIdentifier {
#[serde_as(as = "Base62Encoded<u64>")]
Id(u64),
Slug(String),
} But my question here is; what if I wanted different invocations of the proc-macro on multiple tuple-fields? For example: #[derive(Debug, Clone, Display, SerializeDisplay)]
pub enum IDontKnowThing {
ImaVariant(
#[serde_as(as = "Vec<Base62Encoded<u64>>")] Vec<u64>,
#[serde_as(as = "Base62Encoded<u128>")] u128
),
} Is such a thing possible? How does having the invocation of If you're curious, this is use std::str::FromStr;
use serde::{Deserialize, Serialize, Serializer};
use serde_with::{DeserializeAs, DeserializeFromStr, SerializeAs, SerializeDisplay};
#[derive(PartialEq, SerializeDisplay, DeserializeFromStr)]
pub struct Base62Encoded<T>(pub T);
impl<T> std::fmt::Display for Base62Encoded<T>
where
T: Clone + Into<u128>,
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&base62::encode(self.0.clone()))
}
}
impl<T> FromStr for Base62Encoded<T>
where
u128: TryInto<T>,
{
type Err = base62::DecodeError;
fn from_str(other: &str) -> Result<Self, Self::Err> {
match base62::decode(other)?.try_into() {
Ok(n) => Ok(Base62Encoded(n)),
Err(_) => Err(Self::Err::ArithmeticOverflow),
}
}
}
impl<T> SerializeAs<T> for Base62Encoded<T>
where
T: Clone + Into<u128>,
{
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Base62Encoded(source.clone()).serialize(serializer)
}
}
impl<'de, T> DeserializeAs<'de, T> for Base62Encoded<T>
where
u128: TryInto<T>,
{
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
where
D: serde::Deserializer<'de>,
{
Base62Encoded::deserialize(deserializer).map(|n| n.0)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Hi there, most features of this crate are there to make it easier to use the derive macros The problem with your
In the case of your |
Beta Was this translation helpful? Give feedback.
Hi there, most features of this crate are there to make it easier to use the derive macros
serde::Serialize
andserde::Deserialize
in situations, which don't quite follow the usual rules and need some adjusting. Everything related toserde_as
is part of that.The problem with your
ProjectIdentifier
is that you are not derivingserde::Serialize
orserde::Deserialize
. Theserde_as
annotation on theu64
field will be converted into aserde
annotation, but this is not understood by any of the derive macros. Therefore, you see the error. It seems like you forgot to deriveserde::Deserialize
or accidentally usedSerializeDisplay
instead ofserde::Serialize
.SerializeDisplay
does not interact with