fastsync 0.10.1

A fast, safe one-way directory synchronization tool for local folders and network transfers.
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;

use clap::builder::{
    NonEmptyStringValueParser, PossibleValue, PossibleValuesParser, TypedValueParser,
};
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};

use crate::config::{CompareMode, HashAlgorithm, LogLevel, OutputMode, PreserveMode, VerifyMode};
use crate::filter::{FilterConfig, FilterMode};
use crate::i18n::{Language, set_language, tr};

/// fastsync 命令行参数。
///
/// 参数按照同步语义、比较策略、性能控制和输出行为分组,默认值优先保证安全。
#[derive(Debug, Clone)]
pub struct Cli {
    /// 源目录。
    pub source: PathBuf,

    /// 目标目录。
    pub target: PathBuf,

    /// 只生成计划与摘要,不实际修改目标目录。
    pub dry_run: bool,

    /// 删除目标端源目录中不存在的多余项。默认关闭,避免误删。
    pub delete: bool,

    /// 遍历时跟随符号链接。默认关闭。
    pub follow_symlinks: bool,

    /// 同步路径过滤规则文件。黑名单保护匹配路径,白名单限制同步作用域。
    pub filter: Option<FilterConfig>,

    /// 文件比较策略。
    pub compare: CompareMode,

    /// strict 比较模式的快捷方式:大小一致时始终使用 BLAKE3 确认内容。
    pub strict: bool,

    /// 内容校验哈希算法。当前支持 BLAKE3。
    pub hash: HashAlgorithm,

    /// 复制后的校验强度。
    pub verify: VerifyMode,

    /// 禁用同名且内容相同文件的独立元数据同步。
    pub sync_metadata: bool,

    /// 是否保留修改时间。
    pub preserve_times: PreserveMode,

    /// 是否保留基础权限位。
    pub preserve_permissions: PreserveMode,

    /// 禁用临时文件 + 重命名写入目标文件。
    pub atomic_write: bool,

    /// worker 线程数,可传数字或 auto。
    pub threads: Option<String>,

    /// 有界任务队列长度,默认 threads * 4。
    pub queue_size: Option<usize>,

    /// 最大允许错误数,达到阈值后中止。
    pub max_errors: usize,

    /// 首个错误后立即停止。
    pub stop_on_error: bool,

    /// 日志级别。
    pub log_level: LogLevel,

    /// 摘要输出格式。
    pub output: OutputMode,

    /// 用户界面语言。
    pub language: Language,
}

impl Cli {
    /// 从进程参数解析 CLI,解析失败或请求帮助/版本时由 clap 负责退出。
    pub fn parse() -> Self {
        let args: Vec<_> = std::env::args_os().collect();
        Self::parse_from(args)
    }

