gosh_lan_transfer/
config.rs

1// SPDX-License-Identifier: MIT
2// gosh-lan-transfer - Engine configuration
3
4use std::path::PathBuf;
5
6/// Engine configuration
7#[derive(Debug, Clone)]
8pub struct EngineConfig {
9    /// Port for the HTTP server (default: 53317)
10    pub port: u16,
11    /// Device name shown to peers
12    pub device_name: String,
13    /// Default download directory
14    pub download_dir: PathBuf,
15    /// Trusted hosts that auto-accept transfers
16    pub trusted_hosts: Vec<String>,
17    /// Receive-only mode (disable sending)
18    pub receive_only: bool,
19    /// Maximum number of retry attempts for transient failures (default: 3)
20    pub max_retries: u32,
21    /// Base delay between retries in milliseconds (default: 1000)
22    /// Actual delay uses exponential backoff: delay * 2^attempt
23    pub retry_delay_ms: u64,
24    /// Optional bandwidth limit in bytes per second (None = unlimited)
25    pub bandwidth_limit_bps: Option<u64>,
26}
27
28impl Default for EngineConfig {
29    fn default() -> Self {
30        Self {
31            port: 53317,
32            device_name: hostname::get()
33                .map(|h| h.to_string_lossy().to_string())
34                .unwrap_or_else(|_| "Gosh Device".to_string()),
35            download_dir: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
36            trusted_hosts: Vec::new(),
37            receive_only: false,
38            max_retries: 3,
39            retry_delay_ms: 1000,
40            bandwidth_limit_bps: None,
41        }
42    }
43}
44
45impl EngineConfig {
46    /// Create a new configuration with default values
47    pub fn new() -> Self {
48        Self::default()
49    }
50
51    /// Create a builder for more ergonomic configuration
52    pub fn builder() -> EngineConfigBuilder {
53        EngineConfigBuilder::default()
54    }
55}
56
57/// Builder for EngineConfig
58#[derive(Default)]
59pub struct EngineConfigBuilder {
60    port: Option<u16>,
61    device_name: Option<String>,
62    download_dir: Option<PathBuf>,
63    trusted_hosts: Option<Vec<String>>,
64    receive_only: Option<bool>,
65    max_retries: Option<u32>,
66    retry_delay_ms: Option<u64>,
67    bandwidth_limit_bps: Option<Option<u64>>,
68}
69
70impl EngineConfigBuilder {
71    /// Set the port for the HTTP server
72    pub fn port(mut self, port: u16) -> Self {
73        self.port = Some(port);
74        self
75    }
76
77    /// Set the device name shown to peers
78    pub fn device_name(mut self, name: impl Into<String>) -> Self {
79        self.device_name = Some(name.into());
80        self
81    }
82
83    /// Set the download directory for received files
84    pub fn download_dir(mut self, dir: impl Into<PathBuf>) -> Self {
85        self.download_dir = Some(dir.into());
86        self
87    }
88
89    /// Set the list of trusted hosts (auto-accept transfers)
90    pub fn trusted_hosts(mut self, hosts: Vec<String>) -> Self {
91        self.trusted_hosts = Some(hosts);
92        self
93    }
94
95    /// Add a single trusted host
96    pub fn add_trusted_host(mut self, host: impl Into<String>) -> Self {
97        self.trusted_hosts
98            .get_or_insert_with(Vec::new)
99            .push(host.into());
100        self
101    }
102
103    /// Set receive-only mode
104    pub fn receive_only(mut self, enabled: bool) -> Self {
105        self.receive_only = Some(enabled);
106        self
107    }
108
109    /// Set maximum retry attempts for transient failures
110    pub fn max_retries(mut self, retries: u32) -> Self {
111        self.max_retries = Some(retries);
112        self
113    }
114
115    /// Set base delay between retries in milliseconds
116    pub fn retry_delay_ms(mut self, delay_ms: u64) -> Self {
117        self.retry_delay_ms = Some(delay_ms);
118        self
119    }
120
121    /// Set bandwidth limit in bytes per second (None = unlimited)
122    pub fn bandwidth_limit_bps(mut self, limit: Option<u64>) -> Self {
123        self.bandwidth_limit_bps = Some(limit);
124        self
125    }
126
127    /// Build the configuration
128    pub fn build(self) -> EngineConfig {
129        let default = EngineConfig::default();
130        EngineConfig {
131            port: self.port.unwrap_or(default.port),
132            device_name: self.device_name.unwrap_or(default.device_name),
133            download_dir: self.download_dir.unwrap_or(default.download_dir),
134            trusted_hosts: self.trusted_hosts.unwrap_or(default.trusted_hosts),
135            receive_only: self.receive_only.unwrap_or(default.receive_only),
136            max_retries: self.max_retries.unwrap_or(default.max_retries),
137            retry_delay_ms: self.retry_delay_ms.unwrap_or(default.retry_delay_ms),
138            bandwidth_limit_bps: self
139                .bandwidth_limit_bps
140                .unwrap_or(default.bandwidth_limit_bps),
141        }
142    }
143}