artichoke_backend/extn/stdlib/securerandom/
mod.rsuse crate::convert::implicitly_convert_to_int;
use crate::extn::prelude::*;
pub(in crate::extn) mod mruby;
pub(super) mod trampoline;
#[doc(inline)]
pub use spinoso_securerandom::{
alphanumeric, base64, hex, random_bytes, random_number, urlsafe_base64, uuid, ArgumentError, DomainError,
Error as SecureRandomError, Max, Rand, RandomBytesError, SecureRandom,
};
impl From<SecureRandomError> for Error {
fn from(err: SecureRandomError) -> Self {
match err {
SecureRandomError::Argument(err) => err.into(),
SecureRandomError::RandomBytes(err) => err.into(),
SecureRandomError::Memory(_) => NoMemoryError::with_message("out of memory").into(),
}
}
}
impl From<ArgumentError> for Error {
fn from(err: ArgumentError) -> Self {
spinoso_exception::ArgumentError::from(err.message()).into()
}
}
impl From<RandomBytesError> for Error {
fn from(err: RandomBytesError) -> Self {
RuntimeError::from(err.message()).into()
}
}
impl From<DomainError> for Error {
fn from(err: DomainError) -> Self {
ArgumentError::from(err.message()).into()
}
}
impl TryConvertMut<Value, Max> for Artichoke {
type Error = Error;
fn try_convert_mut(&mut self, max: Value) -> Result<Max, Self::Error> {
let optional: Option<Value> = self.try_convert(max)?;
self.try_convert_mut(optional)
}
}
impl TryConvertMut<Option<Value>, Max> for Artichoke {
type Error = Error;
fn try_convert_mut(&mut self, max: Option<Value>) -> Result<Max, Self::Error> {
if let Some(max) = max {
match max.ruby_type() {
Ruby::Fixnum => {
let max = max.try_convert_into(self)?;
Ok(Max::Integer(max))
}
Ruby::Float => {
let max = max.try_convert_into(self)?;
Ok(Max::Float(max))
}
_ => {
let max = implicitly_convert_to_int(self, max).map_err(|_| {
let mut message = b"invalid argument - ".to_vec();
message.extend_from_slice(max.inspect(self).as_slice());
spinoso_exception::ArgumentError::from(message)
})?;
Ok(Max::Integer(max))
}
}
} else {
Ok(Max::None)
}
}
}
impl ConvertMut<Rand, Value> for Artichoke {
fn convert_mut(&mut self, from: Rand) -> Value {
match from {
Rand::Integer(num) => self.convert(num),
Rand::Float(num) => self.convert_mut(num),
}
}
}