added --bytes option

master
Avril 4 years ago
parent fff867d1c4
commit 9cab3e1b98
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,6 @@
[package] [package]
name = "rng" name = "rng"
version = "0.1.0" version = "0.2.0"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"

@ -4,6 +4,7 @@ extern crate getrandom;
use getrandom::*; use getrandom::*;
mod parse;
mod r#impl; mod r#impl;
fn get<T: Default>() -> Result<T, Error> fn get<T: Default>() -> Result<T, Error>
@ -45,6 +46,7 @@ fn usage()
println!("Usage: rng --ceil <start> <end> [<repeat>]"); println!("Usage: rng --ceil <start> <end> [<repeat>]");
println!("Usage: rng --of <opts...>"); println!("Usage: rng --of <opts...>");
println!("Usage: rng --shuffle <opts...>"); println!("Usage: rng --shuffle <opts...>");
println!("Usage: rng --bytes <number>[k|m|g]");
} }
fn range(args: &[String]) -> Result<(), Error> fn range(args: &[String]) -> Result<(), Error>
@ -146,6 +148,29 @@ fn shuffle(args: &[String]) -> Result<(), Error>
Ok(()) Ok(())
} }
fn bytes(args: &[String]) -> Result<(), Error>
{
if args.len() < 1 {
usage();
} else {
let num = parse::bytes(&args[0]).expect("Failed to parse number of bytes") as usize;
let mut mem = Vec::with_capacity(num);
debug_assert_eq!(mem.capacity(), num);
// SAFETY: populating uninitialised memory with random data. There are no reads to the uninitialised data.
unsafe {
mem.set_len(num);
populate(&mut mem[..])?;
}
debug_assert_eq!(num, mem.len());
let stdout = std::io::stdout();
use std::io::Write;
stdout.lock().write_all(&mem[..]).expect("write error");
}
Ok(())
}
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@ -161,6 +186,7 @@ fn main() -> Result<(), Error> {
"--floor" => floor(&args[2..])?, "--floor" => floor(&args[2..])?,
"--of" => of(&args[2..])?, "--of" => of(&args[2..])?,
"--shuffle" => shuffle(&args[2..])?, "--shuffle" => shuffle(&args[2..])?,
"--bytes" => bytes(&args[2..])?,
_ => usage() _ => usage()
}; };
Ok(()) Ok(())

@ -0,0 +1,35 @@
use std::{
fmt, error,
};
#[derive(Debug)]
pub struct ReadBytesError(String);
impl error::Error for ReadBytesError{}
impl fmt::Display for ReadBytesError
{
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{:?} is not a valid integer of bytes", self.0)
}
}
/// Parse bytes from a string
pub fn bytes(string: impl AsRef<str>) -> Result<u64, ReadBytesError>
{
let string = string.as_ref();
let string = string.trim();
let last = string.chars().last().ok_or_else(move || ReadBytesError(string.to_owned()))?;
let (sl, mul) = match last {
'k' | 'K' => (1, 1024),
'm' | 'M' => (1, 1024 * 1024),
'g' | 'G' => (1, 1024 * 1024 * 1024),
_ if last.is_numeric() => (0, 1),
_ => return Err(ReadBytesError(string.to_owned())),
};
string[..(string.len()-sl)].parse()
.map(|x: u64| x * mul)
.map_err(move |_| ReadBytesError(string.to_owned()))
}
Loading…
Cancel
Save