shaul-core 1.2.0

Core domain, engine, task store, resume and finalizers for shaul
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;

use directories::{BaseDirs, ProjectDirs};
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::header::{Header, RequestOptions};
use crate::task::{TaskOptions, VerifyMode};

/// Effective configuration. Precedence is CLI flags > config file >
/// environment variables > built-in defaults; see `Config::load`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[derive(Default)]
#[non_exhaustive]
pub struct Config {
    /// Download behavior and defaults.
    pub download: DownloadConfig,
    /// HTTP timeout, retry and user-agent settings.
    pub network: NetworkConfig,
    /// Hub provider cache and endpoint settings.
    pub hub: HubConfig,
    /// BitTorrent backend settings.
    pub bt: BtConfig,
    /// Directory for task sidecars; defaults to the platform data directory.
    pub state_dir: Option<PathBuf>,
    /// Default headers applied to every request; values may use `${env:NAME}`
    /// placeholders.
    pub headers: BTreeMap<String, String>,
}

/// Defaults for how files are downloaded, resumed, verified and extracted.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct DownloadConfig {
    /// Default destination directory.
    pub dir: PathBuf,
    /// Default number of parallel connections per task.
    pub connections: usize,
    /// Maximum number of idle connections kept per host in the HTTP pool.
    pub per_host: usize,
    /// Maximum number of active HTTP file-transfer requests per host across
    /// all tasks.
    pub per_host_active: usize,
    /// Maximum number of active HTTP file-transfer requests across all tasks.
    pub global: usize,
    /// Global bandwidth limit in bytes per second. `None` or `0` means
    /// unlimited. Direct HTTP and Hub file transfers share this limit.
    pub rate_limit: Option<u64>,
    /// Additional bandwidth cap per destination host in bytes per second.
    /// `None` or `0` means unlimited.
    pub per_host_rate_limit: Option<u64>,
    /// Number of files downloaded concurrently within one multi-file task.
    pub file_jobs: usize,
    /// Reuse verified downloads through the content-addressed cache.
    pub content_cache: bool,
    /// Override for the content-addressed cache root.
    pub content_cache_dir: Option<PathBuf>,
    /// Refuse a task before transfer when the destination filesystem has less
    /// free space than the known remaining size.
    pub disk_space_check: bool,
    /// Size in bytes of each ranged HTTP segment.
    pub segment_size: u64,
    /// Resume interrupted downloads from checkpoints by default.
    pub resume: bool,
    /// Default checksum verification policy.
    pub verify: VerifyMode,
    /// Extract archives after download by default.
    pub extract: bool,
    /// Default extraction directory; `None` extracts under the destination.
    pub extract_dir: Option<PathBuf>,
    /// Delete archives after successful extraction by default.
    pub delete_archive: bool,
}

/// HTTP networking defaults: timeouts, retries, user agent and auth policy.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct NetworkConfig {
    /// Total request timeout in seconds.
    pub timeout_secs: u64,
    /// Connection timeout in seconds.
    pub connect_timeout_secs: u64,
    /// Number of retry attempts for failed transfers.
    pub retries: u32,
    /// `User-Agent` header sent with every request.
    pub user_agent: String,
    /// Allow authentication headers to follow cross-origin redirects.
    pub allow_cross_origin_auth: bool,
    /// Explicit HTTP/HTTPS/SOCKS proxy URL. When unset, the HTTP client keeps
    /// reqwest's ambient behavior.
    pub proxy: Option<String>,
    /// Comma-separated hosts that bypass the configured proxy.
    pub no_proxy: Option<String>,
}

/// Hugging Face and ModelScope cache and endpoint settings.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[derive(Default)]
#[non_exhaustive]
pub struct HubConfig {
    /// Populate a provider cache while downloading. HF uses the Python
    /// compatible `models--org--repo` layout.
    pub use_cache: bool,
    /// Override for the Hugging Face cache root.
    pub hf_cache: Option<PathBuf>,
    /// Override for the ModelScope cache root.
    pub modelscope_cache: Option<PathBuf>,
    /// Override for the Hugging Face endpoint; defaults to the public site.
    pub hf_endpoint: Option<String>,
    /// Override for the ModelScope endpoint; defaults to the public site.
    pub modelscope_endpoint: Option<String>,
}

/// BitTorrent backend settings.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct BtConfig {
    /// Enable DHT peer discovery.
    pub dht: bool,
    /// Use the trackers announced by the torrent.
    pub trackers: bool,
    /// Listen for incoming peer connections.
    pub listen: bool,
    /// Persist session state and fastresume data under the state directory.
    pub persistence: bool,
    /// Optional limit on connected peers.
    pub peer_limit: Option<usize>,
    /// `ip:port` endpoints used directly as initial peers.
    pub initial_peers: Vec<String>,
}

impl Default for BtConfig {
    fn default() -> Self {
        Self {
            dht: true,
            trackers: true,
            listen: true,
            persistence: true,
            peer_limit: None,
            initial_peers: Vec::new(),
        }
    }
}

