Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Add additional alphabet
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Kuche <[email protected]>
  • Loading branch information
Zitrone44 committed Mar 26, 2018
1 parent 963dbe1 commit aa0560b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zlnk"
version = "0.1.1"
version = "0.2.0"
authors = ["Jonas Kuche <[email protected]>"]

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/env_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Env {
pub database_path: String,
pub url_regex: Regex,
pub short_length: usize,
pub short_alphabet: String,
pub bad_request_message: String
}

Expand All @@ -18,6 +19,7 @@ pub fn init() -> Env {
database_path: env::var("DATABASE_PATH").unwrap_or("./db".to_string()),
url_regex: Regex::new(&env::var("URL_REGEX").unwrap_or(URL_REGEX.to_string())).unwrap(),
short_length: usize::from_str(&env::var("SHORT_LENGTH").unwrap_or("5".to_string())).unwrap(),
short_alphabet: env::var("SHORT_ALPHABET").unwrap_or("hex".to_string()),
bad_request_message: env::var("BAD_REQUEST_MESSAGE").unwrap_or("Ungültige URL".to_string())
}
}
21 changes: 16 additions & 5 deletions src/shortener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ use env_loader::Env;
use rand::{thread_rng, Rng};
use rocksdb::DB;

pub fn random(length: usize) -> String {
pub fn random(length: usize, alphabet_name: String) -> String {
let mut rng = thread_rng();
let rands = rng.gen_iter::<usize>().take(length).collect::<Vec<usize>>();
let hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
let alphabet;
if alphabet_name == "hex" {
alphabet = vec!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
} else if alphabet_name == "decimal" {
alphabet = vec!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
} else if alphabet_name == "alpha" {
alphabet = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
} else if alphabet_name == "alpha-numeric" {
alphabet = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
} else {
panic!("Invalid Alphabet");
}
let mut result = vec![];
for rand in rands {
let num = rand % 16;
result.push(hex[num]);
let num = rand % alphabet.len();
result.push(alphabet[num]);
}
result.join("")
}
Expand All @@ -23,7 +34,7 @@ pub fn short(long_url: String, env: &Env, database: &DB) -> Option<String> {
Some(String::from_utf8(existing_short.to_vec()).unwrap())
}
None => {
let random_value = random(env.short_length);
let random_value = random(env.short_length, env.short_alphabet.to_owned());
database.put(random_value.as_bytes(), long_url.as_bytes()).unwrap();
database.put(long_url.as_bytes(), random_value.as_bytes()).unwrap();
Some(random_value)
Expand Down

0 comments on commit aa0560b

Please sign in to comment.