-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Val err details #12
Val err details #12
Changes from 11 commits
9c2a110
84a0748
da75369
545cf7c
228ef1d
bfa720a
bf23b9c
25abe8f
293c758
db02884
3adae1f
5db539b
6a9684a
426e4bc
c85f522
1cc5924
b779b76
49409f8
39e8928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,9 @@ pub fn define(input: TokenStream) -> TokenStream { | |
|
||
let validate_fn = match &guard { | ||
GuardClosure::Ensure(EnsureClosure(closure)) => quote! { | ||
fn validate(v: &Self::Target) -> Option<prae::ValidationError> { | ||
fn validate(v: &Self::Target) -> Option<prae::ValidationError::<#ty>> { | ||
let f: fn(&Self::Target) -> bool = #closure; | ||
if f(v) { None } else { Some(prae::ValidationError) } | ||
if f(v) { None } else { Some(prae::ValidationError::<#ty>::new(stringify!(#ident), v.clone()) ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the type is not clone? In this case the user will use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, exactly. The following code works on HEAD but not on this branch: #[derive(Debug)]
struct User {
name: String,
}
prae::define! {
ValidUser: User
ensure |u: &User| !u.name.is_empty()
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's either clone, copy or have the error manage reference lifetimes. I would like to avoid having lifetimes as that constraint will leak into the users code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can add a nicer message though, instead of a compile error emanating from the proc macro? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's also can't be done with the current code. Introducing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a way around cloning, as we have to clone at some point so the error can store the value, or.... you convert the value to a string which requires Requiring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latest commit removes the value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making it Do you want to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do it. I'll update the non_clone test too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
}, | ||
GuardClosure::Validate(ValidateClosure(closure, err_ty)) => quote! { | ||
|
@@ -48,7 +48,7 @@ pub fn define(input: TokenStream) -> TokenStream { | |
}; | ||
|
||
let err_ty = match &guard { | ||
GuardClosure::Ensure(_) => quote!(prae::ValidationError), | ||
GuardClosure::Ensure(_) => quote!(prae::ValidationError::<#ty>), | ||
GuardClosure::Validate(ValidateClosure(_, err_ty)) => quote!(#err_ty), | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the whole
test_deps
businessThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the lint to warn about unused crate dependencies.
#![warn(unused_crate_dependencies)]
But it give's false positives when buliding the separate integration test binaries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, now I get it. I'll add a comment referencing the related issue: rust-lang/rust#57274
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 😉