aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/setups
diff options
context:
space:
mode:
Diffstat (limited to 'mingling/src/setups')
-rw-r--r--mingling/src/setups/dirs.rs70
-rw-r--r--mingling/src/setups/exit_code.rs56
2 files changed, 76 insertions, 50 deletions
diff --git a/mingling/src/setups/dirs.rs b/mingling/src/setups/dirs.rs
index ea7f282..65196c2 100644
--- a/mingling/src/setups/dirs.rs
+++ b/mingling/src/setups/dirs.rs
@@ -1,37 +1,53 @@
-// Doc Not Optimize
-use std::marker::PhantomData;
-
-use mingling_core::{ProgramCollect, setup::ProgramSetup};
+use mingling_core::{Program, ProgramCollect, setup::ProgramSetup};
use crate::res::{ResCurrentDir, ResCurrentExe, ResHomeDir, ResTempDir};
-/// Provides the ability to set up commonly used directory resources for the program.
-///
-/// This setup item registers the following directory resources in the program:
-/// - `ResCurrentDir`: Current working directory
-/// - `ResCurrentExe`: Directory containing the executable
-/// - `ResHomeDir`: User's home directory
-/// - `ResTempDir`: Temporary directory
-pub struct DirectoryEnvironmentSetup<C> {
- _collect: PhantomData<C>,
-}
-
-impl<C> Default for DirectoryEnvironmentSetup<C>
-where
- C: ProgramCollect<Enum = C> + 'static,
-{
- fn default() -> Self {
- Self {
- _collect: PhantomData,
- }
- }
-}
+/// `Directory Environment` Setup for managing common directory resources
+///
+/// This Setup registers commonly used directory resources into the program's
+/// resource store. It provides the current working directory, the executable's
+/// directory, the user's home directory, and the system's temporary directory,
+/// so that these paths can be retrieved from the resource store without
+/// recomputing them each time.
+///
+/// # Usage
+///
+/// This Setup can be registered using the
+/// [`Program`](https://docs.rs/mingling/latest/mingling/struct.Program.html)
+/// `with_setup` method, for example:
+///
+/// ```rust
+/// # use mingling::MockProgramCollect as ThisProgram;
+/// use mingling::Program;
+/// use mingling::setup::DirectoryEnvironmentSetup;
+///
+/// let mut program = Program::<ThisProgram>::new();
+/// program.with_setup(DirectoryEnvironmentSetup);
+/// ```
+///
+/// # Behavior
+///
+/// - Registers an [`ResCurrentDir`] resource containing the current working
+/// directory.
+/// - Registers an [`ResCurrentExe`] resource containing the directory of the
+/// currently running executable.
+/// - Registers an [`ResHomeDir`] resource containing the user's home directory.
+/// - Registers an [`ResTempDir`] resource containing the system's temporary
+/// directory.
+///
+/// # Notes
+///
+/// - All directory values are resolved at setup time and stored in the
+/// resource store.
+/// - These resources can be retrieved later using the program's `resource`
+/// accessor with the corresponding resource type.
+pub struct DirectoryEnvironmentSetup;
-impl<C> ProgramSetup<C> for DirectoryEnvironmentSetup<C>
+impl<C> ProgramSetup<C> for DirectoryEnvironmentSetup
where
C: ProgramCollect<Enum = C> + 'static,
{
- fn setup(self, program: &mut crate::Program<C>) {
+ fn setup(self, program: &mut Program<C>) {
program.with_resource(ResCurrentDir::default());
program.with_resource(ResCurrentExe::default());
program.with_resource(ResHomeDir::default());
diff --git a/mingling/src/setups/exit_code.rs b/mingling/src/setups/exit_code.rs
index e31e511..49d5f9f 100644
--- a/mingling/src/setups/exit_code.rs
+++ b/mingling/src/setups/exit_code.rs
@@ -1,8 +1,5 @@
-// Doc Not Optimize
-use std::marker::PhantomData;
-
use mingling_core::{
- ProgramCollect,
+ Program, ProgramCollect,
hook::{ProgramControlUnit, ProgramControls, ProgramHook},
setup::ProgramSetup,
this,
@@ -10,30 +7,43 @@ use mingling_core::{
use crate::res::ResExitCode;
-/// Provides the ability to control the program's exit code, which is returned when the program ends.
+/// `ExitCodeSetup` — Setup for controlling the program's exit code
///
-/// - Use `mingling::update_exit_code` to update the exit code.
-/// - Use `mingling::current_exit_code` to query the current exit code.
-pub struct ExitCodeSetup<C> {
- _collect: PhantomData<C>,
-}
-
-impl<C> Default for ExitCodeSetup<C>
-where
- C: ProgramCollect<Enum = C> + 'static,
-{
- fn default() -> Self {
- Self {
- _collect: PhantomData,
- }
- }
-}
+/// This Setup registers an [`ResExitCode`] resource that tracks the desired exit
+/// code for the program. When the program finishes, a hook reads this resource
+/// and overrides the program's exit code if it has been modified from its
+/// default value of `0`.
+///
+/// # Usage
+///
+/// This Setup can be registered using the
+/// [`Program`](https://docs.rs/mingling/latest/mingling/struct.Program.html)
+/// `with_setup` method, for example:
+///
+/// ```rust
+/// # use mingling::MockProgramCollect as ThisProgram;
+/// use mingling::Program;
+/// use mingling::setup::ExitCodeSetup;
+///
+/// let mut program = Program::<ThisProgram>::new();
+/// program.with_setup(ExitCodeSetup);
+/// ```
+///
+/// # Behavior
+///
+/// - Registers an [`ResExitCode`] resource initialised to `0`.
+/// - Installs a program-finish hook that:
+/// - Reads the current [`ResExitCode`] value.
+/// - Overrides the program's exit code with that value if it is non-zero.
+/// - Leaves the exit code untouched if the resource still holds its default
+/// value of `0`.
+pub struct ExitCodeSetup;
-impl<C> ProgramSetup<C> for ExitCodeSetup<C>
+impl<C> ProgramSetup<C> for ExitCodeSetup
where
C: ProgramCollect<Enum = C> + 'static,
{
- fn setup(self, program: &mut crate::Program<C>) {
+ fn setup(self, program: &mut Program<C>) {
// Insert resource
program.with_resource(ResExitCode { exit_code: 0 });