Skip to content

Commit

Permalink
Fix the tests in endian_trait_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
myrrlyn committed Feb 23, 2018
1 parent bac6b9a commit 47625ff
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
62 changes: 62 additions & 0 deletions endian_trait_derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Changelog

List of notable changes to the custom-derive macro for the `Endian` trait.

## 0.3.2

### Added

- This changelog.

### Changed

- Made the tests pass correctly

The `Endian` trait is in the parent crate, but the macro that derives it is
in this crate, which makes the test libraries very confused when they try to
pull in both from this crate, which is a dependency of the parent and thus
cannot depend on the parent, except when building the *test executable*
which is a standalone crate that pulls in both of them.

## 0.3.1

### Changed

- Increased `syn` and `quote` versions from `0.11` and `0.3` to `0.12` and
`0.4`, respectively

## 0.3.0

### Changed

- Refactored the code generator to reduce duplication
- Minimum compiler version is now 1.20

1.19 allows unit structs to be declared as `Type { }` and tuple structs as
`Type { 0: val, ... }`

1.20 allows the `unimplemented!` macro to take a descriptive string.

## 0.2.0

### Added

- Implement the derive function for tuple structs and for unit structs.
- Tests

### Changed

- Use stable Rust instead of nightly

## 0.1.1

### Added

- crates.io metadata in `Cargo.toml`

## 0.1.0

### Added

- Initial implementation, capable of deriving only on standard, named-field,
structs.
5 changes: 4 additions & 1 deletion endian_trait_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "endian_trait_derive"
version = "0.3.1"
version = "0.3.2"
authors = [
"myrrlyn <myrrlyn@outlook.com>",
]
Expand All @@ -26,6 +26,9 @@ features = [
"derive",
]

[dev-dependencies]
endian_trait = "0.3"

[badges.gitlab]
repository = "myrrlyn/endian_trait"
branch = "master"
Expand Down
51 changes: 51 additions & 0 deletions endian_trait_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This crate shouldn't be used directly; use
```rust,no-run
#[macro_use]
extern crate endian_trait;
# fn main() {}
```
and this crate will be pulled in transitively.
Expand All @@ -34,12 +35,20 @@ crate and `Endian` trait in scope.
```rust
#[macro_use]
extern crate endian_trait;
# // This is needed because of the fact that the test is executed from within
# // the context of the derive crate. The tests in the trait crate demonstrate
# // that the macro is correctly re-exported.
# #[macro_use]
# extern crate endian_trait_derive;
use endian_trait::Endian;
#[derive(Endian)]
struct Foo<A: Endian, B: Endian> {
bar: A,
baz: B,
}
# fn main() {}
```
!*/

Expand Down Expand Up @@ -85,7 +94,49 @@ fn impl_endian(ast: DeriveInput) -> Tokens {
// Get any generics from the struct
let generics: &Generics = &ast.generics;
match ast.data {
// I apologize for the mess that is to come: syn's parser is very
// informative and that means I have to do a lot of filtering to
// establish that the incoming code is the very specific form of
// correct with which I can work.
Data::Enum(ref _variants) => {
/*
// First up, grab the attributes decorating the enum
let attrs: &Vec<Attribute> = &ast.attrs;
// Find the attr that is #[repr(_)]. We need to build one for
// comparison.
let repr_ident = Ident::new("repr");
// Seek for a #[repr(_)] attribute
let repr: &MetaItem = &attrs.iter().find(|ref a| match &a.value {
&MetaItem::List(ref i, _) => i == &repr_ident,
_ => false
})
// Unwrap, and panic if this returned None instead of Some, because
// repr is the bare minimum of required attributes
.expect("Endian can only be derived on enums with #[repr()] attributes")
// Take a reference to the actual value of the attribute, which is
// "repr(_)" from the source.
.value;
// Now figure out what the repr *is*.
// The format is #[repr(Name)] where Name is one of: C, packed,
// {i,u}{8,16,32,64}. This comes out to be a
// List(Ident(repr), Vec<_>) in syn structures. We want the
// one-element Vec's one element. Anything else is broken.
let repr_inner: &NestedMetaItem = match repr {
&MetaItem::List(_, ref inners) => {
if inners.len() != 1 {
panic!("Your #[repr()] attribute is invalid");
}
&inners[0]
},
_ => panic!("Your #[repr()] attribute is invalid"),
};
// The interior is another meta-item, not a literal, because it's
// not in quotes.
let repr_ty: &String = match repr_inner {
&NestedMetaItem::MetaItem(Lit::Str(ref repr_type, _)) => repr_type,
_ => panic!("Your #[repr()] attribute is invalid"),
};
*/
unimplemented!(r#"Enum are not integral types.
If enums are present in a type that will be serialized in a way to require
Expand Down

0 comments on commit 47625ff

Please sign in to comment.