#![no_std]
pub trait Exemplars: Sized {
fn exemplars() -> impl IntoIterator<Item = Self>;
fn exemplar() -> Self {
Self::exemplars()
.into_iter()
.next()
.expect("invalid impl Exemplars: must provide at least one example value")
}
}
impl Exemplars for () {
fn exemplars() -> impl IntoIterator<Item = Self> {
[()]
}
}
macro_rules! impl_for_number_types {
($($t:ident),+) => {$(
impl Exemplars for $t {
fn exemplars() -> impl IntoIterator<Item = Self> {
1..=Self::MAX
}
}
)+}
}
impl_for_number_types!(usize, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
impl<T: Exemplars> Exemplars for Option<T> {
fn exemplars() -> impl IntoIterator<Item = Self> {
T::exemplars().into_iter().map(Some).chain([None])
}
}
#[cfg(feature = "alloc")]
mod alloc {
extern crate alloc;
use crate::Exemplars;
impl Exemplars for alloc::string::String {
fn exemplars() -> impl IntoIterator<Item = Self> {
["example".into()]
}
}
impl<T: Exemplars> Exemplars for alloc::vec::Vec<T> {
fn exemplars() -> impl IntoIterator<Item = Self> {
T::exemplars().into_iter().map(|x| alloc::vec![x])
}
}
}
#[cfg(feature = "std")]
mod std {
extern crate std;
}
#[cfg(feature = "rust_decimal")]
impl Exemplars for ::rust_decimal::Decimal {
fn exemplars() -> impl IntoIterator<Item = Self> {
[Self::ONE]
}
}
#[cfg(feature = "uuid")]
impl Exemplars for ::uuid::Uuid {
fn exemplars() -> impl IntoIterator<Item = Self> {
[Self::max()]
}
}