impl Default for DownloadConfig {
    fn default() -> Self {
        Self {
            dir: PathBuf::from("."),
            connections: 8,
            per_host: 16,
            per_host_active: 16,
            global: 64,
            rate_limit: None,
            per_host_rate_limit: None,
            file_jobs: 1,
            content_cache: false,
            content_cache_dir: None,
            disk_space_check: true,
            segment_size: 8 * 1024 * 1024,
            resume: true,
            verify: VerifyMode::Auto,
            extract: false,
            extract_dir: None,
            delete_archive: false,
        }
    }
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            timeout_secs: 60,
            connect_timeout_secs: 15,
            retries: 5,
            user_agent: format!("shaul/{}", env!("CARGO_PKG_VERSION")),
            allow_cross_origin_auth: false,
            proxy: None,
            no_proxy: None,
        }
    }
}

/// Options after task overrides and config defaults have been resolved.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ResolvedOptions {
    /// Root directory for the task's output files.
    pub destination: PathBuf,
    /// Explicit output path, if one was requested.
    pub output: Option<PathBuf>,
    /// Effective connection count for this task.
    pub connections: usize,
    /// Effective HTTP idle-connection limit per host.
    pub per_host: usize,
    /// Maximum active HTTP file-transfer requests per host carried from
    /// configuration.
    pub per_host_active: usize,
    /// Global active HTTP file-transfer request budget carried from
    /// configuration.
    pub global: usize,
    /// Global bandwidth limit in bytes per second, if configured.
    pub rate_limit: Option<u64>,
    /// Per-host bandwidth limit in bytes per second, if configured.
    pub per_host_rate_limit: Option<u64>,
    /// Additional per-task bandwidth limit in bytes per second, if configured.
    pub task_rate_limit: Option<u64>,
    /// Number of files downloaded concurrently within this task.
    pub file_jobs: usize,
    /// Whether verified files are reused through the content-addressed cache.
    pub content_cache: bool,
    /// Root of the content-addressed cache.
    pub content_cache_dir: PathBuf,
    /// Whether to preflight the destination filesystem's free space.
    pub disk_space_check: bool,
    /// Alternate HTTP(S) URLs for the same file.
    pub mirrors: Vec<url::Url>,
    /// Byte size of each ranged HTTP segment.
    pub segment_size: u64,
    /// Whether resume is enabled for this task.
    pub resume: bool,
    /// Effective checksum verification policy.
    pub verify: VerifyMode,
    /// Expected checksum supplied by the task, if any.
    pub checksum: Option<crate::checksum::Checksum>,
    /// Whether archives should be extracted after download.
    pub extract: bool,
    /// Directory to extract archives into; `None` means the destination.
    pub extract_dir: Option<PathBuf>,
    /// Whether to delete the archive after extraction.
    pub delete_archive: bool,
    /// Whether to populate the provider cache for Hub tasks.
    pub cache: bool,
    /// Include glob filters passed from the task.
    pub include: Vec<String>,
    /// Exclude glob filters passed from the task.
    pub exclude: Vec<String>,
    /// Merged request options: config headers plus task overrides.
    pub request: RequestOptions,
    /// Whether existing destination files are replaced.
    pub overwrite: bool,
    /// Whether conflicting destinations are renamed.
    pub rename_on_conflict: bool,
    /// Effective retry count for failed transfers.
    pub retries: u32,
    /// Effective total request timeout.
    pub timeout: Duration,
    /// Effective connection timeout.
    pub connect_timeout: Duration,
    /// Effective `User-Agent` header.
    pub user_agent: String,
}

impl Config {
    /// Platform default config file path (`config.toml` in the user config
    /// directory).
    pub fn default_config_path() -> Result<PathBuf> {
        let dirs = project_dirs()?;
        Ok(dirs.config_dir().join("config.toml"))
    }

    /// Platform default directory for task sidecars.
    pub fn default_state_dir() -> Result<PathBuf> {
        let dirs = project_dirs()?;
        Ok(dirs.data_dir().join("tasks"))
    }

    /// Configured state directory, or the platform default.
    pub fn state_dir(&self) -> Result<PathBuf> {
        match &self.state_dir {
            Some(dir) => Ok(dir.clone()),
            None => Self::default_state_dir(),
        }
    }

    /// Resolved Hugging Face cache root: configured override or
    /// `~/.cache/huggingface/hub`.
    pub fn hf_cache_dir(&self) -> Result<PathBuf> {
        if let Some(dir) = &self.hub.hf_cache {
            return Ok(dir.clone());
        }
        let base = BaseDirs::new()
            .ok_or_else(|| Error::Config("cannot determine home directory".to_string()))?;
        Ok(base
            .home_dir()
            .join(".cache")
            .join("huggingface")
            .join("hub"))
    }

    /// Resolved ModelScope cache root: configured override or the platform
    /// cache directory.
    pub fn modelscope_cache_dir(&self) -> Result<PathBuf> {
        if let Some(dir) = &self.hub.modelscope_cache {
            return Ok(dir.clone());
        }
        Ok(project_dirs()?.cache_dir().join("modelscope"))
    }

    /// Resolved content-addressed cache root: configured override or the
    /// platform cache directory under `shaul/content`.
    pub fn content_cache_dir(&self) -> Result<PathBuf> {
        if let Some(dir) = &self.download.content_cache_dir
            && !dir.as_os_str().is_empty()
        {
            return Ok(dir.clone());
        }
        Ok(project_dirs()?.cache_dir().join("content"))
    }

    /// Configured Hugging Face endpoint or the public default.
    pub fn hf_endpoint(&self) -> String {
        self.hub
            .hf_endpoint
            .clone()
            .unwrap_or_else(|| "https://huggingface.co".to_string())
    }

