-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add missing
attribute for struct fields
#39
Add missing
attribute for struct fields
#39
Conversation
af3f4a3
to
9c38c99
Compare
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.
Left a few comments. Regarding testing, I prefer an end-to-end test where you define a type which uses the attribute in question and then check that it behaves the way we expect when hydrating. Something like:
#[derive(Debug, Hydrate)]
struct MaybeString {
#[autosurgeon(missng=std::default::Default)]
value: Option<String>
}
#[test]
fn hydrate_missing() {
let mut doc = automerge::AutoCommit::new();
let value: MaybeString = hydrate(&doc).unwrap();
assert!(value.is_none());
}
autosurgeon-derive/src/hydrate.rs
Outdated
@@ -261,6 +261,25 @@ fn gen_newtype_struct_wrapper( | |||
|
|||
let inner_ty = quote_spanned!(field.span()=> #inner_ty); | |||
|
|||
let missing = if let Some(missing_fn) = attrs.missing() { | |||
quote! { |
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 think we'll want to use quote_spanned
here to make sure that the compile error produced is comprehensible. It may be worth adding a trybuild
based UI test to test the errors as described in https://ferrous-systems.com/blog/testing-proc-macros/#diagnostics-error-paths (I didn't do that previously because this was my first major procedural macro project).
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.
Sorry, I'm still not sure what is the right span to use when...
For example, should I use field.span()
here: https://github.com/automerge/autosurgeon/pull/39/files#diff-39105dca315265219e34078932ac265ac7c14d4a5c46958808d175aa7b2de123L265-L276
If I'd like to do something similar to https://github.com/automerge/autosurgeon/pull/39/files#diff-39105dca315265219e34078932ac265ac7c14d4a5c46958808d175aa7b2de123R278-R285
What do you mean by "do anything about the key"? I don't think we need support for enums (provided we support variant fields). |
As in, migrating the key, from no key to some key. But perhaps that's not a possible scenario and I'm simply mistaken. EDIT: Now that I think about it, that doesn't seem like something a default would help with anyway...
Okay, so I'll have to add support for variant fields at least. 🙈 |
68eda70
to
a0a6dcf
Compare
a0ace88
to
21b8444
Compare
#[derive(Clone, Debug, PartialEq, Eq)]
struct Inner(u64);
#[derive(Clone, Debug, PartialEq, Eq, Hydrate)]
#[autosurgeon(hydrate = "hydrate_outer", missing = "Default::default")]
struct Outer(Inner); This should be a parse error, but isn't... I'm not sure how to fix this without a refactoring of the parsing code. Apparently we skip everything if there's autosurgeon/autosurgeon-derive/src/hydrate.rs Lines 25 to 27 in 4d9ad30
|
21b8444
to
e54d195
Compare
e54d195
to
3b28455
Compare
ping @alexjg |
Apologies, I missed this question. Will take a look tomorrow, maybe we will need to refactor a little. To be clear, the impact right now is that if you have a |
Only on the container, but we already ignore everything else if Currently that would not produce parse errors, so it might be a bit confusing for the user... Their attributes are just silently ignored. Perhaps we could fix that in another PR? |
Yeah let's fix the parse errors in another PR |
This is now published as |
I think you forgot to push the |
Good spot, done. |
Closes #32