wsl-clip-core 0.5.1

Core library for wsl-clip clipboard bridge
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
// <FILE>wsl_clip_core/src/config.rs</FILE> - <DESC>Config loader and resolver for wsl-clip</DESC>
// <VERS>VERSION: 0.2.0 - 2025-12-08T00:00:00Z</VERS>
// <WCTX>Introduced ResolvedConfig with precedence (CLI > file > defaults) for bridge Phase 1.</WCTX>

use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Bridge transport mode for CLI and config.
#[derive(Debug, Clone, Copy, ValueEnum, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum BridgeMode {
    Tcp,
    Udp,
}

/// Parse human-friendly duration strings used by bridge flags/config.
/// Supports suffixes h, m, s, ms; plain numbers are milliseconds. "0" means zero duration.
pub fn parse_duration(input: &str) -> Option<Duration> {
    if input.trim().is_empty() {
        return None;
    }
    let lower = input.trim().to_ascii_lowercase();
    if lower == "0" {
        return Some(Duration::from_millis(0));
    }

    let (num_str, unit) = if lower.ends_with("ms") {
        (&lower[..lower.len() - 2], "ms")
    } else if lower.ends_with('h') {
        (&lower[..lower.len() - 1], "h")
    } else if lower.ends_with('m') {
        (&lower[..lower.len() - 1], "m")
    } else if lower.ends_with('s') {
        (&lower[..lower.len() - 1], "s")
    } else {
        (lower.as_str(), "ms")
    };

    let value: u64 = num_str.parse().ok()?;
    match unit {
        "ms" => Some(Duration::from_millis(value)),
        "s" => Some(Duration::from_secs(value)),
        "m" => Some(Duration::from_secs(value * 60)),
        "h" => Some(Duration::from_secs(value * 60 * 60)),
        _ => None,
    }
}

impl std::fmt::Display for BridgeMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BridgeMode::Tcp => write!(f, "tcp"),
            BridgeMode::Udp => write!(f, "udp"),
        }
    }
}

pub(crate) const DEFAULT_BIND: &str = "127.0.0.1:8121";
pub(crate) const DEFAULT_TARGET: &str = "127.0.0.1:8121";
pub(crate) const DEFAULT_MODE: BridgeMode = BridgeMode::Tcp;
pub(crate) const DEFAULT_IDLE_EXIT: &str = "2h"; // 0 = never (per workplan), default 2h
pub(crate) const DEFAULT_MAX_BYTES: u64 = 1_073_741_824; // 1 GiB
pub(crate) const DEFAULT_SOCKET: &str = "/run/wsl-clip/socket";

pub mod loader {
    use super::*;

    /// Raw config loaded from disk (all optional).
    #[derive(Debug, Default, Deserialize, Serialize, Clone)]
    pub struct BridgeConfig {
        pub bind: Option<String>,
        pub target: Option<String>,
        pub mode: Option<BridgeMode>,
        pub idle_exit: Option<String>,
        pub ttl: Option<String>,
        pub max_bytes: Option<u64>,
        pub debug: Option<bool>,
        pub socket: Option<PathBuf>,
        pub log_file: Option<PathBuf>,
        pub token: Option<String>,
        pub token_file: Option<PathBuf>,
    }

    /// Raw config container.
    #[derive(Debug, Default, Deserialize, Serialize, Clone)]
    pub struct AppConfig {
        pub bridge: Option<BridgeConfig>,
    }

    fn default_config_path() -> Option<PathBuf> {
        if let Ok(home) = std::env::var("HOME") {
            let p = Path::new(&home).join(".config/wsl-clip/config.toml");
            return Some(p);
        }
        None
    }

    pub fn load_config() -> AppConfig {
        let path = if let Some(p) = default_config_path() {
            p
        } else {
            return AppConfig::default();
        };
        let data = match fs::read_to_string(&path) {
            Ok(d) => d,
            Err(_) => return AppConfig::default(),
        };
        toml::from_str(&data).unwrap_or_default()
    }
}

pub mod resolved {
    use super::*;

    /// CLI-sourced overrides for bridge subcommands.
    #[derive(Debug, Clone)]
    pub enum BridgeCliOverrides {
        Listen {
            bind: Option<String>,
            mode: Option<BridgeMode>,
            idle_exit: Option<String>,
            ttl: Option<String>,
            max_bytes: Option<u64>,
            token: Option<String>,
            token_file: Option<PathBuf>,
            daemon: bool,
            log_file: Option<PathBuf>,
        },
        Connect {
            to: Option<String>,
            socket: Option<PathBuf>,
            mode: Option<BridgeMode>,
            token: Option<String>,
            token_file: Option<PathBuf>,
        },
        Status,
    }