    /// Configured ModelScope endpoint or the public default.
    pub fn modelscope_endpoint(&self) -> String {
        self.hub
            .modelscope_endpoint
            .clone()
            .unwrap_or_else(|| "https://modelscope.cn".to_string())
    }

    /// Resolve the config path from an explicit argument, `SHAUL_CONFIG`, or
    /// the platform default.
    pub fn resolve_path(explicit: Option<&Path>) -> Result<PathBuf> {
        if let Some(path) = explicit {
            return Ok(path.to_path_buf());
        }
        if let Ok(path) = std::env::var("SHAUL_CONFIG")
            && !path.trim().is_empty()
        {
            return Ok(PathBuf::from(path));
        }
        Self::default_config_path()
    }

    /// Load defaults, then environment variables, then the config file. CLI
    /// flags are applied by the caller afterwards.
    pub fn load(explicit: Option<&Path>) -> Result<(Self, PathBuf)> {
        let path = Self::resolve_path(explicit)?;
        let mut config = Self::default();
        config.apply_env()?;
        if path.exists() {
            let text = std::fs::read_to_string(&path)?;
            let partial: PartialConfig = toml::from_str(&text)?;
            partial.apply(&mut config)?;
        }
        Ok((config, path))
    }

    /// Load only defaults + file. Used by `config set` so environment values
    /// are not baked into the file.
    pub fn load_file_only(path: &Path) -> Result<Self> {
        let mut config = Self::default();
        if path.exists() {
            let text = std::fs::read_to_string(path)?;
            let partial: PartialConfig = toml::from_str(&text)?;
            partial.apply(&mut config)?;
        }
        Ok(config)
    }

    /// Serialize the config as TOML, creating parent directories as needed.
    pub fn save(&self, path: &Path) -> Result<()> {
        if let Some(parent) = path.parent()
            && !parent.as_os_str().is_empty()
        {
            std::fs::create_dir_all(parent)?;
        }
        let text = toml::to_string_pretty(self)?;
        std::fs::write(path, text)?;
        Ok(())
    }

    /// Apply `SHAUL_*` and provider environment variables on top of the
    /// current values.
    pub fn apply_env(&mut self) -> Result<()> {
        if let Some(value) = env("SHAUL_DIR") {
            self.download.dir = PathBuf::from(value);
        }
        if let Some(value) = env_parse::<usize>("SHAUL_CONNECTIONS")? {
            self.download.connections = value;
        }
        if let Some(value) = env_parse::<usize>("SHAUL_PER_HOST")? {
            self.download.per_host = value;
        }
        if let Some(value) = env_parse::<usize>("SHAUL_PER_HOST_ACTIVE")? {
            self.download.per_host_active = value.max(1);
        }
        if let Some(value) = env_parse::<usize>("SHAUL_GLOBAL")? {
            self.download.global = value;
        }
        if let Some(value) = env("SHAUL_RATE_LIMIT") {
            self.download.rate_limit = Some(parse_bytes(&value)?).filter(|value| *value > 0);
        }
        if let Some(value) = env("SHAUL_PER_HOST_RATE_LIMIT") {
            self.download.per_host_rate_limit =
                Some(parse_bytes(&value)?).filter(|value| *value > 0);
        }
        if let Some(value) = env_parse::<usize>("SHAUL_FILE_JOBS")? {
            self.download.file_jobs = value.max(1);
        }
        if let Some(value) = env_parse::<bool>("SHAUL_CONTENT_CACHE")? {
            self.download.content_cache = value;
        }
        if let Some(value) = env("SHAUL_CONTENT_CACHE_DIR") {
            self.download.content_cache_dir = Some(PathBuf::from(value));
        }
        if let Some(value) = env_parse::<bool>("SHAUL_DISK_SPACE_CHECK")? {
            self.download.disk_space_check = value;
        }
        if let Some(value) = env("SHAUL_SEGMENT_SIZE") {
            self.download.segment_size = parse_bytes(&value)?;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_RESUME")? {
            self.download.resume = value;
        }
        if let Some(value) = env("SHAUL_VERIFY") {
            self.download.verify = parse_verify_mode(&value)?;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_EXTRACT")? {
            self.download.extract = value;
        }
        if let Some(value) = env("SHAUL_EXTRACT_DIR") {
            self.download.extract_dir = Some(PathBuf::from(value));
        }
        if let Some(value) = env_parse::<bool>("SHAUL_DELETE_ARCHIVE")? {
            self.download.delete_archive = value;
        }
        if let Some(value) = env("SHAUL_STATE_DIR") {
            self.state_dir = Some(PathBuf::from(value));
        }
        if let Some(value) = env_parse::<u64>("SHAUL_TIMEOUT_SECS")? {
            self.network.timeout_secs = value;
        }
        if let Some(value) = env_parse::<u64>("SHAUL_CONNECT_TIMEOUT_SECS")? {
            self.network.connect_timeout_secs = value;
        }
        if let Some(value) = env_parse::<u32>("SHAUL_RETRIES")? {
            self.network.retries = value;
        }
        if let Some(value) = env("SHAUL_USER_AGENT") {
            self.network.user_agent = value;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_ALLOW_CROSS_ORIGIN_AUTH")? {
            self.network.allow_cross_origin_auth = value;
        }
        if let Some(value) = env("SHAUL_PROXY").or_else(|| {
            env("HTTPS_PROXY")
                .or_else(|| env("https_proxy"))
                .or_else(|| env("HTTP_PROXY"))
                .or_else(|| env("http_proxy"))
                .or_else(|| env("ALL_PROXY"))
                .or_else(|| env("all_proxy"))
        }) {
            self.network.proxy = Some(value);
        }
        if let Some(value) = env("SHAUL_NO_PROXY")
            .or_else(|| env("NO_PROXY"))
            .or_else(|| env("no_proxy"))
        {
            self.network.no_proxy = Some(value);
        }

        if let Some(value) = env("HF_HOME") {
            self.hub.hf_cache = Some(PathBuf::from(value).join("hub"));
        }
        if let Some(value) = env("HF_HUB_CACHE") {
            self.hub.hf_cache = Some(PathBuf::from(value));
        }
        if let Some(value) = env("SHAUL_HF_CACHE") {
            self.hub.hf_cache = Some(PathBuf::from(value));
        }
        if let Some(value) = env("HF_ENDPOINT") {
            self.hub.hf_endpoint = Some(value);
        }
        if let Some(value) = env("SHAUL_HF_ENDPOINT") {
            self.hub.hf_endpoint = Some(value);
        }
        if let Some(value) = env("MODELSCOPE_CACHE") {
            self.hub.modelscope_cache = Some(PathBuf::from(value));
        }
        if let Some(value) = env("SHAUL_MODELSCOPE_CACHE") {
            self.hub.modelscope_cache = Some(PathBuf::from(value));
        }
        if let Some(value) = env("MODELSCOPE_ENDPOINT").or_else(|| env("MODELSCOPE_DOMAIN")) {
            self.hub.modelscope_endpoint = Some(value);
        }
        if let Some(value) = env("SHAUL_MODELSCOPE_ENDPOINT") {
            self.hub.modelscope_endpoint = Some(value);
        }
        if let Some(value) = env_parse::<bool>("SHAUL_HUB_USE_CACHE")? {
            self.hub.use_cache = value;
        }

        if let Some(value) = env_parse::<bool>("SHAUL_BT_DHT")? {
            self.bt.dht = value;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_BT_TRACKERS")? {
            self.bt.trackers = value;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_BT_LISTEN")? {
            self.bt.listen = value;
        }
        if let Some(value) = env_parse::<bool>("SHAUL_BT_PERSISTENCE")? {
            self.bt.persistence = value;
        }
        if let Some(value) = env_parse::<usize>("SHAUL_BT_PEER_LIMIT")? {
            self.bt.peer_limit = Some(value);
        }
        if let Some(value) = env("SHAUL_BT_INITIAL_PEERS") {
            self.bt.initial_peers = value
                .split(',')
                .map(str::trim)
                .filter(|value| !value.is_empty())
                .map(str::to_string)
                .collect();
        }
        Ok(())
    }

