pub struct Env {
pub home: PathBuf,
pub user: String,
pub cwd: Option<PathBuf>,
pub state_dir: PathBuf,
pub workspace_override: Option<OsString>,
pub wrapped: bool,
pub tmpdir: Option<PathBuf>,
pub shell: Option<OsString>,
pub force_shell: Option<String>,
pub update_url: Option<String>,
pub exe: Option<PathBuf>,
}Expand description
What the game knows about the process it runs in. Built once by Env::from_process and passed
down by reference; tests build one with Env::new or a struct literal.
use shellgame::env::Env;
let env = Env {
wrapped: true,
..Env::new("/home/ada", "ada", "/home/ada/.config/shellgame")
};
assert_eq!(env.user, "ada");
assert!(env.wrapped);
assert_eq!(env.cwd, None);Fields§
§home: PathBufThe logical $HOME, as the player’s shell sees it.
user: StringThe player’s name: $USER, or player.
cwd: Option<PathBuf>The player’s working directory, canonical, or None when it no longer exists (the player
deleted the directory they were standing in).
state_dir: PathBufThe directory that holds state.json.
workspace_override: Option<OsString>$SHELLGAME_WORKSPACE: the workspace of a new game. A running game keeps the workspace
saved in its state.
wrapped: boolTrue when $SHELLGAME_WRAPPER is set: the binary runs inside the game shell, which replays
its protocol directives.
tmpdir: Option<PathBuf>$TMPDIR. shellgame remove deletes workspaces below /tmp and below this directory.
shell: Option<OsString>$SHELL, the last resort of shell detection.
force_shell: Option<String>$SHELLGAME_FORCE_SHELL: bash or fish (anything else is ignored by shell detection).
update_url: Option<String>$SHELLGAME_UPDATE_URL: where shellgame update downloads the release archive from.
exe: Option<PathBuf>This binary, canonical (current_exe may report a symlink path on macOS). None when the
system cannot tell.
Implementations§
Source§impl Env
impl Env
Sourcepub fn new(
home: impl Into<PathBuf>,
user: impl Into<String>,
state_dir: impl Into<PathBuf>,
) -> Self
pub fn new( home: impl Into<PathBuf>, user: impl Into<String>, state_dir: impl Into<PathBuf>, ) -> Self
An environment with the given home, user and state directory, outside the game shell, with
no working directory and every optional knob unset. Meant for tests; struct update syntax
(Env { wrapped: true, ..Env::new(..) }) adjusts the rest.
Sourcepub fn from_process() -> Self
pub fn from_process() -> Self
Gather the environment of this process: the variables, the working directory and the path
of this binary. Called once, from main.
Sourcepub fn from_vars(
lookup: impl Fn(&str) -> Option<OsString>,
cwd: Option<PathBuf>,
exe: Option<PathBuf>,
) -> Self
pub fn from_vars( lookup: impl Fn(&str) -> Option<OsString>, cwd: Option<PathBuf>, exe: Option<PathBuf>, ) -> Self
Build an environment from a variable lookup and the two facts that are not variables.
lookup returns the value of a variable; empty values are treated as unset here, so a
lookup can simply wrap std::env::var_os. Without HOME the home directory is /.
use std::ffi::OsString;
use shellgame::env::Env;
let vars = |name: &str| match name {
"HOME" => Some(OsString::from("/home/ada")),
"XDG_CONFIG_HOME" => Some(OsString::from("/home/ada/cfg")),
"SHELLGAME_WRAPPER" => Some(OsString::from("1")),
"USER" => Some(OsString::new()), // empty counts as unset
_ => None,
};
let env = Env::from_vars(vars, None, None);
assert_eq!(env.user, "player");
assert_eq!(env.state_dir, std::path::Path::new("/home/ada/cfg/shellgame"));
assert!(env.wrapped);Trait Implementations§
impl Eq for Env
impl StructuralPartialEq for Env
Auto Trait Implementations§
impl Freeze for Env
impl RefUnwindSafe for Env
impl Send for Env
impl Sync for Env
impl Unpin for Env
impl UnsafeUnpin for Env
impl UnwindSafe for Env
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.