use crate::ProcessContext;
use alloc::{string::String, vec::Vec};
use std::{
env::{args, vars},
sync::LazyLock,
};
pub struct OsProcessContext {
arguments: LazyLock<Vec<String>>,
environment_variables: LazyLock<Vec<(String, String)>>,
argument_skip: usize,
}
impl OsProcessContext {
pub fn new() -> Self {
Self {
arguments: LazyLock::new(|| args().collect()),
environment_variables: LazyLock::new(|| vars().collect()),
argument_skip: 0,
}
}
pub fn with_argument_skip(self, skip: usize) -> Self {
Self {
argument_skip: skip,
..self
}
}
}
impl ProcessContext for OsProcessContext {
fn command_line_rev(&self) -> impl IntoIterator<Item = &str> {
(*self.arguments)
.iter()
.skip(self.argument_skip)
.map(AsRef::as_ref)
.rev()
}
fn environment_variables(&self) -> impl IntoIterator<Item = (&str, &str)> {
(*self.environment_variables)
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref()))
}
}
impl Default for OsProcessContext {
fn default() -> Self {
Self::new()
}
}