gosh_lan_transfer/
config.rs1use std::path::PathBuf;
5
6#[derive(Debug, Clone)]
8pub struct EngineConfig {
9 pub port: u16,
11 pub device_name: String,
13 pub download_dir: PathBuf,
15 pub trusted_hosts: Vec<String>,
17 pub receive_only: bool,
19 pub max_retries: u32,
21 pub retry_delay_ms: u64,
24 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 pub fn new() -> Self {
48 Self::default()
49 }
50
51 pub fn builder() -> EngineConfigBuilder {
53 EngineConfigBuilder::default()
54 }
55}
56
57#[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 pub fn port(mut self, port: u16) -> Self {
73 self.port = Some(port);
74 self
75 }
76
77 pub fn device_name(mut self, name: impl Into<String>) -> Self {
79 self.device_name = Some(name.into());
80 self
81 }
82
83 pub fn download_dir(mut self, dir: impl Into<PathBuf>) -> Self {
85 self.download_dir = Some(dir.into());
86 self
87 }
88
89 pub fn trusted_hosts(mut self, hosts: Vec<String>) -> Self {
91 self.trusted_hosts = Some(hosts);
92 self
93 }
94
95 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 pub fn receive_only(mut self, enabled: bool) -> Self {
105 self.receive_only = Some(enabled);
106 self
107 }
108
109 pub fn max_retries(mut self, retries: u32) -> Self {
111 self.max_retries = Some(retries);
112 self
113 }
114
115 pub fn retry_delay_ms(mut self, delay_ms: u64) -> Self {
117 self.retry_delay_ms = Some(delay_ms);
118 self
119 }
120
121 pub fn bandwidth_limit_bps(mut self, limit: Option<u64>) -> Self {
123 self.bandwidth_limit_bps = Some(limit);
124 self
125 }
126
127 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}