    /// 从给定参数解析 CLI,主要用于主入口和测试。
    pub fn parse_from<I, T>(args: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<OsString> + Clone,
    {
        let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
        let language = Self::detect_language(&args);
        let matches = Self::command(language).get_matches_from(args);
        Self::from_matches(matches, language)
    }

    /// 构造指定语言的 clap 命令定义。
    pub fn command(language: Language) -> Command {
        set_language(language);

        let arguments = static_text(tr(language, "cli.arguments"));
        let options = static_text(tr(language, "cli.options"));
        let usage = tr(language, "cli.usage");
        let network_help = static_text(tr(language, "cli.network_help"));
        let help_template =
            format!("{{about-with-newline}}\n{usage}: {{usage}}\n\n{{all-args}}\n{{after-help}}");

        Command::new("fastsync")
            .version(env!("CARGO_PKG_VERSION"))
            .about(tr(language, "cli.about"))
            .disable_help_flag(true)
            .disable_version_flag(true)
            .help_template(help_template)
            .after_help(network_help)
            .arg(
                Arg::new("source")
                    .value_name("SOURCE")
                    .value_parser(value_parser!(PathBuf))
                    .required(true)
                    .help(tr(language, "cli.source"))
                    .help_heading(arguments),
            )
            .arg(
                Arg::new("target")
                    .value_name("TARGET")
                    .value_parser(value_parser!(PathBuf))
                    .required(true)
                    .help(tr(language, "cli.target"))
                    .help_heading(arguments),
            )
            .arg(flag(
                "dry_run",
                'n',
                "dry-run",
                tr(language, "cli.dry_run"),
                options,
            ))
            .arg(flag(
                "delete",
                'd',
                "delete",
                tr(language, "cli.delete"),
                options,
            ))
            .arg(long_flag(
                "follow_symlinks",
                "follow-symlinks",
                tr(language, "cli.follow_symlinks"),
                options,
            ))
            .arg(
                Arg::new("exclude_from")
                    .short('x')
                    .long("exclude-from")
                    .value_name("FILE")
                    .value_parser(value_parser!(PathBuf))
                    .conflicts_with("include_from")
                    .help(tr(language, "cli.exclude_from"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("include_from")
                    .short('i')
                    .long("include-from")
                    .value_name("FILE")
                    .value_parser(value_parser!(PathBuf))
                    .conflicts_with("exclude_from")
                    .help(tr(language, "cli.include_from"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("compare")
                    .short('c')
                    .long("compare")
                    .value_name("MODE")
                    .value_parser(compare_mode_parser(language))
                    .default_value("fast")
                    .help(tr(language, "cli.compare"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("strict")
                    .long("strict")
                    .action(ArgAction::SetTrue)
                    .conflicts_with("compare")
                    .help(tr(language, "cli.strict"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("hash")
                    .long("hash")
                    .value_name("ALGORITHM")
                    .value_parser(hash_algorithm_parser(language))
                    .default_value("blake3")
                    .help(tr(language, "cli.hash"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("verify")
                    .long("verify")
                    .value_name("MODE")
                    .value_parser(verify_mode_parser(language))
                    .default_value("changed")
                    .help(tr(language, "cli.verify"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("sync_metadata")
                    .long("no-sync-metadata")
                    .action(ArgAction::SetFalse)
                    .help(tr(language, "cli.no_sync_metadata"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("preserve_times")
                    .long("preserve-times")
                    .value_name("MODE")
                    .value_parser(preserve_mode_parser(language))
                    .default_value("auto")
                    .help(tr(language, "cli.preserve_times"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("preserve_permissions")
                    .long("preserve-permissions")
                    .value_name("MODE")
                    .value_parser(preserve_mode_parser(language))
                    .default_value("auto")
                    .help(tr(language, "cli.preserve_permissions"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("atomic_write")
                    .long("no-atomic-write")
                    .action(ArgAction::SetFalse)
                    .help(tr(language, "cli.no_atomic_write"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("threads")
                    .short('t')
                    .long("threads")
                    .value_name("N|auto")
                    .default_value("auto")
                    .help(tr(language, "cli.threads"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("queue_size")
                    .short('q')
                    .long("queue-size")
                    .value_name("N")
                    .value_parser(value_parser!(usize))
                    .help(tr(language, "cli.queue_size"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("max_errors")
                    .long("max-errors")
                    .value_name("N")
                    .value_parser(value_parser!(usize))
                    .default_value("100")
                    .help(tr(language, "cli.max_errors"))
                    .help_heading(options),
            )
            .arg(long_flag(
                "stop_on_error",
                "stop-on-error",
                tr(language, "cli.stop_on_error"),
                options,
            ))
            .arg(
                Arg::new("log_level")
                    .short('l')
                    .long("log-level")
                    .value_name("LEVEL")
                    .value_parser(log_level_parser())
                    .default_value("info")
                    .help(tr(language, "cli.log_level"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("output")
                    .short('o')
                    .long("output")
                    .value_name("FORMAT")
                    .value_parser(output_mode_parser())
                    .default_value("text")
                    .help(tr(language, "cli.output"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("language")
                    .long("lang")
                    .value_name("LOCALE")
                    .value_parser(language_parser())
                    .default_value(language.as_locale())
                    .help(tr(language, "cli.lang"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("help")
                    .short('h')
                    .long("help")
                    .action(ArgAction::Help)
                    .help(tr(language, "cli.help"))
                    .help_heading(options),
            )
            .arg(
                Arg::new("version")
                    .short('V')
                    .long("version")
                    .action(ArgAction::Version)
                    .help(tr(language, "cli.version"))
                    .help_heading(options),
            )
    }

    /// 根据参数中的 `--lang` 或 `FASTSYNC_LANG` 选择帮助/错误语言。
    pub fn detect_language(args: &[OsString]) -> Language {
        let mut iter = args.iter().skip(1);
        while let Some(arg) = iter.next() {
            if let Some(language) = parse_inline_language(arg) {
                return language;
            }

            if arg == "--lang" {
                if let Some(value) = iter.next().and_then(|value| value.to_str()) {
                    if let Some(language) = Language::parse(value) {
                        return language;
                    }
                }
            }
        }

        Language::from_env().unwrap_or(Language::DEFAULT)
    }

    fn from_matches(matches: ArgMatches, fallback_language: Language) -> Self {
        let language = matches
            .get_one::<Language>("language")
            .copied()
            .unwrap_or(fallback_language);

        Self {
            source: matches
                .get_one::<PathBuf>("source")
                .expect("required by clap")
                .clone(),
            target: matches
                .get_one::<PathBuf>("target")
                .expect("required by clap")
                .clone(),
            dry_run: matches.get_flag("dry_run"),
            delete: matches.get_flag("delete"),
            follow_symlinks: matches.get_flag("follow_symlinks"),
            filter: filter_from_matches(&matches),
            compare: *matches
                .get_one::<CompareMode>("compare")
                .expect("defaulted by clap"),
            strict: matches.get_flag("strict"),
            hash: *matches
                .get_one::<HashAlgorithm>("hash")
                .expect("defaulted by clap"),
            verify: *matches
                .get_one::<VerifyMode>("verify")
                .expect("defaulted by clap"),
            sync_metadata: *matches.get_one::<bool>("sync_metadata").unwrap_or(&true),
            preserve_times: *matches
                .get_one::<PreserveMode>("preserve_times")
                .expect("defaulted by clap"),
            preserve_permissions: *matches
                .get_one::<PreserveMode>("preserve_permissions")
                .expect("defaulted by clap"),
            atomic_write: *matches.get_one::<bool>("atomic_write").unwrap_or(&true),
            threads: matches.get_one::<String>("threads").cloned(),
            queue_size: matches.get_one::<usize>("queue_size").copied(),
            max_errors: *matches
                .get_one::<usize>("max_errors")
                .expect("defaulted by clap"),
            stop_on_error: matches.get_flag("stop_on_error"),
            log_level: *matches
                .get_one::<LogLevel>("log_level")
                .expect("defaulted by clap"),
            output: *matches
                .get_one::<OutputMode>("output")
                .expect("defaulted by clap"),
            language,
        }
    }

    /// 测试辅助构造器,避免单元测试依赖命令行字符串解析。
    #[cfg(test)]
    pub fn for_test(source: &std::path::Path, target: &std::path::Path) -> Self {
        Self {
            source: source.to_path_buf(),
            target: target.to_path_buf(),
            dry_run: false,
            delete: false,
            follow_symlinks: false,
            filter: None,
            compare: CompareMode::Fast,
            strict: false,
            hash: HashAlgorithm::Blake3,
            verify: VerifyMode::Changed,
            sync_metadata: true,
            preserve_times: PreserveMode::Auto,
            preserve_permissions: PreserveMode::Auto,
            atomic_write: true,
            threads: Some("auto".to_string()),
            queue_size: None,
            max_errors: 100,
            stop_on_error: false,
            log_level: LogLevel::Info,
            output: OutputMode::Text,
            language: Language::DEFAULT,
        }
    }
}

fn flag(
    id: &'static str,
    short: char,
    long: &'static str,
    help: String,
    heading: &'static str,
) -> Arg {
    Arg::new(id)
        .short(short)
        .long(long)
        .action(ArgAction::SetTrue)
        .help(help)
        .help_heading(heading)
}

fn long_flag(id: &'static str, long: &'static str, help: String, heading: &'static str) -> Arg {
    Arg::new(id)
        .long(long)
        .action(ArgAction::SetTrue)
        .help(help)
        .help_heading(heading)
}

fn filter_from_matches(matches: &ArgMatches) -> Option<FilterConfig> {
    matches
        .get_one::<PathBuf>("exclude_from")
        .cloned()
        .map(|path| FilterConfig {
            mode: FilterMode::Exclude,
            path,
        })
        .or_else(|| {
            matches
                .get_one::<PathBuf>("include_from")
                .cloned()
                .map(|path| FilterConfig {
                    mode: FilterMode::Include,
                    path,
                })
        })
}

fn parse_inline_language(arg: &OsStr) -> Option<Language> {
    arg.to_str()
        .and_then(|raw| raw.strip_prefix("--lang="))
        .and_then(Language::parse)
}

fn static_text(value: String) -> &'static str {
    Box::leak(value.into_boxed_str())
}

fn compare_mode_parser(language: Language) -> impl TypedValueParser<Value = CompareMode> + 'static {
    PossibleValuesParser::new([
        possible_value("fast", language, "value.compare.fast"),
        possible_value("strict", language, "value.compare.strict"),
    ])
    .map(|value| match value.as_str() {
        "fast" => CompareMode::Fast,
        "strict" => CompareMode::Strict,
        _ => unreachable!("validated by clap possible values"),
    })
}

fn verify_mode_parser(language: Language) -> impl TypedValueParser<Value = VerifyMode> + 'static {
    PossibleValuesParser::new([
        possible_value("none", language, "value.verify.none"),
        possible_value("changed", language, "value.verify.changed"),
        possible_value("all", language, "value.verify.all"),
    ])
    .map(|value| match value.as_str() {
        "none" => VerifyMode::None,
        "changed" => VerifyMode::Changed,
        "all" => VerifyMode::All,
        _ => unreachable!("validated by clap possible values"),
    })
}

fn preserve_mode_parser(
    language: Language,
) -> impl TypedValueParser<Value = PreserveMode> + 'static {
    PossibleValuesParser::new([
        possible_value("auto", language, "value.preserve.auto"),
        possible_value("true", language, "value.preserve.true"),
        possible_value("false", language, "value.preserve.false"),
    ])
    .map(|value| match value.as_str() {
        "auto" => PreserveMode::Auto,
        "true" => PreserveMode::True,
        "false" => PreserveMode::False,
        _ => unreachable!("validated by clap possible values"),
    })
}

fn hash_algorithm_parser(
    language: Language,
) -> impl TypedValueParser<Value = HashAlgorithm> + 'static {
    PossibleValuesParser::new([possible_value("blake3", language, "value.hash.blake3")]).map(
        |value| match value.as_str() {
            "blake3" => HashAlgorithm::Blake3,
            _ => unreachable!("validated by clap possible values"),
        },
    )
}

fn log_level_parser() -> impl TypedValueParser<Value = LogLevel> + 'static {
    PossibleValuesParser::new(["error", "warn", "info", "debug", "trace"]).map(|value| match value
        .as_str()
    {
        "error" => LogLevel::Error,
        "warn" => LogLevel::Warn,
        "info" => LogLevel::Info,
        "debug" => LogLevel::Debug,
        "trace" => LogLevel::Trace,
        _ => unreachable!("validated by clap possible values"),
    })
}

fn output_mode_parser() -> impl TypedValueParser<Value = OutputMode> + 'static {
    PossibleValuesParser::new(["text", "json"]).map(|value| match value.as_str() {
        "text" => OutputMode::Text,
        "json" => OutputMode::Json,
        _ => unreachable!("validated by clap possible values"),
    })
}

fn language_parser() -> impl TypedValueParser<Value = Language> + 'static {
    NonEmptyStringValueParser::new().try_map(|value| {
        Language::parse(&value).ok_or_else(|| format!("unsupported locale: {value}"))
    })
}

fn possible_value(name: &'static str, language: Language, help_key: &str) -> PossibleValue {
    PossibleValue::new(name).help(tr(language, help_key))
}

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

    #[test]
    fn default_help_is_english() {
        let help = Cli::command(Language::En).render_long_help().to_string();

        assert!(help.contains(
            "A fast, reliable one-way directory synchronization tool for local and network sync"
        ));
        assert!(help.contains("Source directory."));
        assert!(help.contains("Trust metadata when it matches"));
        assert!(!help.contains("支持本地与网络同步的快速、可靠单向目录同步工具"));
    }

    #[test]
    fn zh_cn_help_is_chinese() {
        let help = Cli::command(Language::ZhCn).render_long_help().to_string();

        assert!(help.contains("支持本地与网络同步的快速、可靠单向目录同步工具"));
        assert!(help.contains("源目录。"));
        assert!(help.contains("元数据一致时信任元数据"));
    }

    #[test]
    fn detects_inline_language_before_full_parse() {
        let args = vec![
            OsString::from("fastsync"),
            OsString::from("--lang=zh-CN"),
            OsString::from("src"),
            OsString::from("dst"),
        ];

        assert_eq!(Cli::detect_language(&args), Language::ZhCn);
    }

    #[test]
    fn parses_locale_aliases_from_language_flag() {
        let cli = Cli::parse_from(["fastsync", "--lang", "zh_CN.UTF-8", "src", "dst"]);

        assert_eq!(cli.language, Language::ZhCn);
    }

    #[test]
    fn supports_c_locale_alias_for_language_flag() {
        let cli = Cli::parse_from(["fastsync", "--lang", "C", "src", "dst"]);

        assert_eq!(cli.language, Language::En);
    }

    #[test]
    fn supports_additional_locale_aliases_for_language_flag() {
        let cli = Cli::parse_from(["fastsync", "--lang", "zh-Hans-CN", "src", "dst"]);
        assert_eq!(cli.language, Language::ZhCn);

        let cli = Cli::parse_from(["fastsync", "--lang=zh_Hans_CN", "src", "dst"]);
        assert_eq!(cli.language, Language::ZhCn);

        let cli = Cli::parse_from(["fastsync", "--lang", "en-GB", "src", "dst"]);
        assert_eq!(cli.language, Language::En);
    }

    #[test]
    fn filter_shortcuts_parse_to_expected_modes() {
        let exclude = Cli::parse_from(["fastsync", "src", "dst", "-x", "ignore.txt"]);
        assert_eq!(
            exclude.filter,
            Some(FilterConfig {
                mode: FilterMode::Exclude,
                path: PathBuf::from("ignore.txt"),
            })
        );

        let include = Cli::parse_from(["fastsync", "src", "dst", "-i", "include.txt"]);
        assert_eq!(
            include.filter,
            Some(FilterConfig {
                mode: FilterMode::Include,
                path: PathBuf::from("include.txt"),
            })
        );
    }

    #[test]
    fn include_and_exclude_filters_conflict() {
        let result = Cli::command(Language::En).try_get_matches_from([
            "fastsync",
            "src",
            "dst",
            "-x",
            "ignore.txt",
            "-i",
            "include.txt",
        ]);

        assert!(result.is_err());
    }
}