    /// Resolved config for bridge commands after applying precedence.
    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
    pub enum ResolvedBridgeCommand {
        Listen(ResolvedBridgeListen),
        Connect(ResolvedBridgeConnect),
        Status(ResolvedBridgeStatus),
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
    pub struct ResolvedBridgeListen {
        pub bind: String,
        pub mode: BridgeMode,
        pub idle_exit: Option<String>,
        pub ttl: Option<String>,
        pub max_bytes: u64,
        pub token: Option<String>,
        pub token_file: Option<PathBuf>,
        pub daemon: bool,
        pub log_file: Option<PathBuf>,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
    pub struct ResolvedBridgeConnect {
        pub to: String,
        pub socket: PathBuf,
        pub mode: BridgeMode,
        pub token: Option<String>,
        pub token_file: Option<PathBuf>,
        pub max_bytes: u64,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
    pub struct ResolvedBridgeStatus {
        pub bind: String,
        pub socket: PathBuf,
        pub mode: BridgeMode,
    }

    /// Merge CLI overrides with file config and defaults. Precedence: CLI > file > defaults.
    pub fn resolve_bridge_command(
        overrides: BridgeCliOverrides,
        cfg: &crate::config::loader::AppConfig,
    ) -> ResolvedBridgeCommand {
        let bridge_cfg = cfg.bridge.clone().unwrap_or_default();
        match overrides {
            BridgeCliOverrides::Listen {
                bind,
                mode,
                idle_exit,
                ttl,
                max_bytes,
                token,
                token_file,
                daemon,
                log_file,
            } => {
                let bind = bind
                    .or(bridge_cfg.bind.clone())
                    .unwrap_or_else(|| DEFAULT_BIND.to_string());
                let mode = mode.or(bridge_cfg.mode).unwrap_or(DEFAULT_MODE);
                let idle_exit = idle_exit
                    .or(bridge_cfg.idle_exit.clone())
                    .or_else(|| Some(DEFAULT_IDLE_EXIT.to_string()));
                let ttl = ttl.or(bridge_cfg.ttl.clone());
                let max_bytes = max_bytes
                    .or(bridge_cfg.max_bytes)
                    .unwrap_or(DEFAULT_MAX_BYTES);
                let token = token.or(bridge_cfg.token.clone());
                let token_file = token_file.or(bridge_cfg.token_file.clone());
                let log_file = log_file.or(bridge_cfg.log_file.clone());
                ResolvedBridgeCommand::Listen(ResolvedBridgeListen {
                    bind,
                    mode,
                    idle_exit,
                    ttl,
                    max_bytes,
                    token,
                    token_file,
                    daemon,
                    log_file,
                })
            }
            BridgeCliOverrides::Connect {
                to,
                socket,
                mode,
                token,
                token_file,
            } => {
                let to = to
                    .or(bridge_cfg.target.clone())
                    .or(bridge_cfg.bind.clone())
                    .unwrap_or_else(|| DEFAULT_TARGET.to_string());
                let socket = socket
                    .or(bridge_cfg.socket.clone())
                    .unwrap_or_else(|| PathBuf::from(DEFAULT_SOCKET));
                let mode = mode.or(bridge_cfg.mode).unwrap_or(DEFAULT_MODE);
                let token = token.or(bridge_cfg.token.clone());
                let token_file = token_file.or(bridge_cfg.token_file.clone());
                ResolvedBridgeCommand::Connect(ResolvedBridgeConnect {
                    to,
                    socket,
                    mode,
                    token,
                    token_file,
                    max_bytes: bridge_cfg.max_bytes.unwrap_or(DEFAULT_MAX_BYTES),
                })
            }
            BridgeCliOverrides::Status => {
                let bind = bridge_cfg.bind.unwrap_or_else(|| DEFAULT_BIND.to_string());
                let socket = bridge_cfg
                    .socket
                    .unwrap_or_else(|| PathBuf::from(DEFAULT_SOCKET));
                let mode = bridge_cfg.mode.unwrap_or(DEFAULT_MODE);
                ResolvedBridgeCommand::Status(ResolvedBridgeStatus { bind, socket, mode })
            }
        }
    }
}

pub use loader::{AppConfig, BridgeConfig, load_config};
pub use resolved::{BridgeCliOverrides, ResolvedBridgeCommand, ResolvedBridgeConnect, ResolvedBridgeListen, ResolvedBridgeStatus, resolve_bridge_command};

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

    fn cfg_from_str(src: &str) -> AppConfig {
        toml::from_str(src).unwrap()
    }

    #[test]
    fn value_enum_roundtrip() {
        let cfg: BridgeConfig = toml::from_str("mode = \"tcp\"").unwrap();
        assert!(matches!(cfg.mode, Some(BridgeMode::Tcp)));
    }

