artichoke_backend/extn/core/env/
mod.rsuse std::borrow::Cow;
use std::collections::HashMap;
use spinoso_env::{ArgumentError as EnvArgumentError, Error as EnvError, InvalidError};
use crate::extn::prelude::*;
pub(in crate::extn) mod mruby;
pub(super) mod trampoline;
#[cfg(not(feature = "core-env-system"))]
type Backend = spinoso_env::Memory;
#[cfg(feature = "core-env-system")]
type Backend = spinoso_env::System;
#[derive(Default, Debug)]
#[allow(missing_copy_implementations)] pub struct Environ(Backend);
impl Environ {
#[must_use]
pub fn new() -> Self {
Self(Backend::new())
}
pub fn get(&self, name: &[u8]) -> Result<Option<Cow<'_, [u8]>>, Error> {
let value = self.0.get(name)?;
Ok(value)
}
pub fn put(&mut self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error> {
self.0.put(name, value)?;
Ok(())
}
pub fn to_map(&self) -> Result<HashMap<Vec<u8>, Vec<u8>>, Error> {
let map = self.0.to_map()?;
Ok(map)
}
}
impl HeapAllocatedData for Environ {
const RUBY_TYPE: &'static str = "Artichoke::Environ";
}
impl From<EnvArgumentError> for Error {
fn from(err: EnvArgumentError) -> Self {
ArgumentError::from(err.message()).into()
}
}
impl From<InvalidError> for Error {
fn from(err: InvalidError) -> Self {
SystemCallError::from(err.into_message()).into()
}
}
impl From<EnvError> for Error {
fn from(err: EnvError) -> Self {
match err {
EnvError::Argument(err) => err.into(),
EnvError::Invalid(err) => err.into(),
}
}
}