Skip to main content

brush_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 shell.
29    ///
30    /// # Arguments
31    ///
32    /// * `error` - The error to format
33    /// * `shell` - The shell context in which the error occurred.
34    fn format_error(
35        &self,
36        error: &error::Error,
37        shell: &Shell<impl extensions::ShellExtensions>,
38    ) -> String {
39        let _ = shell;
40        std::format!("error: {error:#}\n")
41    }
42}
43
44/// Default shell error behavior implementation.
45#[derive(Clone, Default)]
46pub struct DefaultErrorFormatter;
47
48impl ErrorFormatter for DefaultErrorFormatter {}
49
50/// Trait for placeholder behavior (stub for future extension).
51pub trait PlaceholderBehavior: Clone + Default + Send + Sync + 'static {}
52
53/// Default placeholder implementation.
54#[derive(Clone, Default)]
55pub struct DefaultPlaceholder;
56
57impl PlaceholderBehavior for DefaultPlaceholder {}