pub struct System { /* private fields */ }
Expand description
A hash-like accessor for environment variables using platform APIs.
System
is an accessor to the host system’s environment variables using the
functions provided by the Rust Standard Library in the
std::env
module.
Use of this ENV
backend allows Ruby code to access and modify the host
system. It is not appropriate to use this backend in embedded or untrusted
contexts.
§Examples
Fetching an environment variable:
const ENV: System = System::new();
assert!(ENV.get(b"PATH")?.is_some());
Setting an environment variable:
const ENV: System = System::new();
ENV.put(b"ENV_BACKEND", Some(b"spinoso_env::System"))?;
assert_eq!(
std::env::var("ENV_BACKEND").as_deref(),
Ok("spinoso_env::System")
);
Implementations§
Source§impl System
impl System
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, default ENV System
backend.
The resulting environment variable accessor has access to the host system via platform APIs.
§Examples
const ENV: System = System::new();
Sourcepub fn get(
self,
name: &[u8],
) -> Result<Option<Cow<'static, [u8]>>, ArgumentError>
pub fn get( self, name: &[u8], ) -> Result<Option<Cow<'static, [u8]>>, ArgumentError>
Retrieves the value for environment variable name
.
Returns None
if the named variable does not exist. If the retrieved
environment variable value cannot be converted from a platform string
to a byte vector, None
is returned.
§Implementation notes
This method accesses the host system’s environment using env::var_os
.
§Examples
const ENV: System = System::new();
assert!(ENV.get(b"PATH")?.is_some());
§Errors
If name
contains a NUL byte, e.g. b'\0'
, an error is returned.
If the environment variable name or value cannot be converted from a byte vector to a platform string, an error is returned.
Sourcepub fn put(self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error>
pub fn put(self, name: &[u8], value: Option<&[u8]>) -> Result<(), Error>
Sets the environment variable name
to value
.
If the value given is None
the environment variable is deleted.
§Implementation notes
This method accesses the host system’s environment using env::set_var
and env::remove_var
.
§Examples
const ENV: System = System::new();
ENV.put(b"RUBY", Some(b"Artichoke"))?;
assert_eq!(ENV.get(b"RUBY")?.as_deref(), Some(&b"Artichoke"[..]));
ENV.put(b"RUBY", None)?;
assert_eq!(ENV.get(b"RUBY")?, None);
§Errors
If name
contains a NUL byte, e.g. b'\0'
, an argument error is
returned.
If name
contains an ‘=’ byte, e.g. b'='
, an EINVAL
error is
returned.
If value
is Some
and contains a NUL byte, e.g. b'\0'
, an
argument error is returned.
If the environment variable name or value cannot be converted from a byte vector to a platform string, an error is returned.
Sourcepub fn to_map(self) -> Result<HashMap<Vec<u8>, Vec<u8>>, ArgumentError>
pub fn to_map(self) -> Result<HashMap<Vec<u8>, Vec<u8>>, ArgumentError>
Serialize the environ to a HashMap
.
Map keys are environment variable names and map values are environment variable values.
§Implementation notes
This method accesses the host system’s environment using env::vars_os
.
§Examples
const ENV: System = System::new();
let map = ENV.to_map()?;
assert!(map.contains_key(&b"PATH"[..]));
§Errors
If any environment variable name or value cannot be converted from a platform string to a byte vector, an error is returned.