    /// Parse the configured default headers into validated [`Header`] values.
    pub fn header_defaults(&self) -> Result<Vec<Header>> {
        self.headers
            .iter()
            .map(|(name, value)| Header::new(name, value))
            .collect()
    }

    /// Combine config defaults with per-task options into fully resolved
    /// settings.
    pub fn resolve(&self, destination: &Path, options: &TaskOptions) -> Result<ResolvedOptions> {
        let mut request = options.request.merged(&self.header_defaults()?);
        request.allow_cross_origin_auth |= self.network.allow_cross_origin_auth;
        Ok(ResolvedOptions {
            destination: destination.to_path_buf(),
            output: options.output.clone(),
            connections: options
                .connections
                .unwrap_or(self.download.connections)
                .max(1),
            per_host: self.download.per_host.max(1),
            per_host_active: self.download.per_host_active.max(1),
            global: self.download.global.max(1),
            rate_limit: self.download.rate_limit.filter(|value| *value > 0),
            per_host_rate_limit: self.download.per_host_rate_limit.filter(|value| *value > 0),
            task_rate_limit: options.rate_limit.filter(|value| *value > 0),
            file_jobs: options.file_jobs.unwrap_or(self.download.file_jobs).max(1),
            content_cache: options.content_cache.unwrap_or(self.download.content_cache),
            content_cache_dir: self.content_cache_dir()?,
            disk_space_check: self.download.disk_space_check,
            mirrors: options.mirrors.clone(),
            segment_size: self.download.segment_size.max(64 * 1024),
            resume: options.resume.unwrap_or(self.download.resume),
            verify: options.verify.unwrap_or(self.download.verify),
            checksum: options.checksum.clone(),
            extract: options.extract.unwrap_or(self.download.extract),
            extract_dir: options
                .extract_dir
                .clone()
                .or_else(|| self.download.extract_dir.clone()),
            delete_archive: options
                .delete_archive
                .unwrap_or(self.download.delete_archive),
            cache: options.cache.unwrap_or(self.hub.use_cache),
            include: options.include.clone(),
            exclude: options.exclude.clone(),
            request,
            overwrite: options.overwrite,
            rename_on_conflict: options.rename_on_conflict,
            retries: self.network.retries,
            timeout: Duration::from_secs(self.network.timeout_secs.max(1)),
            connect_timeout: Duration::from_secs(self.network.connect_timeout_secs.max(1)),
            user_agent: self.network.user_agent.clone(),
        })
    }

