pub enum Pattern<'a> {
Char(char),
Str(&'a str),
Pred(Box<dyn Fn(char) -> bool>),
Multiple(Vec<Pattern<'a>>),
}
Expand description
Pattern on which a string can be split.
Variants§
Char(char)
Split on a matching char
Str(&'a str)
Split on a matching str
Pred(Box<dyn Fn(char) -> bool>)
Split if a char matches the predicate
Multiple(Vec<Pattern<'a>>)
Multiple patterns
§Example
use serde_aux::prelude::*;
use std::str::FromStr;
fn parser<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: FromStr + serde::Deserialize<'de> + 'static,
<T as FromStr>::Err: std::fmt::Display,
{
StringOrVecToVec::with_separator(vec![Pattern::Char('+'), Pattern::Char('-')]).into_deserializer()(deserializer)
}
#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct MyStruct {
#[serde(deserialize_with = "parser")]
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]);
Trait Implementations§
Source§impl<'a> FromIterator<&'a str> for Pattern<'a>
impl<'a> FromIterator<&'a str> for Pattern<'a>
Source§impl<'a> FromIterator<Pattern<'a>> for Pattern<'a>
impl<'a> FromIterator<Pattern<'a>> for Pattern<'a>
§Example
use serde_aux::prelude::*;
use std::str::FromStr;
fn parser<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: FromStr + serde::Deserialize<'de> + 'static,
<T as FromStr>::Err: std::fmt::Display,
{
StringOrVecToVec::with_separator(vec!['-', '+'].into_iter().collect::<Pattern>()).into_deserializer()(deserializer)
}
#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct MyStruct {
#[serde(deserialize_with = "parser")]
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]);
Auto Trait Implementations§
impl<'a> Freeze for Pattern<'a>
impl<'a> !RefUnwindSafe for Pattern<'a>
impl<'a> !Send for Pattern<'a>
impl<'a> !Sync for Pattern<'a>
impl<'a> Unpin for Pattern<'a>
impl<'a> !UnwindSafe for Pattern<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more