conch_runtime_pshaw/
env.rs

1//! This module defines various interfaces and implementations of shell environments.
2//! See the documentation around `Env` or `DefaultEnv` to get started.
3
4use futures_core::future::BoxFuture;
5use std::error::Error;
6
7mod args;
8mod async_io;
9pub mod builtin;
10mod cur_dir;
11mod env_impl;
12mod executable;
13mod fd;
14mod fd_manager;
15mod fd_opener;
16mod func;
17mod last_status;
18mod restorer;
19mod string_wrapper;
20mod var;
21
22pub use self::args::{
23    ArgsEnv, ArgumentsEnvironment, SetArgumentsEnvironment, ShiftArgumentsEnvironment,
24};
25pub use self::async_io::{ArcUnwrappingAsyncIoEnv, AsyncIoEnvironment, TokioAsyncIoEnv};
26pub use self::builtin::{Builtin, BuiltinEnvironment};
27pub use self::cur_dir::{
28    ChangeWorkingDirectoryEnvironment, VirtualWorkingDirEnv, WorkingDirectoryEnvironment,
29};
30pub use self::env_impl::{
31    DefaultEnv, DefaultEnvArc, DefaultEnvConfig, DefaultEnvConfigArc, Env, EnvConfig,
32};
33pub use self::executable::{ExecutableData, ExecutableEnvironment, TokioExecEnv};
34pub use self::fd::{FileDescEnv, FileDescEnvironment};
35pub use self::fd_manager::{
36    FileDescManagerEnv, FileDescManagerEnvironment, TokioFileDescManagerEnv,
37};
38pub use self::fd_opener::{ArcFileDescOpenerEnv, FileDescOpener, FileDescOpenerEnv, Pipe};
39pub use self::func::{
40    FnEnv, FnFrameEnv, FunctionEnvironment, FunctionFrameEnvironment, UnsetFunctionEnvironment,
41};
42pub use self::last_status::{LastStatusEnv, LastStatusEnvironment};
43pub use self::restorer::{EnvRestorer, RedirectEnvRestorer, Restorer, VarEnvRestorer};
44pub use self::string_wrapper::StringWrapper;
45pub use self::var::{
46    ExportedVariableEnvironment, UnsetVariableEnvironment, VarEnv, VariableEnvironment,
47};
48
49/// An interface for checking if the current environment is an interactive one.
50pub trait IsInteractiveEnvironment {
51    /// Indicates if running in interactive mode.
52    fn is_interactive(&self) -> bool;
53}
54
55impl<'a, T: ?Sized + IsInteractiveEnvironment> IsInteractiveEnvironment for &'a T {
56    fn is_interactive(&self) -> bool {
57        (**self).is_interactive()
58    }
59}
60
61/// An interface for reporting arbitrary errors.
62pub trait ReportErrorEnvironment {
63    /// Reports any `Error` as appropriate, e.g. print to stderr.
64    fn report_error<'a>(
65        &mut self,
66        fail: &'a (dyn Error + Sync + Send + 'static),
67    ) -> BoxFuture<'a, ()>;
68}
69
70impl<'b, T: ?Sized + ReportErrorEnvironment> ReportErrorEnvironment for &'b mut T {
71    fn report_error<'a>(
72        &mut self,
73        fail: &'a (dyn Error + Sync + Send + 'static),
74    ) -> BoxFuture<'a, ()> {
75        (**self).report_error(fail)
76    }
77}
78
79/// An interface for all environments that can produce another environment,
80/// identical to itself, but any changes applied to the sub environment will
81/// not be reflected on the parent.
82///
83/// Although this trait is very similar to the `Clone` trait, it is beneficial
84/// for subenvironments to be created as cheaply as possible (in the event that
85/// no changes are made to the subenvironment, there is no need for a deep clone),
86/// without relying on default `Clone` implementations or semantics.
87///
88/// It is strongly encouraged for implementors to utilize clone-on-write smart
89/// pointers or other mechanisms (e.g. `Rc`) to ensure creating and mutating sub
90/// environments is as cheap as possible.
91pub trait SubEnvironment: Sized {
92    /// Create a new sub-environment, which starts out idential to its parent,
93    /// but any changes on the new environment will not be reflected on the parent.
94    fn sub_env(&self) -> Self;
95}