Skip to main content

shrub_core/
extensions.rs

1//! Definition of shell behavior traits and defaults.
2
3use crate::{Shell, error, extensions};
4
5/// Trait for static shell extensions. Collects all associated types needed to
6/// instantiate a shell into a single containing struct.
7pub trait ShellExtensions: Clone + Default + Send + Sync + 'static {
8    /// Type of the error behavior implementation.
9    type ErrorFormatter: ErrorFormatter;
10}
11
12/// Shell extensions implementation constructed from component types.
13#[derive(Clone, Default)]
14pub struct ShellExtensionsImpl<EF: ErrorFormatter = DefaultErrorFormatter> {
15    _marker: std::marker::PhantomData<EF>,
16}
17
18impl<EF: ErrorFormatter> ShellExtensions for ShellExtensionsImpl<EF> {
19    type ErrorFormatter = EF;
20}
21
22/// Default shell extensions implementation.
23/// This is a type alias for the most common shell configuration.
24pub type DefaultShellExtensions = ShellExtensionsImpl<DefaultErrorFormatter>;
25
26/// Trait for defining shell error behaviors.
27pub trait ErrorFormatter: Clone + Default + Send + Sync + 'static {
28    /// Format the given error for display within the context of the provided
29    /// shell.
30    ///
31    /// # Arguments
32    ///
33    /// * `error` - The error to format
34    /// * `shell` - The shell context in which the error occurred.
35    fn format_error(
36        &self,
37        error: &error::Error,
38        shell: &Shell<impl extensions::ShellExtensions>,
39    ) -> String {
40        let _ = shell;
41        std::format!("error: {error:#}\n")
42    }
43}
44
45/// Default shell error behavior implementation.
46#[derive(Clone, Default)]
47pub struct DefaultErrorFormatter;
48
49impl ErrorFormatter for DefaultErrorFormatter {}
50
51/// Trait for placeholder behavior (stub for future extension).
52pub trait PlaceholderBehavior: Clone + Default + Send + Sync + 'static {}
53
54/// Default placeholder implementation.
55#[derive(Clone, Default)]
56pub struct DefaultPlaceholder;
57
58impl PlaceholderBehavior for DefaultPlaceholder {}