    /// Read a config value by its dotted key, as used by `config get`.
    pub fn get_value(&self, key: &str) -> Result<String> {
        let value = match key {
            "download.dir" => self.download.dir.display().to_string(),
            "download.connections" => self.download.connections.to_string(),
            "download.per_host" => self.download.per_host.to_string(),
            "download.per_host_active" => self.download.per_host_active.to_string(),
            "download.global" => self.download.global.to_string(),
            "download.rate_limit" => self
                .download
                .rate_limit
                .map(|value| value.to_string())
                .unwrap_or_default(),
            "download.per_host_rate_limit" => self
                .download
                .per_host_rate_limit
                .map(|value| value.to_string())
                .unwrap_or_default(),
            "download.file_jobs" => self.download.file_jobs.to_string(),
            "download.content_cache" => self.download.content_cache.to_string(),
            "download.content_cache_dir" => self.content_cache_dir()?.display().to_string(),
            "download.disk_space_check" => self.download.disk_space_check.to_string(),
            "download.segment_size" => self.download.segment_size.to_string(),
            "download.resume" => self.download.resume.to_string(),
            "download.verify" => verify_mode_str(self.download.verify).to_string(),
            "download.extract" => self.download.extract.to_string(),
            "download.extract_dir" => self
                .download
                .extract_dir
                .as_ref()
                .map(|path| path.display().to_string())
                .unwrap_or_default(),
            "download.delete_archive" => self.download.delete_archive.to_string(),
            "network.timeout_secs" => self.network.timeout_secs.to_string(),
            "network.connect_timeout_secs" => self.network.connect_timeout_secs.to_string(),
            "network.retries" => self.network.retries.to_string(),
            "network.user_agent" => self.network.user_agent.clone(),
            "network.allow_cross_origin_auth" => self.network.allow_cross_origin_auth.to_string(),
            "network.proxy" => self.network.proxy.clone().unwrap_or_default(),
            "network.no_proxy" => self.network.no_proxy.clone().unwrap_or_default(),
            "hub.use_cache" => self.hub.use_cache.to_string(),
            "hub.hf_cache" => self.hf_cache_dir()?.display().to_string(),
            "hub.modelscope_cache" => self.modelscope_cache_dir()?.display().to_string(),
            "hub.hf_endpoint" => self.hf_endpoint(),
            "hub.modelscope_endpoint" => self.modelscope_endpoint(),
            "bt.dht" => self.bt.dht.to_string(),
            "bt.trackers" => self.bt.trackers.to_string(),
            "bt.listen" => self.bt.listen.to_string(),
            "bt.persistence" => self.bt.persistence.to_string(),
            "bt.peer_limit" => self
                .bt
                .peer_limit
                .map(|value| value.to_string())
                .unwrap_or_default(),
            "bt.initial_peers" => self.bt.initial_peers.join(","),
            "state_dir" => self
                .state_dir
                .as_ref()
                .map(|path| path.display().to_string())
                .unwrap_or_default(),
            _ if key.starts_with("headers.") => {
                let name = &key["headers.".len()..];
                self.headers.get(name).cloned().unwrap_or_default()
            }
            _ => return Err(Error::Config(format!("unknown config key: {key}"))),
        };
        Ok(value)
    }

