eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Preset configuration management for YahooConnector.
//!
//! This module provides functionality to load and save preset configurations,
//! allowing users to define and reuse custom connector settings.

use crate::YahooError;
use crate::builder::YahooConnectorBuilder;
use crate::enterprise::EnterpriseConfig;
use crate::{
    circuit_breaker::CircuitBreakerConfig, connection_pool::ConnectionPoolConfig,
    observability::ObservabilityConfig, rate_limiter::RateLimitConfig,
    request_deduplication::DeduplicationConfig, response_cache::ResponseCacheConfig,
    retry::RetryConfig,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// A preset configuration that can be loaded or saved.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresetConfig {
    /// Name of the preset
    pub name: String,

    /// Description of the preset
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Requests per second allowed
    pub rate_limit: f64,

    /// Number of failures before circuit breaker opens
    pub circuit_breaker_threshold: u32,

    /// Time window for counting failures (seconds)
    pub circuit_breaker_window_secs: u64,

    /// How long circuit breaker stays open (seconds)
    pub circuit_breaker_timeout_secs: u64,

    /// Number of retry attempts
    pub retry_attempts: u32,

    /// Initial delay between retries (milliseconds)
    pub retry_initial_delay_ms: u64,

    /// Maximum delay between retries (milliseconds)
    pub retry_max_delay_ms: u64,

    /// Request timeout (seconds)
    pub timeout_secs: u64,

    /// Maximum number of cache entries
    pub cache_size: usize,

    /// Cache entry time-to-live (seconds)
    pub cache_duration_secs: u64,

    /// Maximum concurrent connections in pool
    pub connection_pool_max: usize,

    /// Enable verbose logging
    pub verbose_logging: bool,

    /// Enable request metrics
    pub enable_metrics: bool,

    /// Enable distributed tracing
    pub enable_tracing: bool,
}

impl From<&YahooConnectorBuilder> for PresetConfig {
    fn from(builder: &YahooConnectorBuilder) -> Self {
        Self {
            name: "custom".to_string(),
            description: None,
            rate_limit: builder.rate_limit,
            circuit_breaker_threshold: builder.circuit_breaker_threshold,
            circuit_breaker_window_secs: builder.circuit_breaker_window_secs,
            circuit_breaker_timeout_secs: builder.circuit_breaker_timeout_secs,
            retry_attempts: builder.retry_attempts,
            retry_initial_delay_ms: builder.retry_initial_delay_ms,
            retry_max_delay_ms: builder.retry_max_delay_ms,
            timeout_secs: builder.timeout.as_secs(),
            cache_size: builder.cache_size,
            cache_duration_secs: builder.cache_duration_secs,
            connection_pool_max: builder.connection_pool_max,
            verbose_logging: builder.verbose_logging,
            enable_metrics: builder.enable_metrics,
            enable_tracing: builder.enable_tracing,
        }
    }
}

impl From<PresetConfig> for YahooConnectorBuilder {
    fn from(preset: PresetConfig) -> Self {
        Self {
            rate_limit: preset.rate_limit,
            circuit_breaker_threshold: preset.circuit_breaker_threshold,
            circuit_breaker_window_secs: preset.circuit_breaker_window_secs,
            circuit_breaker_timeout_secs: preset.circuit_breaker_timeout_secs,
            retry_attempts: preset.retry_attempts,
            retry_initial_delay_ms: preset.retry_initial_delay_ms,
            retry_max_delay_ms: preset.retry_max_delay_ms,
            timeout: Duration::from_secs(preset.timeout_secs),
            cache_size: preset.cache_size,
            cache_duration_secs: preset.cache_duration_secs,
            connection_pool_max: preset.connection_pool_max,
            verbose_logging: preset.verbose_logging,
            enable_metrics: preset.enable_metrics,
            enable_tracing: preset.enable_tracing,
        }
    }
}

