31 lines
907 B
Rust
31 lines
907 B
Rust
use arbitrary::{Arbitrary, Unstructured};
|
|
use canvas::{Card, CardField};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct RandomCard(pub(crate) Card);
|
|
#[derive(Debug)]
|
|
struct RandomCardField(CardField);
|
|
|
|
impl<'a> Arbitrary<'a> for RandomCardField {
|
|
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
|
|
let i = u.int_in_range(0..=15)?;
|
|
if let Ok(w) = CardField::try_from_bits(i) {
|
|
Ok(RandomCardField(w))
|
|
} else {
|
|
Ok(RandomCardField(CardField::Empty))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> Arbitrary<'a> for RandomCard {
|
|
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
|
|
let mut r = vec![];
|
|
for _ in 0..5 {
|
|
r.push(u.arbitrary::<RandomCardField>()?)
|
|
}
|
|
let w: [RandomCardField; 5] = r.try_into().unwrap();
|
|
let r = Card::from(w.map(|x: RandomCardField| x.0));
|
|
Ok(RandomCard(r))
|
|
}
|
|
}
|