cstats_hook/
lib.rs

1//! # cstats-hook
2//!
3//! Hook generation library for shell integration with cstats.
4//! Provides templates and utilities for generating shell hooks that enable
5//! automatic statistics collection for command executions.
6
7pub mod error;
8pub mod generator;
9pub mod templates;
10
11pub use error::{Error, Result};
12pub use generator::HookGenerator;
13
14/// Supported shell types for hook generation
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ShellType {
17    /// Bash shell
18    Bash,
19    /// Zsh shell  
20    Zsh,
21    /// Fish shell
22    Fish,
23    /// PowerShell
24    PowerShell,
25}
26
27impl std::fmt::Display for ShellType {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            ShellType::Bash => write!(f, "bash"),
31            ShellType::Zsh => write!(f, "zsh"),
32            ShellType::Fish => write!(f, "fish"),
33            ShellType::PowerShell => write!(f, "powershell"),
34        }
35    }
36}
37
38impl std::str::FromStr for ShellType {
39    type Err = Error;
40
41    fn from_str(s: &str) -> Result<Self> {
42        match s.to_lowercase().as_str() {
43            "bash" => Ok(ShellType::Bash),
44            "zsh" => Ok(ShellType::Zsh),
45            "fish" => Ok(ShellType::Fish),
46            "powershell" | "pwsh" => Ok(ShellType::PowerShell),
47            _ => Err(Error::UnsupportedShell(s.to_string())),
48        }
49    }
50}
51
52/// Hook configuration options
53#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
54pub struct HookConfig {
55    /// Shell type for the hook
56    pub shell_type: String,
57
58    /// Commands to automatically wrap with cstats collection
59    pub auto_commands: Vec<String>,
60
61    /// Default source identifier for the shell
62    pub default_source: Option<String>,
63
64    /// Enable automatic collection for all commands
65    pub auto_collect_all: bool,
66
67    /// Custom cstats binary path
68    pub cstats_path: Option<String>,
69
70    /// Additional environment variables to set
71    pub env_vars: std::collections::HashMap<String, String>,
72}
73
74impl Default for HookConfig {
75    fn default() -> Self {
76        Self {
77            shell_type: "bash".to_string(),
78            auto_commands: vec![
79                "git".to_string(),
80                "cargo".to_string(),
81                "make".to_string(),
82                "npm".to_string(),
83                "yarn".to_string(),
84            ],
85            default_source: None,
86            auto_collect_all: false,
87            cstats_path: None,
88            env_vars: std::collections::HashMap::new(),
89        }
90    }
91}