use crate::models::Profile;
use std::env;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ChaserConfig {
pub context_limit: usize,
pub timeout_ms: u64,
pub profile: Profile,
pub lazy_init: bool,
pub chrome_path: Option<PathBuf>,
pub headless: bool,
pub viewport_width: u32,
pub viewport_height: u32,
}
impl Default for ChaserConfig {
fn default() -> Self {
Self {
context_limit: 20,
timeout_ms: 60000,
profile: Profile::Windows,
lazy_init: false,
chrome_path: None,
headless: false,
viewport_width: 1920,
viewport_height: 1080,
}
}
}
impl ChaserConfig {
pub fn from_env() -> Self {
let mut config = Self::default();
if let Ok(val) = env::var("CHASER_CONTEXT_LIMIT") {
if let Ok(limit) = val.parse() {
config.context_limit = limit;
}
}
if let Ok(val) = env::var("CHASER_TIMEOUT") {
if let Ok(timeout) = val.parse() {
config.timeout_ms = timeout;
}
}
if let Ok(val) = env::var("CHASER_PROFILE") {
if let Some(profile) = Profile::parse(&val) {
config.profile = profile;
}
}
if let Ok(val) = env::var("CHASER_LAZY_INIT") {
config.lazy_init = val.eq_ignore_ascii_case("true") || val == "1";
}
if let Ok(val) = env::var("CHROME_BIN") {
config.chrome_path = Some(PathBuf::from(val));
}
if let Ok(val) = env::var("CHASER_HEADLESS") {
config.headless = val.eq_ignore_ascii_case("true") || val == "1";
}
config
}
pub fn with_context_limit(mut self, limit: usize) -> Self {
self.context_limit = limit;
self
}
pub fn with_timeout_ms(mut self, timeout: u64) -> Self {
self.timeout_ms = timeout;
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout_ms = timeout.as_millis() as u64;
self
}
pub fn with_profile(mut self, profile: Profile) -> Self {
self.profile = profile;
self
}
pub fn with_lazy_init(mut self, lazy: bool) -> Self {
self.lazy_init = lazy;
self
}
pub fn with_chrome_path(mut self, path: impl Into<PathBuf>) -> Self {
self.chrome_path = Some(path.into());
self
}
pub fn with_headless(mut self, headless: bool) -> Self {
self.headless = headless;
self
}
pub fn with_viewport(mut self, width: u32, height: u32) -> Self {
self.viewport_width = width;
self.viewport_height = height;
self
}
pub fn timeout(&self) -> Duration {
Duration::from_millis(self.timeout_ms)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = ChaserConfig::default();
assert_eq!(config.context_limit, 20);
assert_eq!(config.timeout_ms, 60000);
assert_eq!(config.profile, Profile::Windows);
assert!(!config.lazy_init);
assert!(!config.headless);
}
#[test]
fn test_builder_pattern() {
let config = ChaserConfig::default()
.with_context_limit(10)
.with_timeout_ms(30000)
.with_profile(Profile::Linux)
.with_lazy_init(true)
.with_headless(true);
assert_eq!(config.context_limit, 10);
assert_eq!(config.timeout_ms, 30000);
assert_eq!(config.profile, Profile::Linux);
assert!(config.lazy_init);
assert!(config.headless);
}
}