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

Migrate to the new proc_macro API (fix #19) #20

Merged
merged 2 commits into from
Apr 21, 2018
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
2 changes: 1 addition & 1 deletion cmp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn difference() {
DecodeError { position: 4, kind: Length });
assert_eq!(x.from_base64().unwrap(), vec![0, 0]);
assert_eq!(base64::decode(x).err().unwrap(),
base64::DecodeError::InvalidByte(2, b'\n'));
base64::DecodeError::InvalidLength);
let x = b"AAB";
assert_eq!(BASE64.decode(x).err().unwrap(),
DecodeError { position: 0, kind: Length });
Expand Down
73 changes: 30 additions & 43 deletions lib/macro/internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ extern crate syn;
extern crate data_encoding;

#[cfg(not(feature = "stable"))]
use proc_macro::{Spacing, TokenNode, TokenStream, TokenTree, TokenTreeIter};
use proc_macro::token_stream::IntoIter;
#[cfg(not(feature = "stable"))]
use proc_macro::{TokenStream, TokenTree};
use std::collections::HashMap;
#[cfg(feature = "stable")]
use syn::{Lit, Token, TokenTree};
#[cfg(feature = "stable")]
use syn::parse::IResult;
#[cfg(feature = "stable")]
use syn::{Lit, Token, TokenTree};

use data_encoding::{BitOrder, Encoding, Specification, Translate, Wrap};

#[cfg(not(feature = "stable"))]
fn parse_op(tokens: &mut TokenTreeIter, op: char, key: &str) {
fn parse_op(tokens: &mut IntoIter, op: char, key: &str) {
match tokens.next() {
Some(TokenTree { span: _, kind: TokenNode::Op(x, Spacing::Alone) })
if x == op => (),
Some(TokenTree::Op(x)) if x.op() == op => (),
_ => panic!("expected {:?} after {}", op, key),
}
}
Expand All @@ -45,17 +46,17 @@ fn parse_op<'a>(input: &'a str, op: Token, key: &str) -> &'a str {
}