    /// Set a config value by its dotted key, as used by `config set`.
    pub fn set_value(&mut self, key: &str, value: &str) -> Result<()> {
        match key {
            "download.dir" => self.download.dir = PathBuf::from(value),
            "download.connections" => self.download.connections = parse_value(value)?,
            "download.per_host" => self.download.per_host = parse_value(value)?,
            "download.per_host_active" => {
                self.download.per_host_active = parse_value::<usize>(value)?.max(1)
            }
            "download.global" => self.download.global = parse_value(value)?,
            "download.rate_limit" => {
                self.download.rate_limit = if value.trim().is_empty() {
                    None
                } else {
                    Some(parse_bytes(value)?).filter(|value| *value > 0)
                }
            }
            "download.per_host_rate_limit" => {
                self.download.per_host_rate_limit = if value.trim().is_empty() {
                    None
                } else {
                    Some(parse_bytes(value)?).filter(|value| *value > 0)
                }
            }
            "download.file_jobs" => self.download.file_jobs = parse_value::<usize>(value)?.max(1),
            "download.content_cache" => self.download.content_cache = parse_value(value)?,
            "download.content_cache_dir" => {
                self.download.content_cache_dir = if value.trim().is_empty() {
                    None
                } else {
                    Some(PathBuf::from(value))
                }
            }
            "download.disk_space_check" => self.download.disk_space_check = parse_value(value)?,
            "download.segment_size" => self.download.segment_size = parse_bytes(value)?,
            "download.resume" => self.download.resume = parse_value(value)?,
            "download.verify" => self.download.verify = parse_verify_mode(value)?,
            "download.extract" => self.download.extract = parse_value(value)?,
            "download.extract_dir" => {
                self.download.extract_dir = if value.trim().is_empty() {
                    None
                } else {
                    Some(PathBuf::from(value))
                }
            }
            "download.delete_archive" => self.download.delete_archive = parse_value(value)?,
            "network.timeout_secs" => self.network.timeout_secs = parse_value(value)?,
            "network.connect_timeout_secs" => {
                self.network.connect_timeout_secs = parse_value(value)?
            }
            "network.retries" => self.network.retries = parse_value(value)?,
            "network.user_agent" => self.network.user_agent = value.to_string(),
            "network.allow_cross_origin_auth" => {
                self.network.allow_cross_origin_auth = parse_value(value)?
            }
            "network.proxy" => {
                self.network.proxy = if value.trim().is_empty() {
                    None
                } else {
                    Some(value.to_string())
                }
            }
            "network.no_proxy" => {
                self.network.no_proxy = if value.trim().is_empty() {
                    None
                } else {
                    Some(value.to_string())
                }
            }
            "hub.use_cache" => self.hub.use_cache = parse_value(value)?,
            "hub.hf_cache" => {
                self.hub.hf_cache = if value.trim().is_empty() {
                    None
                } else {
                    Some(PathBuf::from(value))
                }
            }
            "hub.modelscope_cache" => {
                self.hub.modelscope_cache = if value.trim().is_empty() {
                    None
                } else {
                    Some(PathBuf::from(value))
                }
            }
            "hub.hf_endpoint" => {
                self.hub.hf_endpoint = if value.trim().is_empty() {
                    None
                } else {
                    Some(value.to_string())
                }
            }
            "hub.modelscope_endpoint" => {
                self.hub.modelscope_endpoint = if value.trim().is_empty() {
                    None
                } else {
                    Some(value.to_string())
                }
            }
            "bt.dht" => self.bt.dht = parse_value(value)?,
            "bt.trackers" => self.bt.trackers = parse_value(value)?,
            "bt.listen" => self.bt.listen = parse_value(value)?,
            "bt.persistence" => self.bt.persistence = parse_value(value)?,
            "bt.peer_limit" => {
                self.bt.peer_limit = if value.trim().is_empty() {
                    None
                } else {
                    Some(parse_value(value)?)
                }
            }
            "bt.initial_peers" => {
                self.bt.initial_peers = value
                    .split(',')
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .map(str::to_string)
                    .collect();
            }
            "state_dir" => {
                self.state_dir = if value.trim().is_empty() {
                    None
                } else {
                    Some(PathBuf::from(value))
                }
            }
            _ if key.starts_with("headers.") => {
                let name = key["headers.".len()..].to_string();
                Header::new(&name, value)?;
                if value.trim().is_empty() {
                    self.headers.remove(&name);
                } else {
                    self.headers.insert(name, value.to_string());
                }
            }
            _ => return Err(Error::Config(format!("unknown config key: {key}"))),
        }
        Ok(())
    }
}

fn project_dirs() -> Result<ProjectDirs> {
    ProjectDirs::from("", "", "shaul")
        .ok_or_else(|| Error::Config("cannot determine platform config directory".to_string()))
}

fn env(name: &str) -> Option<String> {
    std::env::var(name)
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}

fn env_parse<T: FromStr>(name: &str) -> Result<Option<T>> {
    match env(name) {
        Some(value) => value
            .parse::<T>()
            .map(Some)
            .map_err(|_| Error::Config(format!("invalid value for {name}: {value}"))),
        None => Ok(None),
    }
}

fn parse_value<T: FromStr>(value: &str) -> Result<T> {
    value
        .parse::<T>()
        .map_err(|_| Error::Config(format!("invalid value: {value}")))
}

/// Parse a verify mode name or one of its boolean-like aliases.
pub fn parse_verify_mode(value: &str) -> Result<VerifyMode> {
    match value.trim().to_ascii_lowercase().as_str() {
        "auto" => Ok(VerifyMode::Auto),
        "require" | "on" | "true" | "yes" | "1" => Ok(VerifyMode::Require),
        "disabled" | "off" | "false" | "no" | "0" | "none" => Ok(VerifyMode::Disabled),
        _ => Err(Error::Config(format!("invalid verify mode: {value}"))),
    }
}

/// Canonical string form of a verify mode.
pub fn verify_mode_str(mode: VerifyMode) -> &'static str {
    match mode {
        VerifyMode::Auto => "auto",
        VerifyMode::Require => "require",
        VerifyMode::Disabled => "disabled",
    }
}

/// Parse byte sizes such as `8MiB`, `256K`, `1G` or a plain byte count.
pub fn parse_bytes(value: &str) -> Result<u64> {
    let value = value.trim();
    let lower = value.to_ascii_lowercase();
    let (number, multiplier) = if let Some(number) = lower.strip_suffix("kib") {
        (number, 1024)
    } else if let Some(number) = lower.strip_suffix("mib") {
        (number, 1024 * 1024)
    } else if let Some(number) = lower.strip_suffix("gib") {
        (number, 1024 * 1024 * 1024)
    } else if let Some(number) = lower.strip_suffix("kb") {
        (number, 1000)
    } else if let Some(number) = lower.strip_suffix("mb") {
        (number, 1000 * 1000)
    } else if let Some(number) = lower.strip_suffix("gb") {
        (number, 1000 * 1000 * 1000)
    } else if let Some(number) = lower.strip_suffix('k') {
        (number, 1024)
    } else if let Some(number) = lower.strip_suffix('m') {
        (number, 1024 * 1024)
    } else if let Some(number) = lower.strip_suffix('g') {
        (number, 1024 * 1024 * 1024)
    } else {
        (lower.as_str(), 1)
    };
    let number = number.trim();
    let parsed = number
        .parse::<f64>()
        .map_err(|_| Error::Config(format!("invalid byte size: {value}")))?;
    if !parsed.is_finite() || parsed < 0.0 {
        return Err(Error::Config(format!("invalid byte size: {value}")));
    }
    Ok((parsed * multiplier as f64) as u64)
}

