artichoke_backend/extn/core/env/
mod.rs1use std::borrow::Cow;
22use std::collections::HashMap;
23
24use spinoso_env::{ArgumentError as EnvArgumentError, Error as EnvError, InvalidError};
25
26use crate::extn::prelude::*;
27
28pub(in crate::extn) mod mruby;
29pub(super) mod trampoline;
30
31#[cfg(not(feature = "core-env-system"))]
32type Backend = spinoso_env::Memory;
33#[cfg(feature = "core-env-system")]
34type Backend = spinoso_env::System;
35
36#[derive(Default, Debug)]
37#[allow(missing_copy_implementations, reason = "not all backends implement `Copy`")]
38pub struct Environ(Backend);
39
40impl Environ {
41 #[must_use]
42 pub fn new() -> Self {
43 Self(Backend::new())
44 }
45
46 pub fn get(&self, name: &[u8]) -> Result<Option<Cow<'_, [u8]>>, Error> {
47 let value = self.0.get(name)?;
48 Ok(value)
49 }
50
51 pub fn put(&mut self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error> {
52 self.0.put(name, value)?;
53 Ok(())
54 }
55
56 pub fn to_map(&self) -> Result<HashMap<Vec<u8>, Vec<u8>>, Error> {
57 let map = self.0.to_map()?;
58 Ok(map)
59 }
60}
61
62impl HeapAllocatedData for Environ {
63 const RUBY_TYPE: &'static str = "Artichoke::Environ";
64}
65
66impl From<EnvArgumentError> for Error {
67 fn from(err: EnvArgumentError) -> Self {
68 ArgumentError::from(err.message()).into()
69 }
70}
71
72impl From<InvalidError> for Error {
73 fn from(err: InvalidError) -> Self {
74 SystemCallError::from(err.into_message()).into()
76 }
77}
78
79impl From<EnvError> for Error {
80 fn from(err: EnvError) -> Self {
81 match err {
82 EnvError::Argument(err) => err.into(),
83 EnvError::Invalid(err) => err.into(),
84 }
85 }
86}