Skip to main content

Env

Struct Env 

Source
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: PathBuf

The logical $HOME, as the player’s shell sees it.

§user: String

The 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: PathBuf

The 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: bool

True 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

Source

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.

Source

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.

Source

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§

Source§

impl Clone for Env

Source§

fn clone(&self) -> Env

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Env

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Env

Source§

impl PartialEq for Env

Source§

fn eq(&self, other: &Env) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.