#[derive(Debug, Default, Deserialize)]
struct PartialConfig {
    #[serde(default)]
    download: Option<PartialDownload>,
    #[serde(default)]
    network: Option<PartialNetwork>,
    #[serde(default)]
    hub: Option<PartialHub>,
    #[serde(default)]
    bt: Option<PartialBt>,
    #[serde(default)]
    state_dir: Option<PathBuf>,
    #[serde(default)]
    headers: Option<BTreeMap<String, String>>,
}

#[derive(Debug, Default, Deserialize)]
struct PartialDownload {
    #[serde(default)]
    dir: Option<PathBuf>,
    #[serde(default)]
    connections: Option<usize>,
    #[serde(default)]
    per_host: Option<usize>,
    #[serde(default)]
    per_host_active: Option<usize>,
    #[serde(default)]
    global: Option<usize>,
    #[serde(default)]
    rate_limit: Option<u64>,
    #[serde(default)]
    per_host_rate_limit: Option<u64>,
    #[serde(default)]
    file_jobs: Option<usize>,
    #[serde(default)]
    content_cache: Option<bool>,
    #[serde(default)]
    content_cache_dir: Option<PathBuf>,
    #[serde(default)]
    disk_space_check: Option<bool>,
    #[serde(default)]
    segment_size: Option<u64>,
    #[serde(default)]
    resume: Option<bool>,
    #[serde(default)]
    verify: Option<VerifyMode>,
    #[serde(default)]
    extract: Option<bool>,
    #[serde(default)]
    extract_dir: Option<PathBuf>,
    #[serde(default)]
    delete_archive: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
struct PartialNetwork {
    #[serde(default)]
    timeout_secs: Option<u64>,
    #[serde(default)]
    connect_timeout_secs: Option<u64>,
    #[serde(default)]
    retries: Option<u32>,
    #[serde(default)]
    user_agent: Option<String>,
    #[serde(default)]
    allow_cross_origin_auth: Option<bool>,
    #[serde(default)]
    proxy: Option<String>,
    #[serde(default)]
    no_proxy: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct PartialHub {
    #[serde(default)]
    use_cache: Option<bool>,
    #[serde(default)]
    hf_cache: Option<PathBuf>,
    #[serde(default)]
    modelscope_cache: Option<PathBuf>,
    #[serde(default)]
    hf_endpoint: Option<String>,
    #[serde(default)]
    modelscope_endpoint: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct PartialBt {
    #[serde(default)]
    dht: Option<bool>,
    #[serde(default)]
    trackers: Option<bool>,
    #[serde(default)]
    listen: Option<bool>,
    #[serde(default)]
    persistence: Option<bool>,
    #[serde(default)]
    peer_limit: Option<usize>,
    #[serde(default)]
    initial_peers: Option<Vec<String>>,
}

impl PartialConfig {
    fn apply(self, config: &mut Config) -> Result<()> {
        if let Some(download) = self.download {
            if let Some(value) = download.dir {
                config.download.dir = value;
            }
            if let Some(value) = download.connections {
                config.download.connections = value;
            }
            if let Some(value) = download.per_host {
                config.download.per_host = value;
            }
            if let Some(value) = download.per_host_active {
                config.download.per_host_active = value.max(1);
            }
            if let Some(value) = download.global {
                config.download.global = value;
            }
            if let Some(value) = download.rate_limit {
                config.download.rate_limit = (value > 0).then_some(value);
            }
            if let Some(value) = download.per_host_rate_limit {
                config.download.per_host_rate_limit = (value > 0).then_some(value);
            }
            if let Some(value) = download.file_jobs {
                config.download.file_jobs = value.max(1);
            }
            if let Some(value) = download.content_cache {
                config.download.content_cache = value;
            }
            if let Some(value) = download.content_cache_dir {
                config.download.content_cache_dir =
                    (!value.as_os_str().is_empty()).then_some(value);
            }
            if let Some(value) = download.disk_space_check {
                config.download.disk_space_check = value;
            }
            if let Some(value) = download.segment_size {
                config.download.segment_size = value;
            }
            if let Some(value) = download.resume {
                config.download.resume = value;
            }
            if let Some(value) = download.verify {
                config.download.verify = value;
            }
            if let Some(value) = download.extract {
                config.download.extract = value;
            }
            if let Some(value) = download.extract_dir {
                config.download.extract_dir = Some(value);
            }
            if let Some(value) = download.delete_archive {
                config.download.delete_archive = value;
            }
        }
        if let Some(network) = self.network {
            if let Some(value) = network.timeout_secs {
                config.network.timeout_secs = value;
            }
            if let Some(value) = network.connect_timeout_secs {
                config.network.connect_timeout_secs = value;
            }
            if let Some(value) = network.retries {
                config.network.retries = value;
            }
            if let Some(value) = network.user_agent {
                config.network.user_agent = value;
            }
            if let Some(value) = network.allow_cross_origin_auth {
                config.network.allow_cross_origin_auth = value;
            }
            if let Some(value) = network.proxy {
                config.network.proxy = (!value.trim().is_empty()).then_some(value);
            }
            if let Some(value) = network.no_proxy {
                config.network.no_proxy = (!value.trim().is_empty()).then_some(value);
            }
        }
        if let Some(hub) = self.hub {
            if let Some(value) = hub.use_cache {
                config.hub.use_cache = value;
            }
            if let Some(value) = hub.hf_cache {
                config.hub.hf_cache = Some(value);
            }
            if let Some(value) = hub.modelscope_cache {
                config.hub.modelscope_cache = Some(value);
            }
            if let Some(value) = hub.hf_endpoint {
                config.hub.hf_endpoint = Some(value);
            }
            if let Some(value) = hub.modelscope_endpoint {
                config.hub.modelscope_endpoint = Some(value);
            }
        }
        if let Some(bt) = self.bt {
            if let Some(value) = bt.dht {
                config.bt.dht = value;
            }
            if let Some(value) = bt.trackers {
                config.bt.trackers = value;
            }
            if let Some(value) = bt.listen {
                config.bt.listen = value;
            }
            if let Some(value) = bt.persistence {
                config.bt.persistence = value;
            }
            if let Some(value) = bt.peer_limit {
                config.bt.peer_limit = Some(value);
            }
            if let Some(value) = bt.initial_peers {
                config.bt.initial_peers = value;
            }
        }
        if let Some(value) = self.state_dir {
            config.state_dir = Some(value);
        }
        if let Some(headers) = self.headers {
            for (name, value) in headers {
                Header::new(&name, &value)?;
                config.headers.insert(name, value);
            }
        }
        Ok(())
    }
}

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

    #[test]
    fn file_overrides_environment_layer() {
        let mut config = Config::default();
        config.download.connections = 3; // pretend env set this
        let partial: PartialConfig = toml::from_str("[download]\nconnections = 11\n").unwrap();
        partial.apply(&mut config).unwrap();
        assert_eq!(config.download.connections, 11);
    }

    #[test]
    fn parses_byte_sizes() {
        assert_eq!(parse_bytes("8MiB").unwrap(), 8 * 1024 * 1024);
        assert_eq!(parse_bytes("1.5G").unwrap(), 1_610_612_736);
    }

    #[test]
    fn gets_and_sets_values() {
        let mut config = Config::default();
        config.set_value("download.connections", "12").unwrap();
        assert_eq!(config.get_value("download.connections").unwrap(), "12");
        config.set_value("download.rate_limit", "2MiB").unwrap();
        assert_eq!(
            config.get_value("download.rate_limit").unwrap(),
            (2 * 1024 * 1024).to_string()
        );
        config
            .set_value("download.per_host_rate_limit", "1MiB")
            .unwrap();
        assert_eq!(
            config.get_value("download.per_host_rate_limit").unwrap(),
            (1024 * 1024).to_string()
        );
        config.set_value("download.file_jobs", "3").unwrap();
        assert_eq!(config.get_value("download.file_jobs").unwrap(), "3");
        config.set_value("download.content_cache", "true").unwrap();
        assert_eq!(config.get_value("download.content_cache").unwrap(), "true");
        config
            .set_value("download.content_cache_dir", "/tmp/shaul-content")
            .unwrap();
        assert_eq!(
            config.get_value("download.content_cache_dir").unwrap(),
            Path::new("/tmp/shaul-content").display().to_string()
        );
        config.set_value("download.per_host_active", "7").unwrap();
        assert_eq!(config.get_value("download.per_host_active").unwrap(), "7");
        config
            .set_value("network.proxy", "http://127.0.0.1:8080")
            .unwrap();
        assert_eq!(
            config.get_value("network.proxy").unwrap(),
            "http://127.0.0.1:8080"
        );
        config
            .set_value("network.no_proxy", "localhost,127.0.0.1")
            .unwrap();
        assert_eq!(
            config.get_value("network.no_proxy").unwrap(),
            "localhost,127.0.0.1"
        );
        config.set_value("headers.X-Test", "yes").unwrap();
        assert_eq!(config.get_value("headers.X-Test").unwrap(), "yes");
    }

    #[test]
    fn resolves_proxy_and_rate_limits() {
        let mut config = Config::default();
        config.download.rate_limit = Some(1024);
        config.download.per_host_rate_limit = Some(512);
        config.network.proxy = Some("http://127.0.0.1:8080".to_string());
        config.network.no_proxy = Some("localhost".to_string());
        config.download.file_jobs = 4;
        config.download.content_cache = true;
        let options = TaskOptions {
            rate_limit: Some(2048),
            file_jobs: Some(2),
            content_cache: Some(false),
            ..TaskOptions::default()
        };
        let resolved = config.resolve(Path::new("."), &options).unwrap();
        assert_eq!(resolved.rate_limit, Some(1024));
        assert_eq!(resolved.per_host_rate_limit, Some(512));
        assert_eq!(resolved.task_rate_limit, Some(2048));
        assert_eq!(resolved.file_jobs, 2);
        assert_eq!(resolved.per_host_active, 16);
        assert!(!resolved.content_cache);
        assert!(resolved.content_cache_dir.ends_with("content"));

        let partial: PartialConfig = toml::from_str(
            "[download]\nrate_limit = 0\n[network]\nproxy = \"\"\nno_proxy = \"\"\n",
        )
        .unwrap();
        partial.apply(&mut config).unwrap();
        assert_eq!(config.download.rate_limit, None);
        assert_eq!(config.network.proxy, None);
        assert_eq!(config.network.no_proxy, None);
    }
}