impl From<PresetConfig> for EnterpriseConfig {
    fn from(preset: PresetConfig) -> Self {
        Self {
            circuit_breaker: CircuitBreakerConfig {
                failure_threshold: preset.circuit_breaker_threshold,
                success_threshold: 3, // reasonable default
                recovery_timeout_ms: preset.circuit_breaker_timeout_secs * 1000, // convert to ms
                half_open_max_requests: 3, // reasonable default
                failure_rate_window_ms: preset.circuit_breaker_window_secs * 1000, // convert to ms
                minimum_request_volume: 10, // minimum 10 calls before circuit breaker activates
                categorize_failures: true, // enable smart error categorization
            },
            retry: RetryConfig {
                max_attempts: preset.retry_attempts,
                base_delay_ms: preset.retry_initial_delay_ms,
                max_delay_ms: preset.retry_max_delay_ms,
                backoff_multiplier: 2.0, // exponential backoff
                jitter_factor: 0.1,      // 10% jitter to prevent thundering herd
                enable_exponential_backoff: true,
                respect_error_categories: true,
            },
            deduplication: DeduplicationConfig {
                cache_ttl_ms: preset.cache_duration_secs * 1000, // convert to ms
                max_cache_entries: preset.cache_size,
                deduplicate_in_flight: true,
                cache_successes: true,
                cache_failures: true,
                failure_cache_ttl_ms: 30_000, // 30 seconds for failures
                max_key_length: 256,
            },
            response_cache: ResponseCacheConfig {
                max_entries: preset.cache_size,
                default_ttl_ms: preset.cache_duration_secs * 1000, // convert to ms
                quote_ttl_ms: (preset.cache_duration_secs / 5) * 1000, // 1/5 of default for live quotes
                search_ttl_ms: preset.cache_duration_secs * 1000,
                history_ttl_ms: preset.cache_duration_secs * 1000,
                max_memory_bytes: 50 * 1024 * 1024, // 50MB default
                enable_size_eviction: true,
                cleanup_interval_ms: 60_000, // 1 minute
                cache_errors: false,         // don't cache errors by default
                error_ttl_ms: 5_000,         // 5 seconds for errors
            },
            observability: ObservabilityConfig {
                enable_logging: true, // always enable logging for presets
                log_level: if preset.verbose_logging {
                    crate::observability::LogLevel::Debug
                } else {
                    crate::observability::LogLevel::Info
                },
                enable_metrics: preset.enable_metrics,
                enable_tracing: preset.enable_tracing,
                enable_health_checks: true,
                metrics_interval_ms: 60_000, // 1 minute
                log_request_details: preset.verbose_logging,
                log_error_details: true, // always log error details
                slow_request_threshold_ms: preset.timeout_secs * 1000, // use timeout as slow request threshold
            },
            connection_pool: ConnectionPoolConfig {
                max_connections_per_host: preset.connection_pool_max / 2, // split between per-host and total
                max_total_connections: preset.connection_pool_max,
                connect_timeout_ms: preset.timeout_secs * 1000, // convert to ms
                request_timeout_ms: preset.timeout_secs * 1000, // convert to ms
                keep_alive_timeout_ms: 90_000,                  // 90 seconds
                idle_timeout_ms: 60_000,                        // 1 minute
                enable_http2: true,
                enable_connection_reuse: true,
                cleanup_interval_ms: 300_000, // 5 minutes
                user_agent: "EEYF/0.1.0 (Enterprise)".to_string(),
                enable_tcp_keepalive: true,
                tcp_keepalive_interval_ms: 30_000, // 30 seconds
            },
            rate_limiter: RateLimitConfig {
                requests_per_hour: (preset.rate_limit * 3600.0) as u32, // convert per-second to per-hour
                burst_limit: (preset.rate_limit * 2.0).max(1.0) as u32, // 2x rate limit as burst
                min_interval: Duration::from_millis((1000.0 / preset.rate_limit.max(1.0)) as u64),
            },
            enable_all_features: true, // presets always enable all features
        }
    }
}

/// Manages preset configurations for YahooConnector.
pub struct PresetManager {
    /// Built-in presets
    builtins: HashMap<String, PresetConfig>,

    /// User config directory (~/.config/eeyf/presets/)
    user_presets_dir: Option<PathBuf>,

    /// Project local directory (./.eeyf/presets/)
    project_presets_dir: Option<PathBuf>,
}

