-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from greyblake/arbitrary-any
Support derive of Arbitrary for any inner type
- Loading branch information
Showing
9 changed files
with
143 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "any_arbitrary" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
arbitrary = { version = "1.3.2", features = ["derive"] } | ||
arbtest = "0.2.0" | ||
nutype = { path = "../../nutype", features = ["arbitrary"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use arbitrary::Arbitrary; | ||
use nutype::nutype; | ||
|
||
#[derive(Arbitrary)] | ||
struct Point { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
// Inner type is custom type Point, which implements Arbitrary. | ||
// There is no validation, but custom sanitization. | ||
// Deriving Arbitrary with custom validation would not be possible in this case. | ||
#[nutype( | ||
derive(Arbitrary), | ||
sanitize(with = |mut point| { | ||
point.x = point.x.clamp(0, 100); | ||
point.y = point.y.clamp(-200, 200); | ||
point | ||
}) | ||
)] | ||
pub struct Location(Point); | ||
|
||
fn main() { | ||
arbtest::builder().run(|u| { | ||
let location = u.arbitrary::<Location>()?; | ||
let point = location.into_inner(); | ||
assert!(point.x >= 0 && point.x <= 100); | ||
assert!(point.y >= -200 && point.y <= 200); | ||
Ok(()) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use quote::quote; | ||
|
||
use crate::{ | ||
any::models::{AnyGuard, AnyInnerType}, | ||
common::models::TypeName, | ||
}; | ||
|
||
pub fn gen_impl_trait_arbitrary( | ||
type_name: &TypeName, | ||
inner_type: &AnyInnerType, | ||
guard: &AnyGuard, | ||
) -> Result<TokenStream, syn::Error> { | ||
// It's not possible to generate implementation of `Arbitrary` trait, because we don't know nor | ||
// type nor validation rules. | ||
if guard.has_validation() { | ||
let msg = format!( | ||
"Cannot derive trait `Arbitrary` for a custom type `{type_name}` which contains validation.\nYou have to implement `Arbitrary` trait manually to guarantee that it respects the validation rules.", | ||
); | ||
return Err(syn::Error::new(Span::call_site(), msg)); | ||
} | ||
|
||
// Generate implementation of `Arbitrary` trait, assuming that inner type implements Arbitrary | ||
// too. | ||
Ok(quote!( | ||
impl ::arbitrary::Arbitrary<'_> for #type_name { | ||
fn arbitrary(u: &mut ::arbitrary::Unstructured<'_>) -> ::arbitrary::Result<Self> { | ||
let inner_value: #inner_type = u.arbitrary()?; | ||
Ok(#type_name::new(inner_value)) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn size_hint(_depth: usize) -> (usize, Option<usize>) { | ||
let n = ::core::mem::size_of::<#inner_type>(); | ||
(n, Some(n)) | ||
} | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters