macro_rules! StringOrVecToVecParser {
    ($name:ident, $separator:expr, $skip_empty:expr) => { ... };
    ($t:ty, $name:ident, $pattern:expr, $converter:expr, $skip_empty:expr) => { ... };
}
Expand description

Create a parser quickly.

use serde_aux::prelude::*;
use std::str::FromStr;

serde_aux::StringOrVecToVecParser!(parse_between_commas, |c| { c == ',' }, true);

#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct MyStruct {
    #[serde(deserialize_with = "parse_between_commas")]
    list: Vec<i32>,
}

let s = r#" { "list": "1,2,3,4" } "#;
let a: MyStruct = serde_json::from_str(s).unwrap();
assert_eq!(&a.list, &[1, 2, 3, 4]);

let s = r#" { "list": [1,2,3,4] } "#;
let a: MyStruct = serde_json::from_str(s).unwrap();
assert_eq!(&a.list, &[1, 2, 3, 4]);


serde_aux::StringOrVecToVecParser!(u8, parse_hex_with_spaces, ' ', |s| { u8::from_str_radix(s, 16) }, true);

#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct MyStructHex {
    #[serde(deserialize_with = "parse_hex_with_spaces")]
    list: Vec<u8>,
}

let s = r#" { "list": "a1 b2 c3 d4 " } "#;
let a: MyStructHex = serde_json::from_str(s).unwrap();
assert_eq!(&a.list, &[0xa1, 0xb2, 0xc3, 0xd4]);

let s = r#" { "list": "a1 b2 c3  d4   " } "#;
let a: MyStructHex = serde_json::from_str(s).unwrap();
assert_eq!(&a.list, &[0xa1, 0xb2, 0xc3, 0xd4]);