diff src/environ.rs @ 100:3f11b8d30f63

Implement environment variable management. This actually wires up the environment variable handling to libpam, so that applications and modules can manage the environment through the authentication process.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 17:08:01 -0400
parents b87100c5eed4
children
line wrap: on
line diff
--- a/src/environ.rs	Tue Jun 24 14:54:47 2025 -0400
+++ b/src/environ.rs	Tue Jun 24 17:08:01 2025 -0400
@@ -3,26 +3,31 @@
 //! PAM modules can set environment variables to apply to a user session.
 //! This module manages the interface for doing all of that.
 
+use crate::constants::Result;
 use std::ffi::{OsStr, OsString};
 
 /// A key/value map for environment variables, as [`OsString`]s.
 ///
 /// This is a limited subset of what [`HashMap`](std::collections::HashMap)
 /// can do. Notably, we do *not* support mutable iteration.
-pub trait EnvironMap {
+pub trait EnvironMap<'a> {
     /// Gets the environment variable of the given key.
-    fn get(&self, val: &OsStr) -> Option<&OsStr>;
+    fn get(&self, key: impl AsRef<OsStr>) -> Option<OsString>;
 
     /// Iterates over the key/value pairs of the environment.
-    fn iter(&self) -> impl Iterator<Item = (&OsStr, &OsStr)>;
+    fn iter(&self) -> Result<impl Iterator<Item = (OsString, OsString)>>;
 }
 
-pub trait EnvironMapMut: EnvironMap {
+pub trait EnvironMapMut<'a>: EnvironMap<'a> {
     /// Sets the environment variable with the given key,
     /// returning the old one if present.
-    fn insert(&mut self, key: &OsStr, val: &OsStr) -> Option<OsString>;
+    fn insert(
+        &mut self,
+        key: impl AsRef<OsStr>,
+        val: impl AsRef<OsStr>,
+    ) -> Result<Option<OsString>>;
 
     /// Removes the environment variable from the map, returning its old value
     /// if present.
-    fn remove(&mut self, key: &OsStr) -> Option<OsString>;
+    fn remove(&mut self, key: impl AsRef<OsStr>) -> Result<Option<OsString>>;
 }