Browse Source

added --bytes option

master
Avril 8 months ago
parent
commit
9cab3e1b98
Signed by: flanchan GPG Key ID: 284488987C31F630
  1. 2
      Cargo.toml
  2. 26
      src/main.rs
  3. 35
      src/parse.rs

2
Cargo.toml

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

26
src/main.rs

@ -4,6 +4,7 @@ extern crate getrandom;
use getrandom::*;
mod parse;
mod r#impl;
fn get<T: Default>() -> Result<T, Error>
@ -45,6 +46,7 @@ fn usage()
println!("Usage: rng --ceil <start> <end> [<repeat>]");
println!("Usage: rng --of <opts...>");
println!("Usage: rng --shuffle <opts...>");
println!("Usage: rng --bytes <number>[k|m|g]");
}
fn range(args: &[String]) -> Result<(), Error>
@ -146,6 +148,29 @@ fn shuffle(args: &[String]) -> Result<(), Error>
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> {
let args: Vec<String> = std::env::args().collect();
@ -161,6 +186,7 @@ fn main() -> Result<(), Error> {
"--floor" => floor(&args[2..])?,
"--of" => of(&args[2..])?,
"--shuffle" => shuffle(&args[2..])?,
"--bytes" => bytes(&args[2..])?,
_ => usage()
};
Ok(())

35
src/parse.rs

@ -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