Skip to content
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

Fix non-deterministic error messages #2

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ All user visible changes to this project will be documented in this file. This p



## [0.1.2] · ???
[0.1.2]: /../../tree/v0.1.2

[Diff](/../../compare/v0.1.1...v0.1.2)

### Fixed

- Non-deterministic error messages. ([#2])

[#2]: /../../pull/2




## [0.1.1] · 2021-08-13
[0.1.1]: /../../tree/v0.1.1

Expand Down
6 changes: 3 additions & 3 deletions core/src/codegen/parse_attrs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `#[derive(ParseAttrs)]` proc macro implementation.

use std::{collections::HashSet, convert::TryFrom, iter};
use std::{collections::BTreeSet, convert::TryFrom, iter};

use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -414,12 +414,12 @@ struct FieldAttrs {
/// Names of [`syn::Attribute`]'s arguments to use for parsing __instead
/// of__ the [`ParseAttrs`]'s field's [`syn::Ident`].
// #[parse(value, alias = arg)]
args: HashSet<syn::Ident>,
args: BTreeSet<syn::Ident>,

/// Names of [`syn::Attribute`]'s arguments to use for parsing __along
/// with__ the [`ParseAttrs`]'s field's [`syn::Ident`].
// #[parse(value, alias = alias)]
aliases: HashSet<syn::Ident>,
aliases: BTreeSet<syn::Ident>,

/// [`dedup`]lication strategy of how multiple values of the
/// [`ParseAttrs`]'s field should be merged.
Expand Down
64 changes: 64 additions & 0 deletions tests/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,70 @@ mod value {
}
}

mod required_aliased {
use synthez::Required;

use super::*;

#[derive(Debug, Default, ParseAttrs)]
struct Attr {
#[parse(value, alias = required)]
name: Required<syn::Ident>,
}

#[test]
fn allows_present() {
let input: syn::DeriveInput = syn::parse_quote! {
#[attr(name = minas)]
struct Dummy;
};

let res = Attr::parse_attrs("attr", &input);
assert!(res.is_ok(), "failed: {}", res.unwrap_err());

assert_eq!(
*res.unwrap().name,
syn::Ident::new_on_call_site("minas"),
);
}

#[test]
fn allows_alias() {
let input: syn::DeriveInput = syn::parse_quote! {
#[attr(required = tirith)]
struct Dummy;
};

let res = Attr::parse_attrs("attr", &input);
assert!(res.is_ok(), "failed: {}", res.unwrap_err());

assert_eq!(
*res.unwrap().name,
syn::Ident::new_on_call_site("tirith"),
);
}

#[test]
fn forbids_absent() {
let input: syn::DeriveInput = syn::parse_quote! {
struct Dummy;
};

let res = Attr::parse_attrs("attr", &input);
assert!(res.is_err(), "should fail, but is ok");

let err = res.unwrap_err().to_string();
assert!(
err.contains(
"either `name` or `required` argument of `#[attr]` \
attribute is expected to be present, but is absent",
),
"wrong err:\n{}",
err,
);
}
}

mod required_fallback {
use synthez::{field, Required};

Expand Down