impl PresetManager {
    /// Creates a new preset manager with built-in presets.
    pub fn new() -> Self {
        let mut builtins = HashMap::new();

        // Production preset
        let production = PresetConfig::from(&YahooConnectorBuilder::production());
        builtins.insert("production".to_string(), production);

        // Development preset
        let development = PresetConfig::from(&YahooConnectorBuilder::default());
        builtins.insert("development".to_string(), development);

        // Enterprise preset
        let enterprise = PresetConfig::from(&YahooConnectorBuilder::enterprise());
        builtins.insert("enterprise".to_string(), enterprise);

        // Minimal preset
        let minimal = PresetConfig::from(&YahooConnectorBuilder::minimal());
        builtins.insert("minimal".to_string(), minimal);

        Self {
            builtins,
            user_presets_dir: Self::get_user_presets_dir(),
            project_presets_dir: Self::get_project_presets_dir(),
        }
    }

    /// Gets the user presets directory (~/.config/eeyf/presets/)
    fn get_user_presets_dir() -> Option<PathBuf> {
        #[cfg(target_os = "windows")]
        {
            std::env::var("APPDATA")
                .ok()
                .map(|appdata| PathBuf::from(appdata).join("eeyf").join("presets"))
        }

        #[cfg(not(target_os = "windows"))]
        {
            std::env::var("HOME").ok().map(|home| {
                PathBuf::from(home)
                    .join(".config")
                    .join("eeyf")
                    .join("presets")
            })
        }
    }

    /// Gets the project presets directory (./.eeyf/presets/)
    fn get_project_presets_dir() -> Option<PathBuf> {
        std::env::current_dir()
            .ok()
            .map(|cwd| cwd.join(".eeyf").join("presets"))
    }

    /// Loads a preset by name.
    ///
    /// Searches in this order:
    /// 1. Built-in presets
    /// 2. Project-local presets (./.eeyf/presets/)
    /// 3. User presets (~/.config/eeyf/presets/)
    ///
    /// # Errors
    ///
    /// Returns an error if the preset is not found or cannot be loaded.
    pub fn load_preset(&self, name: &str) -> Result<PresetConfig, YahooError> {
        // Check built-in presets first
        if let Some(preset) = self.builtins.get(name) {
            return Ok(preset.clone());
        }

        // Check project-local presets
        if let Some(ref dir) = self.project_presets_dir {
            if let Ok(preset) = self.load_preset_from_dir(dir, name) {
                return Ok(preset);
            }
        }

        // Check user presets
        if let Some(ref dir) = self.user_presets_dir {
            if let Ok(preset) = self.load_preset_from_dir(dir, name) {
                return Ok(preset);
            }
        }

        Err(YahooError::InvalidStatusCode(format!(
            "Preset '{}' not found. Available built-in presets: production, development, enterprise, minimal",
            name
        )))
    }

    /// Loads a preset from a specific directory.
    fn load_preset_from_dir(&self, dir: &Path, name: &str) -> Result<PresetConfig, YahooError> {
        // Try .toml first, then .json
        let toml_path = dir.join(format!("{}.toml", name));
        let json_path = dir.join(format!("{}.json", name));

        if toml_path.exists() {
            let content = fs::read_to_string(&toml_path).map_err(|e| {
                YahooError::InvalidStatusCode(format!("Failed to read preset file: {}", e))
            })?;

            let preset: PresetConfig = toml::from_str(&content).map_err(|e| {
                YahooError::InvalidStatusCode(format!("Failed to parse TOML preset: {}", e))
            })?;

            return Ok(preset);
        }

        if json_path.exists() {
            let content = fs::read_to_string(&json_path).map_err(|e| {
                YahooError::InvalidStatusCode(format!("Failed to read preset file: {}", e))
            })?;

            let preset: PresetConfig = serde_json::from_str(&content).map_err(|e| {
                YahooError::InvalidStatusCode(format!("Failed to parse JSON preset: {}", e))
            })?;

            return Ok(preset);
        }

        Err(YahooError::InvalidStatusCode(format!(
            "Preset file not found in directory: {:?}",
            dir
        )))
    }

