mirror of
https://github.com/actix/actix-web.git
synced 2024-12-16 21:26:34 +00:00
add tests for deserialize_any
This commit is contained in:
parent
6df38522eb
commit
8d6c23038a
1 changed files with 98 additions and 0 deletions
|
@ -722,6 +722,104 @@ mod tests {
|
|||
assert_eq!(vals.value, "/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_path_decode_any() {
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum AnyEnumCustom {
|
||||
String(String),
|
||||
Int(u32),
|
||||
Other,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for AnyEnumCustom {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
struct Vis;
|
||||
impl<'de> Visitor<'de> for Vis {
|
||||
type Value = AnyEnumCustom;
|
||||
|
||||
fn expecting<'a>(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'a>,
|
||||
) -> std::fmt::Result {
|
||||
write!(f, "my thing")
|
||||
}
|
||||
|
||||
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(AnyEnumCustom::Int(v))
|
||||
}
|
||||
|
||||
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
|
||||
v.parse().map(AnyEnumCustom::Int).or_else(|_| {
|
||||
Ok(match v {
|
||||
"other" => AnyEnumCustom::Other,
|
||||
_ => AnyEnumCustom::String(format!("some str: {v}")),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_any(Vis)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum AnyEnumDerive {
|
||||
String(String),
|
||||
Int(u32),
|
||||
Other,
|
||||
}
|
||||
|
||||
// single
|
||||
let rdef = ResourceDef::new("/{key}");
|
||||
|
||||
let mut path = Path::new("/%25");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: AnyEnumCustom = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment, AnyEnumCustom::String("some str: %".to_string()));
|
||||
|
||||
let mut path = Path::new("/%25");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: AnyEnumDerive = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment, AnyEnumDerive::String("%".to_string()));
|
||||
|
||||
// seq
|
||||
let rdef = ResourceDef::new("/{key}/{value}");
|
||||
|
||||
let mut path = Path::new("/other/123");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: (AnyEnumCustom, AnyEnumDerive) =
|
||||
serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment.0, AnyEnumCustom::Other);
|
||||
// Deserializes to `String` instead of `Int` because deserializing defaults to `str` and serde doesn't automatically implement parsing numbers from `&str`s
|
||||
assert_eq!(segment.1, AnyEnumDerive::String("123".to_string()));
|
||||
|
||||
// map
|
||||
#[derive(Deserialize)]
|
||||
struct Vals {
|
||||
key: AnyEnumCustom,
|
||||
value: AnyEnumDerive,
|
||||
}
|
||||
|
||||
let rdef = ResourceDef::new("/{key}/{value}");
|
||||
|
||||
let mut path = Path::new("/123/%2F");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let vals: Vals = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(vals.key, AnyEnumCustom::Int(123));
|
||||
assert_eq!(vals.value, AnyEnumDerive::String("/".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_borrowed() {
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
Loading…
Reference in a new issue