pdk-classy 1.3.0-alpha.0

PDK Classy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2023 Salesforce, Inc. All rights reserved.
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RandomGeneratorError {
    #[error("Error generating random bytes: {msg:?}")]
    GeneratorError { msg: String },
}

pub fn generate_u32() -> Result<u32, RandomGeneratorError> {
    let mut buf = [0u8; 4];
    getrandom::getrandom(&mut buf)
        .map_err(|e| RandomGeneratorError::GeneratorError { msg: e.to_string() })?;

    Ok(u32::from_be_bytes(buf))
}