pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub fn slurp<T: AsRef<str>>(argument: T) -> Result<String> {
let destination = argument.as_ref();
let body = if destination.starts_with("http://") || destination.starts_with("https://") {
ureq::get(destination).call()?.into_string()?
} else {
std::fs::read_to_string(destination)?
};
Ok(body)
}
pub mod random {
pub struct Random {
generator: oorandom::Rand64,
}
impl Default for Random {
fn default() -> Self {
let mut buffer = [0u8; 32];
getrandom::getrandom(&mut buffer).unwrap_or(());
let seed = buffer.iter().enumerate().fold(0, |acc: u32, (i, x)| {
let value = if x > &127 { 2u32.pow(i as u32) } else { 0 };
acc + value
});
Self {
generator: oorandom::Rand64::new(seed as u128),
}
}
}
impl Random {
pub fn new() -> Self {
Self::default()
}
pub fn with_seed(self, seed: u128) -> Self {
Self {
generator: oorandom::Rand64::new(seed),
}
}
pub fn rand_u64(&mut self) -> u64 {
self.generator.rand_u64()
}
pub fn rand_i64(&mut self) -> i64 {
self.generator.rand_i64()
}
pub fn rand_float(&mut self) -> f64 {
self.generator.rand_float()
}
pub fn rand_range(&mut self, range: std::ops::Range<u64>) -> u64 {
self.generator.rand_range(range)
}
}
}
pub mod prelude {
pub use super::{slurp, Result, random::Random};
pub use itertools::{self, Itertools};
pub use regex::{self, Regex};
pub use ureq as http;
}