    /// Saves a preset to the user presets directory.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Attempting to save a built-in preset
    /// - Unable to create the presets directory
    /// - Unable to write the preset file
    pub fn save_preset(
        &self,
        preset: &PresetConfig,
        format: PresetFormat,
    ) -> Result<(), YahooError> {
        // Prevent overwriting built-in presets
        if self.builtins.contains_key(&preset.name) {
            return Err(YahooError::InvalidStatusCode(format!(
                "Cannot overwrite built-in preset '{}'",
                preset.name
            )));
        }

        let dir = self.user_presets_dir.as_ref().ok_or_else(|| {
            YahooError::InvalidStatusCode("Unable to determine user presets directory".into())
        })?;

        // Create directory if it doesn't exist
        fs::create_dir_all(dir).map_err(|e| {
            YahooError::InvalidStatusCode(format!("Failed to create presets directory: {}", e))
        })?;

        let (filename, content) = match format {
            PresetFormat::Toml => {
                let content = toml::to_string_pretty(preset).map_err(|e| {
                    YahooError::InvalidStatusCode(format!(
                        "Failed to serialize preset to TOML: {}",
                        e
                    ))
                })?;
                (format!("{}.toml", preset.name), content)
            }
            PresetFormat::Json => {
                let content = serde_json::to_string_pretty(preset).map_err(|e| {
                    YahooError::InvalidStatusCode(format!(
                        "Failed to serialize preset to JSON: {}",
                        e
                    ))
                })?;
                (format!("{}.json", preset.name), content)
            }
        };

        let path = dir.join(filename);
        fs::write(&path, content).map_err(|e| {
            YahooError::InvalidStatusCode(format!("Failed to write preset file: {}", e))
        })?;

        Ok(())
    }

    /// Lists all available presets (built-in and user-defined).
    pub fn list_presets(&self) -> Vec<String> {
        let mut presets: Vec<String> = self.builtins.keys().cloned().collect();

        // Add user presets
        if let Some(ref dir) = self.user_presets_dir {
            if let Ok(entries) = fs::read_dir(dir) {
                for entry in entries.flatten() {
                    if let Some(name) = entry.path().file_stem() {
                        if let Some(name_str) = name.to_str() {
                            if !presets.contains(&name_str.to_string()) {
                                presets.push(name_str.to_string());
                            }
                        }
                    }
                }
            }
        }

        // Add project presets
        if let Some(ref dir) = self.project_presets_dir {
            if let Ok(entries) = fs::read_dir(dir) {
                for entry in entries.flatten() {
                    if let Some(name) = entry.path().file_stem() {
                        if let Some(name_str) = name.to_str() {
                            if !presets.contains(&name_str.to_string()) {
                                presets.push(name_str.to_string());
                            }
                        }
                    }
                }
            }
        }

        presets.sort();
        presets
    }
}

impl Default for PresetManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Format for saving preset configurations.
#[derive(Debug, Clone, Copy)]
pub enum PresetFormat {
    /// TOML format
    Toml,
    /// JSON format
    Json,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_preset_manager_builtins() {
        let manager = PresetManager::new();

        assert!(manager.load_preset("production").is_ok());
        assert!(manager.load_preset("development").is_ok());
        assert!(manager.load_preset("enterprise").is_ok());
        assert!(manager.load_preset("minimal").is_ok());
    }

    #[test]
    fn test_preset_manager_not_found() {
        let manager = PresetManager::new();
        assert!(manager.load_preset("nonexistent").is_err());
    }

    #[test]
    fn test_preset_config_from_builder() {
        let builder = YahooConnectorBuilder::production();
        let preset = PresetConfig::from(&builder);

        assert_eq!(preset.rate_limit, 0.5);
        assert_eq!(preset.circuit_breaker_threshold, 5);
        assert!(preset.enable_tracing);
    }

    #[test]
    fn test_builder_from_preset() {
        let mut preset = PresetConfig::from(&YahooConnectorBuilder::production());
        preset.name = "test".to_string();
        preset.rate_limit = 7.5;
        preset.cache_size = 9999;

        let builder = YahooConnectorBuilder::from(preset);
        assert_eq!(builder.rate_limit, 7.5);
        assert_eq!(builder.cache_size, 9999);
    }

    #[test]
    fn test_list_presets() {
        let manager = PresetManager::new();
        let presets = manager.list_presets();

        assert!(presets.contains(&"production".to_string()));
        assert!(presets.contains(&"development".to_string()));
        assert!(presets.contains(&"enterprise".to_string()));
        assert!(presets.contains(&"minimal".to_string()));
    }

    #[test]
    fn test_prevent_overwrite_builtin() {
        let manager = PresetManager::new();
        let mut preset = PresetConfig::from(&YahooConnectorBuilder::production());
        preset.name = "production".to_string(); // Set to a built-in preset name

        let result = manager.save_preset(&preset, PresetFormat::Toml);
        assert!(result.is_err());
    }
}