commit 48e21f0c8030fa807bca5073a66bcd9ca4b65cb5
parent fddc85961032ebd0c983420fbcb4878ae4de70da
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Fri, 12 Jun 2015 17:00:05 +0300
Adding wordlist build.rs file
Diffstat:
3 files changed, 73 insertions(+), 22 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -2,7 +2,7 @@
name = "diceware"
version = "0.1.0"
authors = ["Yuval Langer <yuval.langer@gmail.com>"]
-# // build = "build.rs"
+build = "build.rs"
[dependencies]
diff --git a/build.rs b/build.rs
@@ -0,0 +1,64 @@
+use std::env;
+use std::fs::File;
+use std::io::Write;
+use std::path::Path;
+
+const BEALE_CONTENTS: &'static str = include_str!("bin/wordlists/beale.wordlist.asc");
+const REINHOLD_CONTENTS: &'static str = include_str!("bin/wordlists/diceware.wordlist.asc");
+
+fn make_wordlist(contents: &str) -> Vec<&str> {
+ contents.split('\n')
+ .skip(2)
+ .take(7776)
+ .map(|s| s.splitn(2, '\t').nth(1).unwrap())
+ .collect()
+}
+
+fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String {
+ let mut output = std::string::String::new();
+ output.push_str("const BEALE_WORDLIST: [BealeWord; 7776] = [\n");
+ for word in wordlist {
+ output.push_str(" BealeWord(\"");
+ for c in word.chars() {
+ if c == '"' {
+ output.push_str("\\\"");
+ } else {
+ output.push(c);
+ }
+ }
+ output.push_str("\"),\n");
+ }
+ output.push_str("];\n");
+ return output;
+}
+
+fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String {
+ let mut output = std::string::String::new();
+ output.push_str("const REINHOLD_WORDLIST: [ReinholdWord; 7776] = [\n");
+ for word in wordlist {
+ output.push_str(" ReinholdWord(\"");
+ for c in word.chars() {
+ if c == '"' {
+ output.push_str("\\\"");
+ } else {
+ output.push(c);
+ }
+ }
+ output.push_str("\"),\n");
+ }
+ output.push_str("];\n");
+ return output;
+}
+fn main() {
+ let out_dir = env::var("OUT_DIR").unwrap();
+ let dest_path = Path::new(&out_dir).join("diceware.rs");
+ let mut f = File::create(&dest_path).unwrap();
+
+ f.write_all(
+ make_beale_struct(make_wordlist(BEALE_CONTENTS)).as_bytes()
+ ).unwrap();
+
+ f.write_all(
+ make_reinhold_struct(make_wordlist(REINHOLD_CONTENTS)).as_bytes()
+ ).unwrap();
+}
diff --git a/src/main.rs b/src/main.rs
@@ -1,6 +1,7 @@
extern crate rand;
extern crate getopts;
+
//use std::env;
//use std::io::Read;
// use std::fs::File;
@@ -11,37 +12,23 @@ use rand::Rng;
mod diceware {
extern crate rand;
- const BEALE_CONTENTS: &'static str = include_str!("../bin/wordlists/beale.wordlist.asc");
- const REINHOLD_CONTENTS: &'static str = include_str!("../bin/wordlists/diceware.wordlist.asc");
-
-
- fn make_wordlist(contents: &str) -> Vec<&str> {
- contents.split('\n')
- .skip(2)
- .take(7776)
- .map(|s| s.splitn(2, '\t').nth(1).unwrap())
- .collect()
- }
+ include!(concat!(env!("OUT_DIR"), "/diceware.rs"));
- #[derive(Debug)]
- pub struct BealeWord (&'static str);
+ #[derive(Debug,Copy,Clone)]
+ pub struct BealeWord(&'static str);
- #[derive(Debug)]
- pub struct ReinholdWord (&'static str);
+ #[derive(Debug,Copy,Clone)]
+ pub struct ReinholdWord(&'static str);
impl rand::Rand for BealeWord {
fn rand<R: rand::Rng>(rng: &mut R) -> BealeWord {
- let wordlist = make_wordlist(BEALE_CONTENTS);
- let c = rng.choose(&wordlist);
- BealeWord(c.unwrap())
+ rng.choose(&BEALE_WORDLIST).unwrap().clone()
}
}
impl rand::Rand for ReinholdWord {
fn rand<R: rand::Rng>(rng: &mut R) -> ReinholdWord {
- let wordlist = make_wordlist(REINHOLD_CONTENTS);
- let c = rng.choose(&wordlist);
- ReinholdWord(c.unwrap())
+ rng.choose(&REINHOLD_WORDLIST).unwrap().clone()
}
}
}