knarkzel 0.3.0

Useful functions and re-exports missing in std
Documentation
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(());
            // parse array of 32 u8s as 32 bit number, upper limit 4294967295
            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)
        }
    }
}

/// `use knarkzel::prelude::*;`
pub mod prelude {
/*!
# Http
    
The `slurp` function can be used for either reading http
    
```rust
let body = slurp("https://example.com")?;
```

# Filesystem
    
...or for reading a file.

```rust
let columns = slurp("mock.csv")?;
```
    
For more complex stuff such as setting headers, use the `http` module.

```rust
let body = http::post("https://example.com")
    .set("Example-Header", "header value")
    .call()?
    .into_string()?;
```

# Regex

```rust
let text = "Not my favorite movie: 'Citizen Kane' (1941).";
let regex = Regex::new(r"'([^']+)'\s+\((\d{4})\)")?;
let captures = regex.captures(text)?;

assert_eq!(&captures[0], "'Citizen Kane' (1941)");
assert_eq!(&captures[1], "Citizen Kane");
assert_eq!(&captures[2], "1941");
```
    
# Random

```rust
let random = Random::new().with_seed(1234);
let unsigned = random.rand_u64();
let signed = random.rand_i64();
let float = random.rand_float();
let range = random.rand_range(1..=100);
```
*/
    pub use super::{slurp, Result, random::Random};
    pub use itertools::{self, Itertools};
    pub use regex::{self, Regex};
    pub use ureq as http;
}