Skip to content
Open
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
81 changes: 40 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,6 @@ trait FormatOutput {
fn write(&self, w: &mut Write, name: &String, hex: bool, vec: &Vec<CEnum>) -> Result<()>;
}

struct FormatOutputFromPrimative;
impl FormatOutput for FormatOutputFromPrimative {
fn write(&self, w: &mut Write, name: &String, hex: bool, vec: &Vec<CEnum>) -> Result<()> {
try!(write!(w, "impl ::num::traits::FromPrimitive for {} {{\n", name));
try!(write!(w, " #[allow(dead_code)]\n"));
try!(write!(w, " fn from_i64(n: i64) -> Option<Self> {{\n"));
try!(write!(w, " match n {{\n"));
for v in vec {
if hex {
try!(write!(w, " 0x{:X} => Some({}::{}),\n", v.i, name, v.s));
}
else {
try!(write!(w, " {} => Some({}::{}),\n", v.i, name, v.s));
}
}
try!(write!(w, " _ => None\n"));
try!(write!(w, " }}\n"));
try!(write!(w, " }}\n"));
try!(write!(w, " #[allow(dead_code)]\n"));
try!(write!(w, " fn from_u64(n: u64) -> Option<Self> {{\n"));
try!(write!(w, " match n {{\n"));
for v in vec {
if hex {
try!(write!(w, " 0x{:X} => Some({}::{}),\n", v.i, name, v.s));
}
else {
try!(write!(w, " {} => Some({}::{}),\n", v.i, name, v.s));
}
}
try!(write!(w, " _ => None\n"));
try!(write!(w, " }}\n"));
try!(write!(w, " }}\n"));
try!(write!(w, "}}\n"));
Ok(())
}
}

struct FormatOutputPrettyFmt;
impl FormatOutput for FormatOutputPrettyFmt {
#[allow(unused_variables)]
Expand Down Expand Up @@ -207,7 +170,10 @@ impl FormatOutput for FormatOutputFromStr {

struct FormatOutputEnum;
impl FormatOutputEnum {
fn write(&self, w: &mut Write, name: &String, derive: Option<&String>, hex: bool, vec: &Vec<CEnum>) -> Result<()> {
fn write(&self, w: &mut Write, name: &String, derive: Option<&String>, hex: bool, efp: bool, vec: &Vec<CEnum>) -> Result<()> {
if efp {
try!(write!(w, "enum_from_primitive! {{\n"));
}
try!(write!(w, "#[allow(dead_code, non_camel_case_types)]\n"));
match derive
{
Expand All @@ -226,6 +192,9 @@ impl FormatOutputEnum {
}

try!(write!(w, "}}\n"));
if efp {
try!(write!(w, "}}\n"));
}
Ok(())
}
}
Expand Down Expand Up @@ -311,7 +280,7 @@ fn parse_toml(path: &PathBuf) -> Result<FileArgs>
fn get_num(s: &str) -> i32 {
use std::str::FromStr;
use regex::Regex;
let re_int = Regex::new(r"^(0x)?([:digit:]+)$").unwrap();
let re_int = Regex::new(r"^(0x)?([:xdigit:]+)$").unwrap();
let re_shift = Regex::new(r"^([:digit:]+)[:space:]*<<[:space:]*([:digit:]+)$").unwrap();

if re_int.is_match(s) {
Expand Down Expand Up @@ -407,7 +376,6 @@ pub fn process(file_path_in: Option<&PathBuf>, file_path_out: Option<&PathBuf>,
if file_args.fromstr { fov.push(Box::new(FormatOutputFromStr)); }
if file_args.default { fov.push(Box::new(FormatOutputDefault)); }
if file_args.display { fov.push(Box::new(FormatOutputDisplay)); }
if file_args.fromprimative { fov.push(Box::new(FormatOutputFromPrimative)); }
if file_args.pretty_fmt { fov.push(Box::new(FormatOutputPrettyFmt)); }

let vi = get_input(file_path_in, &file_args);
Expand All @@ -429,7 +397,7 @@ pub fn process(file_path_in: Option<&PathBuf>, file_path_out: Option<&PathBuf>,
};

let derive = file_args.derive.as_ref();
try!(FormatOutputEnum.write(&mut w, &name, derive, file_args.hex, &vi));
try!(FormatOutputEnum.write(&mut w, &name, derive, file_args.hex, file_args.fromprimative, &vi));
for vw in fov {
try!(vw.write(&mut w, &name, file_args.hex, &vi));
}
Expand Down Expand Up @@ -562,3 +530,34 @@ fn test_parse_buff_enum() {
assert!(v[4].i == 20); assert!(v[4].s == "RTM_NEWADDR");
assert!(v[5].i == 21); assert!(v[5].s == "RTM_DELADDR");
}

#[test]
fn test_parse_buff_hex() {
use std::io::Cursor;
let s = "ten = 0xa,\n\
eleven = 0xb,\n\
twelve = 12,";

let buff = Cursor::new(s.as_bytes());
let v = parse_buff(buff, true);

assert!(v[0].i == 10); assert!(v[0].s == "ten");
assert!(v[1].i == 11); assert!(v[1].s == "eleven");
assert!(v[2].i == 12); assert!(v[2].s == "twelve");
}

#[test]
#[should_panic]
fn test_parse_buff_not_hex() {
use std::io::Cursor;
let s = "ten = 10,\n\
eleven = 11,\n\
twelve = 12abc,"; // <-- not valid for base 10

let buff = Cursor::new(s.as_bytes());
let v = parse_buff(buff, true);

assert!(v[0].i == 10); assert!(v[0].s == "ten");
assert!(v[1].i == 11); assert!(v[1].s == "eleven");
assert!(v[2].i == 12); assert!(v[2].s == "twelve");
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn parse_options() -> (Args, FileArgs) {
opts.optflag("", "default", "implement the Default trait with the first \
value");
opts.optflag("", "display", "implement the std::fmt::Display trait");
opts.optflag("", "fromprimative", "implement the num::traits::FromPrimitive trait");
opts.optflag("", "fromprimative", "use the enum_primitive crate to get from_primative trait");
opts.optflag("", "fromstr", "implement the std::str::FromStr trait");
opts.optflag("", "hex", "hexadecimal output");
opts.optflag("", "pretty_fmt", "implement pretty_fmt()");
Expand Down