    #[test]
    fn parse_duration_supports_units() {
        assert_eq!(parse_duration("1500").unwrap(), Duration::from_millis(1500));
        assert_eq!(parse_duration("2s").unwrap(), Duration::from_secs(2));
        assert_eq!(parse_duration("3m").unwrap(), Duration::from_secs(180));
        assert_eq!(parse_duration("1h").unwrap(), Duration::from_secs(3600));
        assert_eq!(parse_duration("0").unwrap(), Duration::from_millis(0));
    }

    #[test]
    fn listen_defaults_when_empty() {
        let cfg = AppConfig::default();
        let resolved = resolve_bridge_command(
            BridgeCliOverrides::Listen {
                bind: None,
                mode: None,
                idle_exit: None,
                ttl: None,
                max_bytes: None,
                token: None,
                token_file: None,
                daemon: false,
                log_file: None,
            },
            &cfg,
        );
        match resolved {
            ResolvedBridgeCommand::Listen(listen) => {
                assert_eq!(listen.bind, DEFAULT_BIND);
                assert_eq!(listen.mode, DEFAULT_MODE);
                assert_eq!(listen.idle_exit, Some(DEFAULT_IDLE_EXIT.to_string()));
                assert_eq!(listen.max_bytes, DEFAULT_MAX_BYTES);
                assert!(!listen.daemon);
            }
            _ => panic!("expected listen"),
        }
    }

    #[test]
    fn cli_overrides_config() {
        let cfg = cfg_from_str(
            r#"[bridge]
bind = "10.0.0.5:9000"
mode = "udp"
idle_exit = "30m"
max_bytes = 42
"#,
        );
        let resolved = resolve_bridge_command(
            BridgeCliOverrides::Listen {
                bind: Some("1.2.3.4:9999".into()),
                mode: Some(BridgeMode::Tcp),
                idle_exit: Some("10m".into()),
                ttl: None,
                max_bytes: Some(100),
                token: None,
                token_file: None,
                daemon: true,
                log_file: None,
            },
            &cfg,
        );
        match resolved {
            ResolvedBridgeCommand::Listen(listen) => {
                assert_eq!(listen.bind, "1.2.3.4:9999");
                assert_eq!(listen.mode, BridgeMode::Tcp);
                assert_eq!(listen.idle_exit, Some("10m".into()));
                assert_eq!(listen.max_bytes, 100);
                assert!(listen.daemon);
            }
            _ => panic!("expected listen"),
        }
    }

    #[test]
    fn connect_target_precedence_cli_over_config_over_default() {
        let cfg = cfg_from_str(
            r#"[bridge]
target = "10.1.1.1:8121"
bind = "10.2.2.2:9000"
socket = "/tmp/custom.sock"
mode = "udp"
"#,
        );
        let resolved = resolve_bridge_command(
            BridgeCliOverrides::Connect {
                to: Some("1.1.1.1:7000".into()),
                socket: None,
                mode: None,
                token: None,
                token_file: None,
            },
            &cfg,
        );
        match resolved {
            ResolvedBridgeCommand::Connect(conn) => {
                assert_eq!(conn.to, "1.1.1.1:7000");
                assert_eq!(conn.socket, PathBuf::from("/tmp/custom.sock"));
                assert_eq!(conn.mode, BridgeMode::Udp);
            }
            _ => panic!("expected connect"),
        }
    }

    #[test]
    fn connect_falls_back_to_default_when_no_config() {
        let cfg = AppConfig::default();
        let resolved = resolve_bridge_command(
            BridgeCliOverrides::Connect {
                to: None,
                socket: None,
                mode: None,
                token: None,
                token_file: None,
            },
            &cfg,
        );
        match resolved {
            ResolvedBridgeCommand::Connect(conn) => {
                assert_eq!(conn.to, DEFAULT_TARGET);
                assert_eq!(conn.socket, PathBuf::from(DEFAULT_SOCKET));
                assert_eq!(conn.mode, DEFAULT_MODE);
            }
            _ => panic!("expected connect"),
        }
    }

    #[test]
    fn status_uses_defaults_and_config() {
        let cfg = cfg_from_str(
            r#"[bridge]
bind = "10.0.0.5:8121"
socket = "/tmp/x.sock"
"#,
        );
        let resolved = resolve_bridge_command(BridgeCliOverrides::Status, &cfg);
        match resolved {
            ResolvedBridgeCommand::Status(status) => {
                assert_eq!(status.bind, "10.0.0.5:8121");
                assert_eq!(status.socket, PathBuf::from("/tmp/x.sock"));
                assert_eq!(status.mode, DEFAULT_MODE);
            }
            _ => panic!("expected status"),
        }
    }
}

// <FILE>wsl_clip_core/src/config.rs</FILE> - <DESC>Config loader and resolver for wsl-clip</DESC>
// <VERS>END OF VERSION: 0.2.0 - 2025-12-08T00:00:00Z</VERS>