#[cfg(not(feature = "stable"))]
fn parse_map(mut tokens: TokenTreeIter) -> HashMap<String, TokenNode> {
fn parse_map(mut tokens: IntoIter) -> HashMap<String, TokenTree> {
let mut map = HashMap::new();
while let Some(key) = tokens.next() {
let key = match key.kind {
TokenNode::Term(term) => term.as_str().to_string(),
let key = match key {
TokenTree::Term(term) => term.as_str().to_string(),
_ => panic!("expected key got {}", key),
};
parse_op(&mut tokens, ':', &key);
let value = match tokens.next() {
None => panic!("expected value for {}", key),
Some(value) => value.kind,
Some(value) => value,
};
parse_op(&mut tokens, ',', &key);
let _ = map.insert(key, value);
Expand Down Expand Up @@ -92,13 +93,13 @@ fn parse_map(mut input: &str) -> HashMap<String, Token> {
}

#[cfg(not(feature = "stable"))]
fn get_string(map: &mut HashMap<String, TokenNode>, key: &str) -> String {
fn get_string(map: &mut HashMap<String, TokenTree>, key: &str) -> String {
let node = match map.remove(key) {
None => return String::new(),
Some(node) => node,
};
let literal = match node {
TokenNode::Literal(literal) => literal,
TokenTree::Literal(literal) => literal,
_ => panic!("expected literal for {}", key),
};
match syn::parse::string(&literal.to_string()) {
Expand All @@ -116,13 +117,13 @@ fn get_string(map: &mut HashMap<String, Token>, key: &str) -> String {
}

#[cfg(not(feature = "stable"))]
fn get_usize(map: &mut HashMap<String, TokenNode>, key: &str) -> usize {
fn get_usize(map: &mut HashMap<String, TokenTree>, key: &str) -> usize {
let node = match map.remove(key) {
None => return 0,
Some(node) => node,
};
let literal = match node {
TokenNode::Literal(literal) => literal,
TokenTree::Literal(literal) => literal,
_ => panic!("expected literal for {}", key),
};
match literal.to_string().parse() {
Expand All @@ -140,19 +141,17 @@ fn get_usize(map: &mut HashMap<String, Token>, key: &str) -> usize {
}

#[cfg(not(feature = "stable"))]
fn get_padding(map: &mut HashMap<String, TokenNode>) -> Option<char> {
fn get_padding(map: &mut HashMap<String, TokenTree>) -> Option<char> {
let node = match map.remove("padding") {
None => return None,
Some(node) => node,
};
let literal = match node {
TokenNode::Term(term) if term.as_str() == "None" => return None,
TokenNode::Literal(literal) => literal,
TokenTree::Term(term) if term.as_str() == "None" => return None,
TokenTree::Literal(literal) => literal,
_ => panic!("expected literal for padding"),
};
Some(syn::parse::character(&literal.to_string()).expect(
"expected char for padding",
))
Some(syn::parse::character(&literal.to_string()).expect("expected char for padding"))
}
#[cfg(feature = "stable")]
fn get_padding(map: &mut HashMap<String, Token>) -> Option<char> {
Expand All @@ -165,18 +164,16 @@ fn get_padding(map: &mut HashMap<String, Token>) -> Option<char> {
}

#[cfg(not(feature = "stable"))]
fn get_bool(map: &mut HashMap<String, TokenNode>, key: &str) -> Option<bool> {
fn get_bool(map: &mut HashMap<String, TokenTree>, key: &str) -> Option<bool> {
let node = match map.remove(key) {
None => return None,
Some(node) => node,
};
let term = match node {
TokenNode::Term(term) => term,
TokenTree::Term(term) => term,
_ => panic!("expected literal for padding"),
};
Some(syn::parse::boolean(term.as_str()).expect(
"expected bool for padding",
))
Some(syn::parse::boolean(term.as_str()).expect("expected bool for padding"))
}
#[cfg(feature = "stable")]
fn get_bool(map: &mut HashMap<String, Token>, key: &str) -> Option<bool> {
Expand All @@ -188,20 +185,16 @@ fn get_bool(map: &mut HashMap<String, Token>, key: &str) -> Option<bool> {
}

#[cfg(not(feature = "stable"))]
fn get_bit_order(map: &mut HashMap<String, TokenNode>) -> BitOrder {
fn get_bit_order(map: &mut HashMap<String, TokenTree>) -> BitOrder {
let node = match map.remove("bit_order") {
None => return BitOrder::MostSignificantFirst,
Some(node) => node,
};
let msb = "MostSignificantFirst";
let lsb = "LeastSignificantFirst";
match node {
TokenNode::Term(term) if term.as_str() == msb => {
BitOrder::MostSignificantFirst
}
TokenNode::Term(term) if term.as_str() == lsb => {
BitOrder::LeastSignificantFirst
}
TokenTree::Term(term) if term.as_str() == msb => BitOrder::MostSignificantFirst,
TokenTree::Term(term) if term.as_str() == lsb => BitOrder::LeastSignificantFirst,
_ => panic!("expected {} or {} for bit_order", msb, lsb),
}
}
Expand All @@ -211,12 +204,8 @@ fn get_bit_order(map: &mut HashMap<String, Token>) -> BitOrder {
let lsb = "LeastSignificantFirst";
match map.remove("bit_order") {
None => return BitOrder::MostSignificantFirst,
Some(Token::Ident(ref ident)) if ident.as_ref() == msb => {
BitOrder::MostSignificantFirst
}
Some(Token::Ident(ref ident)) if ident.as_ref() == lsb => {
BitOrder::LeastSignificantFirst
}
Some(Token::Ident(ref ident)) if ident.as_ref() == msb => BitOrder::MostSignificantFirst,
Some(Token::Ident(ref ident)) if ident.as_ref() == lsb => BitOrder::LeastSignificantFirst,
Some(_) => panic!("expected {} or {} for bit_order", msb, lsb),
}
}
Expand All @@ -228,13 +217,12 @@ fn check_present<T>(hash_map: &HashMap<String, T>, key: &str) {
}

#[cfg(not(feature = "stable"))]
fn get_encoding(mut hash_map: &mut HashMap<String, TokenNode>) -> Encoding {
fn get_encoding(mut hash_map: &mut HashMap<String, TokenTree>) -> Encoding {
check_present(&hash_map, "symbols");
let spec = Specification {
symbols: get_string(&mut hash_map, "symbols"),
bit_order: get_bit_order(&mut hash_map),
check_trailing_bits: get_bool(&mut hash_map, "check_trailing_bits")
.unwrap_or(true),
check_trailing_bits: get_bool(&mut hash_map, "check_trailing_bits").unwrap_or(true),
padding: get_padding(&mut hash_map),
ignore: get_string(&mut hash_map, "ignore"),
wrap: Wrap {
Expand All @@ -254,8 +242,7 @@ fn get_encoding(mut hash_map: &mut HashMap<String, Token>) -> Encoding {
let spec = Specification {
symbols: get_string(&mut hash_map, "symbols"),
bit_order: get_bit_order(&mut hash_map),
check_trailing_bits: get_bool(&mut hash_map, "check_trailing_bits")
.unwrap_or(true),
check_trailing_bits: get_bool(&mut hash_map, "check_trailing_bits").unwrap_or(true),
padding: get_padding(&mut hash_map),
ignore: get_string(&mut hash_map, "ignore"),
wrap: Wrap {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
//! | Input | `data-encoding` | `rustc` | `base64` | GNU `base64` |
//! | ---------- | --------------- | -------- | -------- | ------------- |
//! | `AAB=` | `Trailing(2)` | `[0, 0]` | `[0, 0]` | `\x00\x00` |
//! | `AA\nB=` | `Length(4)` | `[0, 0]` | `Err(2)` | `\x00\x00` |
//! | `AA\nB=` | `Length(4)` | `[0, 0]` | `Length` | `\x00\x00` |
//! | `AAB` | `Length(0)` | `[0, 0]` | `[0, 0]` | Invalid input |
//! | `A\rA\nB=` | `Length(4)` | `[0, 0]` | `Err(1)` | Invalid input |
//! | `-_\r\n` | `Symbol(0)` | `[251]` | `Err(0)` | Invalid input |
Expand Down