added uppercase variations for letter and alphanumeric generation symbols and removed --upper flag
This commit is contained in:
parent
32734cc91c
commit
0d9519c0db
3 changed files with 20 additions and 21 deletions
|
|
@ -77,7 +77,9 @@ Generates random codes based on a user-defined pattern:
|
||||||
|
|
||||||
- `n`: Number (0-9)
|
- `n`: Number (0-9)
|
||||||
- `l`: Letter (a-z)
|
- `l`: Letter (a-z)
|
||||||
|
- `L`: Uppercase Letter (A-Z)
|
||||||
- `a`: Alphanumeric (0-9, a-z)
|
- `a`: Alphanumeric (0-9, a-z)
|
||||||
|
- `A`: Uppercase Alphanumeric (0-9, a-z)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ dolltool -g "nn-lll" -c 3
|
$ dolltool -g "nn-lll" -c 3
|
||||||
|
|
|
||||||
16
src/main.rs
16
src/main.rs
|
|
@ -40,26 +40,22 @@ struct Args {
|
||||||
/// Decodes the following dollcode into an integer or text
|
/// Decodes the following dollcode into an integer or text
|
||||||
decode: Option<String>,
|
decode: Option<String>,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = false)]
|
|
||||||
/// Specifies the encoding/decoding method as text, not as an integer
|
|
||||||
text: bool,
|
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
/// Generates codes from a user-defined pattern (n = number; l = letter; a = alphanumeric)
|
/// Generates codes from a user-defined pattern (n = number; l = letter; L = uppercase letter; a = alphanumeric; A = uppercase alphanumeric)
|
||||||
generate: Option<String>,
|
generate: Option<String>,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = 1)]
|
#[arg(short, long, default_value_t = 1)]
|
||||||
/// Specifies how many codes to generate
|
/// Specifies how many codes to generate
|
||||||
count: usize,
|
count: usize,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = false)]
|
|
||||||
/// Outputs generated letters as uppercase as opposed to lowercase
|
|
||||||
upper: bool,
|
|
||||||
|
|
||||||
#[arg(long, default_value_t = false)]
|
#[arg(long, default_value_t = false)]
|
||||||
/// Will generate codes without encoding it into dollcode
|
/// Will generate codes without encoding it into dollcode
|
||||||
no_doll: bool,
|
no_doll: bool,
|
||||||
|
|
||||||
|
#[arg(short, long, default_value_t = false)]
|
||||||
|
/// Specifies the encoding/decoding method as text, not as an integer
|
||||||
|
text: bool,
|
||||||
|
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
/// Display the license for dolltool
|
/// Display the license for dolltool
|
||||||
license: bool,
|
license: bool,
|
||||||
|
|
@ -75,7 +71,7 @@ fn main() {
|
||||||
|
|
||||||
if let Some(pattern) = args.generate {
|
if let Some(pattern) = args.generate {
|
||||||
for _ in 0..args.count {
|
for _ in 0..args.count {
|
||||||
let generated = generate_from_pattern(&pattern, args.upper);
|
let generated = generate_from_pattern(&pattern);
|
||||||
|
|
||||||
if args.no_doll {
|
if args.no_doll {
|
||||||
println!("{}", generated);
|
println!("{}", generated);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
|
|
||||||
pub fn generate_from_pattern(pattern: &str, upper: bool) -> String {
|
pub fn generate_from_pattern(pattern: &str) -> String {
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
|
|
||||||
|
|
@ -9,18 +9,19 @@ pub fn generate_from_pattern(pattern: &str, upper: bool) -> String {
|
||||||
'n' => output.push(rng.random_range('0'..='9')),
|
'n' => output.push(rng.random_range('0'..='9')),
|
||||||
'l' => {
|
'l' => {
|
||||||
let letter = rng.random_range('a'..='z');
|
let letter = rng.random_range('a'..='z');
|
||||||
output.push(if upper {
|
output.push(letter);
|
||||||
letter.to_ascii_uppercase()
|
}
|
||||||
} else {
|
'L' => {
|
||||||
letter
|
let letter = rng.random_range('A'..='Z');
|
||||||
});
|
output.push(letter);
|
||||||
}
|
}
|
||||||
'a' => {
|
'a' => {
|
||||||
let pool = if upper {
|
let pool = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
let idx = rng.random_range(0..pool.len());
|
||||||
} else {
|
output.push(pool.chars().nth(idx).unwrap());
|
||||||
"0123456789abcdefghijklmnopqrstuvwxyz"
|
}
|
||||||
};
|
'A' => {
|
||||||
|
let pool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
let idx = rng.random_range(0..pool.len());
|
let idx = rng.random_range(0..pool.len());
|
||||||
output.push(pool.chars().nth(idx).unwrap());
|
output.push(pool.chars().nth(idx).unwrap());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue