pub struct StringOrVecToVec<'a, T, E> { /* private fields */ }
Expand description
Builder to create a parser, that parses a separated string or a vec into a vec.
§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::default().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]);
Implementations§
Source§impl<'a, 'de, T> StringOrVecToVec<'a, T, T::Err>
impl<'a, 'de, T> StringOrVecToVec<'a, T, T::Err>
Sourcepub fn with_separator(separator: impl Into<Pattern<'a>>) -> Self
pub fn with_separator(separator: impl Into<Pattern<'a>>) -> Self
Create a StringOrVecToVec
builder with a custom separator. T::from_str
is used to parse
the elements of the list.
§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(|c| c == '-' || c == '+').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]);
Sourcepub fn skip_empty(&mut self, skip_empty: bool) -> &mut Self
pub fn skip_empty(&mut self, skip_empty: bool) -> &mut Self
Sets the flag to skip empty separations.
use serde_aux::prelude::*;
use std::str::FromStr;
fn parser_skip_empty<'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,
{
let mut parser = StringOrVecToVec::with_separator(|c| c == '-' || c == '+');
parser.skip_empty(true);
parser.into_deserializer()(deserializer)
}
#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct MyStructSkipEmpty {
#[serde(deserialize_with = "parser_skip_empty")]
list: Vec<i32>,
}
let s = r#" { "list": "1-2+3-4--++--" } "#;
let a: MyStructSkipEmpty = serde_json::from_str(s).unwrap();
assert_eq!(&a.list, &[1, 2, 3, 4]);
Source§impl<'a, T, E> StringOrVecToVec<'a, T, E>
impl<'a, T, E> StringOrVecToVec<'a, T, E>
Sourcepub fn new(
separator: impl Into<Pattern<'a>>,
parser: impl FnMut(&str) -> Result<T, E> + 'static,
skip_empty: bool,
) -> Self
pub fn new( separator: impl Into<Pattern<'a>>, parser: impl FnMut(&str) -> Result<T, E> + 'static, skip_empty: bool, ) -> Self
Create a deserializer with a custom separator and parsing function.
§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::new('-', |s| s.trim().parse(), false).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]);
Sourcepub fn with_parser(parser: impl FnMut(&str) -> Result<T, E> + 'static) -> Self
pub fn with_parser(parser: impl FnMut(&str) -> Result<T, E> + 'static) -> Self
Create a deserializer with a custom parsing function. The input string will be separated on
,
.
§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_parser(|s| s.trim().parse()).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]);
Sourcepub fn into_deserializer<'de, D>(
self,
) -> impl FnMut(D) -> Result<Vec<T>, <D as Deserializer<'de>>::Error>
pub fn into_deserializer<'de, D>( self, ) -> impl FnMut(D) -> Result<Vec<T>, <D as Deserializer<'de>>::Error>
Creates the actual deserializer from this builder.
Trait Implementations§
Auto Trait Implementations§
impl<'a, T, E> Freeze for StringOrVecToVec<'a, T, E>
impl<'a, T, E> !RefUnwindSafe for StringOrVecToVec<'a, T, E>
impl<'a, T, E> !Send for StringOrVecToVec<'a, T, E>
impl<'a, T, E> !Sync for StringOrVecToVec<'a, T, E>
impl<'a, T, E> Unpin for StringOrVecToVec<'a, T, E>
impl<'a, T, E> !UnwindSafe for StringOrVecToVec<'a, T, E>
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