pub enum InlineList<T> {
    List(Vec<T>),
    Inline(T),
}
Available on crate feature serde only.
Expand description

A data type that can support listed items, or inline it if there is single item.

use serde::Deserialize;
use yaml_peg::{node, serde::InlineList};

#[derive(Deserialize)]
struct Content {
    img: InlineList<Img>,
}

#[derive(Deserialize)]
struct Img {
    src: String,
}

let n_listed = node!({"img" => node!([node!({"src" => "img/1.png"}), node!({"src" => "img/2.png"})])});
let n_inline = node!({"img" => node!({"src" => "img/1.png"})});
let listed = Content::deserialize(n_listed).unwrap();
let inline = Content::deserialize(n_inline).unwrap();
for (i, img) in listed.img.iter().enumerate() {
    assert_eq!(format!("img/{}.png", i + 1), img.src);
}
for (i, img) in inline.img.into_iter().enumerate() {
    assert_eq!(format!("img/{}.png", i + 1), img.src);
}

Variants

List(Vec<T>)

Listed representation.

Inline(T)

Inline representation.

Implementations

Return the iterator over the items.

Get the length of the list.

Return true if the list is empty.

Return true if the list has only one item.

Trait Implementations

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.