1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! ENV is a hash-like accessor for environment variables.
//!
//! This module implements the [`ENV`] singleton object from Ruby Core.
//!
//! In Artichoke, the environment variable store is modeled as a hash map of
//! byte vector keys and values, e.g. `HashMap<Vec<u8>, Vec<u8>>`. Backends are
//! expected to convert their internals to this representation in their public
//! APIs. For this reason, all APIs exposed by ENV backends in this crate are
//! fallible.
//!
//! You can use this object in your application by accessing it directly. As a
//! Core API, it is globally available:
//!
//! ```ruby
//! ENV["PATH"]
//! ENV["PS1"] = 'artichoke> '
//! ```
//!
//! [`ENV`]: https://ruby-doc.org/core-3.1.2/ENV.html

use 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)] // not all backends implement `Copy`
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 {
        // TODO: This should be an `Errno::EINVAL`.
        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(),
        }
    }
}