Skip to main content

fyi_core/init/
options.rs

1//! Initialization options and result types.
2//!
3//! This module contains the configuration structs used for initialization:
4//! - [`InitOptions`]: Options for controlling initialization behavior
5//! - [`InstallSettingsOptions`]: Options for settings installation
6//! - [`InitResult`]: Result of a successful initialization
7//! - [`InitError`]: Error types for initialization operations
8
9use std::path::PathBuf;
10
11use super::otel::DEFAULT_OTEL_PORT;
12
13/// Options for core initialization.
14///
15/// This struct contains all the options needed to initialize fyi programmatically.
16/// For CLI usage, the CLI layer adds additional options like `print` and `db_only`.
17///
18/// This struct is the single source of truth for initialization settings, shared
19/// between CLI commands (`init`, `activate`) and programmatic API usage.
20#[derive(Debug, Clone)]
21pub struct InitOptions {
22    /// Frameworks to install hooks for (e.g., "claude", "gemini", "codex").
23    /// If empty, auto-detection will be used (CLI behavior).
24    #[deprecated(since = "0.2.0", note = "use `frameworks` field instead")]
25    pub framework: String,
26    /// Frameworks to install hooks for (e.g., `["claude", "gemini", "codex"]`).
27    /// If empty, auto-detection will be used (CLI behavior).
28    pub frameworks: Vec<String>,
29    /// Install hooks to project config instead of global.
30    pub local: bool,
31    /// Install hooks to project local config (not committed to git).
32    pub settings_local: bool,
33    /// Configure OpenTelemetry env vars for token tracking.
34    pub otel: bool,
35    /// Port for OTel server.
36    pub otel_port: u16,
37    /// Remove OTel configuration (disables token tracking).
38    pub remove_otel: bool,
39}
40
41impl Default for InitOptions {
42    fn default() -> Self {
43        #[expect(deprecated)]
44        Self {
45            framework: String::new(),
46            frameworks: vec![],
47            local: false,
48            settings_local: false,
49            otel: false,
50            otel_port: DEFAULT_OTEL_PORT,
51            remove_otel: false,
52        }
53    }
54}
55
56impl InitOptions {
57    /// Create new InitOptions with default values.
58    pub fn new() -> Self {
59        Self::default()
60    }
61
62    /// Create InitOptions for a single framework.
63    pub fn for_framework(framework: impl Into<String>) -> Self {
64        let framework = framework.into();
65        #[expect(deprecated)]
66        Self {
67            framework: framework.clone(),
68            frameworks: vec![framework],
69            ..Default::default()
70        }
71    }
72
73    /// Create InitOptions for multiple frameworks.
74    pub fn for_frameworks(frameworks: Vec<String>) -> Self {
75        #[expect(deprecated)]
76        Self {
77            framework: frameworks.first().cloned().unwrap_or_default(),
78            frameworks,
79            ..Default::default()
80        }
81    }
82
83    /// Get the list of frameworks to initialize.
84    /// Returns frameworks if non-empty, otherwise returns a single-element vec with framework.
85    pub fn get_frameworks(&self) -> Vec<String> {
86        if self.frameworks.is_empty() {
87            #[expect(deprecated)]
88            if self.framework.is_empty() {
89                vec![]
90            } else {
91                vec![self.framework.clone()]
92            }
93        } else {
94            self.frameworks.clone()
95        }
96    }
97
98    /// Set install location to project config instead of global.
99    pub fn local(mut self, local: bool) -> Self {
100        self.local = local;
101        self
102    }
103
104    /// Set install location to project local config (not committed to git).
105    pub fn settings_local(mut self, settings_local: bool) -> Self {
106        self.settings_local = settings_local;
107        self
108    }
109
110    /// Enable OpenTelemetry configuration for token tracking.
111    pub fn otel(mut self, otel: bool) -> Self {
112        self.otel = otel;
113        self
114    }
115
116    /// Set the port for OTel server.
117    pub fn otel_port(mut self, port: u16) -> Self {
118        self.otel_port = port;
119        self
120    }
121
122    /// Remove OTel configuration (disables token tracking).
123    pub fn remove_otel(mut self, remove: bool) -> Self {
124        self.remove_otel = remove;
125        self
126    }
127}
128
129/// Options for installing settings.
130#[derive(Debug, Clone, Default)]
131pub struct InstallSettingsOptions {
132    /// OTel environment variables to add.
133    pub otel_env: Option<serde_json::Value>,
134    /// Whether to remove existing OTel configuration.
135    pub remove_otel: bool,
136}
137
138/// Result of initialization.
139///
140/// Contains information about what was initialized and where.
141#[derive(Debug, Clone)]
142pub struct InitResult {
143    /// Path to the database (if database initialization was requested).
144    pub db_path: Option<PathBuf>,
145    /// Path where settings/hooks were installed.
146    pub settings_path: PathBuf,
147    /// The hooks configuration that was generated.
148    pub hooks_config: serde_json::Value,
149}
150
151/// Error types for initialization operations.
152#[derive(Debug, thiserror::Error)]
153pub enum InitError {
154    /// Unknown framework name.
155    #[error("unknown framework: {0}")]
156    UnknownFramework(String),
157
158    /// Failed to determine settings path.
159    #[error("failed to determine settings path: {0}")]
160    SettingsPath(String),
161
162    /// Failed to read or write settings file.
163    #[error("settings file error: {0}")]
164    SettingsFile(#[from] std::io::Error),
165
166    /// Failed to parse or serialize configuration.
167    #[error("configuration error: {0}")]
168    Config(String),
169}
170
171#[cfg(test)]
172mod tests {
173    use super::*;
174
175    #[test]
176    #[expect(deprecated)]
177    fn test_init_options_default() {
178        let opts = InitOptions::default();
179        // Default has empty framework/frameworks, relies on CLI or for_framework()
180        assert_eq!(opts.framework, "");
181        assert!(opts.frameworks.is_empty());
182        assert!(!opts.local);
183        assert!(!opts.settings_local);
184        assert!(!opts.otel);
185        // otel_port defaults to 4318 (standard OTel HTTP/JSON port)
186        assert_eq!(opts.otel_port, 4318);
187        assert!(!opts.remove_otel);
188    }
189
190    #[test]
191    fn test_init_options_for_framework() {
192        let opts = InitOptions::for_framework("claude");
193        assert_eq!(opts.get_frameworks(), vec!["claude"]);
194        assert!(!opts.local);
195        assert!(!opts.otel);
196    }
197
198    #[test]
199    fn test_init_options_for_frameworks() {
200        let opts = InitOptions::for_frameworks(vec!["claude".into(), "gemini".into()]);
201        assert_eq!(opts.get_frameworks(), vec!["claude", "gemini"]);
202    }
203
204    #[test]
205    fn test_init_options_builder() {
206        let opts = InitOptions::for_framework("claude")
207            .local(true)
208            .otel(true)
209            .otel_port(9999);
210        assert_eq!(opts.get_frameworks(), vec!["claude"]);
211        assert!(opts.local);
212        assert!(opts.otel);
213        assert_eq!(opts.otel_port, 9999);
214    }
215}