serde_aux::field_attributes

Struct StringOrVecToVec

Source
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>
where T: FromStr + Deserialize<'de> + 'static, <T as FromStr>::Err: Display,

Source

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]);
Source

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>

Source

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]);
Source

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]);
Source

pub fn into_deserializer<'de, D>( self, ) -> impl FnMut(D) -> Result<Vec<T>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>, T: Deserialize<'de>, E: Display, 'a: 'de,

Creates the actual deserializer from this builder.

Trait Implementations§

Source§

impl<'a, 'de, T> Default for StringOrVecToVec<'a, T, T::Err>
where T: FromStr + Deserialize<'de> + 'static, <T as FromStr>::Err: Display,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.