serde_aux::field_attributes

Enum Pattern

Source
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> From<&'a str> for Pattern<'a>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a, P> From<P> for Pattern<'a>
where P: Fn(char) -> bool + 'static,

Source§

fn from(pred: P) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Vec<Pattern<'a>>> for Pattern<'a>

Source§

fn from(patterns: Vec<Pattern<'a>>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<char> for Pattern<'a>

Source§

fn from(c: char) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromIterator<&'a str> for Pattern<'a>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = &'a str>,

Creates a value from an iterator. Read more
Source§

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

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = Pattern<'a>>,

Creates a value from an iterator. Read more
Source§

impl<'a> FromIterator<char> for Pattern<'a>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = char>,

Creates a value from an iterator. Read more

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> 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.