Skip to main content

prmt/
module_trait.rs

1use crate::detector::DetectionContext;
2use crate::error::Result;
3use crate::style::Shell;
4use std::env;
5use std::path::{Path, PathBuf};
6use std::sync::Arc;
7
8#[derive(Debug, Clone)]
9pub struct ModuleContext {
10    pub no_version: bool,
11    pub exit_code: Option<i32>,
12    pub detection: DetectionContext,
13    pub shell: Shell,
14    pub stdin_data: Option<Arc<serde_json::Value>>,
15    pub cwd: Option<PathBuf>,
16}
17
18impl Default for ModuleContext {
19    fn default() -> Self {
20        Self {
21            no_version: false,
22            exit_code: None,
23            detection: DetectionContext::default(),
24            shell: Shell::None,
25            stdin_data: None,
26            cwd: env::current_dir().ok(),
27        }
28    }
29}
30
31impl ModuleContext {
32    pub fn marker_path(&self, marker: &str) -> Option<&Path> {
33        self.detection.get(marker)
34    }
35
36    pub fn current_dir(&self) -> Option<&Path> {
37        self.cwd.as_deref()
38    }
39}
40
41pub trait Module: Send + Sync {
42    fn fs_markers(&self) -> &'static [&'static str] {
43        &[]
44    }
45
46    fn is_blocking(&self) -> bool {
47        false
48    }
49
50    fn render(&self, format: &str, context: &ModuleContext) -> Result<Option<String>>;
51}
52
53pub type ModuleRef = Arc<dyn Module>;