aria2-core 0.2.2

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, session persistence, and BitTorrent seeding
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
use std::collections::HashMap;
use std::fmt;

use super::option::{OptionRegistry, OptionType, OptionValue};

#[derive(Debug, Clone)]
pub enum ConfigSource {
    Defaults,
    Environment,
    ConfigFile,
    CommandLine,
}

impl fmt::Display for ConfigSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Defaults => write!(f, "defaults"),
            Self::Environment => write!(f, "environment"),
            Self::ConfigFile => write!(f, "config-file"),
            Self::CommandLine => write!(f, "command-line"),
        }
    }
}

#[derive(Debug, Clone)]
pub struct ConfigError {
    pub source: ConfigSource,
    pub option: String,
    pub message: String,
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}: {}", self.source, self.option, self.message)
    }
}

pub struct ConfigParser {
    options: HashMap<String, OptionValue>,
    sources: Vec<ConfigSource>,
    errors: Vec<ConfigError>,
    registry: OptionRegistry,
}

impl ConfigParser {
    pub fn new() -> Self {
        Self {
            options: HashMap::new(),
            sources: Vec::new(),
            errors: Vec::new(),
            registry: OptionRegistry::new(),
        }
    }

    pub fn with_registry(registry: OptionRegistry) -> Self {
        Self {
            registry,
            ..Self::new()
        }
    }

    pub fn set(&mut self, name: impl Into<String>, value: OptionValue) {
        let key = name.into();
        if let Some(def) = self.registry.get(key.as_str()) {
            match def.parse_value(&value.to_string()) {
                Ok(v) => {
                    self.options.insert(key, v);
                }
                Err(e) => self.errors.push(ConfigError {
                    source: ConfigSource::CommandLine,
                    option: key.clone(),
                    message: e,
                }),
            }
        } else {
            self.options.insert(key, value);
        }
    }

    pub fn set_raw(&mut self, name: impl Into<String>, value: impl Into<String>) {
        let key = name.into();
        if let Some(def) = self.registry.get(key.as_str()) {
            match def.parse_value(&value.into()) {
                Ok(v) => {
                    self.options.insert(key, v);
                }
                Err(e) => self.errors.push(ConfigError {
                    source: ConfigSource::CommandLine,
                    option: key.clone(),
                    message: e,
                }),
            }
        } else {
            self.options.insert(key, OptionValue::Str(value.into()));
        }
    }

    pub fn get(&self, name: &str) -> Option<&OptionValue> {
        self.options.get(name)
    }
    pub fn get_str(&self, name: &str) -> Option<&str> {
        self.options.get(name).and_then(|v| v.as_str())
    }
    pub fn get_i64(&self, name: &str) -> Option<i64> {
        self.options.get(name).and_then(|v| v.as_i64())
    }
    pub fn get_bool(&self, name: &str) -> Option<bool> {
        self.options.get(name).and_then(|v| v.as_bool())
    }
    pub fn contains(&self, name: &str) -> bool {
        self.options.contains_key(name)
    }

    pub fn parse_cli_args(&mut self, args: &[&str]) {
        self.sources.push(ConfigSource::CommandLine);
        let mut i = 0;
        while i < args.len() {
            let arg = &args[i];
            if let Some(opt_name) = arg.strip_prefix("--") {
                if opt_name.starts_with("no-") && opt_name.len() > 3 {
                    let real_name = &opt_name[3..];
                    self.set(real_name, OptionValue::Bool(false));
                } else if opt_name.contains('=') {
                    let parts: Vec<&str> = opt_name.splitn(2, '=').collect();
                    if parts.len() == 2 {
                        self.set_raw(parts[0], parts[1]);
                    }
                } else if i + 1 < args.len() && !args[i + 1].starts_with('-') {
                    self.set_raw(opt_name, args[i + 1]);
                    i += 1;
                } else {
                    if let Some(def) = self.registry.get(opt_name) {
                        if def.opt_type() == OptionType::Boolean {
                            self.set(opt_name, OptionValue::Bool(true));
                        } else {
                            self.set_raw(opt_name, "");
                        }
                    } else {
                        self.set_raw(opt_name, "");
                    }
                }
            } else if arg.starts_with('-') && arg.len() == 2 {
                let c = arg.chars().nth(1).unwrap();
                let opt_name = self
                    .registry
                    .all()
                    .values()
                    .find(|def| def.short_name() == Some(c))
                    .map(|def| def.name().to_string());
                if let Some(name) = opt_name {
                    if i + 1 < args.len() && !args[i + 1].starts_with('-') {
                        self.set_raw(&name, args[i + 1]);
                        i += 1;
                    } else {
                        self.set(&name, OptionValue::Bool(true));
                    }
                }
            } else if let Some(rest) = arg.strip_prefix('@') {
                self.parse_file(rest);
            } else {
                i += 1;
            }
            i += 1;
        }
    }

    pub fn parse_file(&mut self, path: &str) {
        self.sources.push(ConfigSource::ConfigFile);
        if let Ok(content) = std::fs::read_to_string(path) {
            for line in content.lines() {
                let line = line.trim();
                if line.is_empty()
                    || line.starts_with('#')
                    || line.starts_with('[')
                    || line.starts_with(';')
                {
                    continue;
                }
                if let Some(eq_pos) = line.find('=') {
                    let name = line[..eq_pos].trim();
                    let value = line[eq_pos + 1..].trim();
                    if !name.is_empty() {
                        self.set_raw(name, value);
                    }
                }
            }
        }
    }

    pub fn parse_env_vars(&mut self) {
        self.sources.push(ConfigSource::Environment);
        for (key, value) in std::env::vars() {
            if let Some(rest) = key.strip_prefix("ARIA2_") {
                let opt_name = rest.to_lowercase().replace('_', "-");
                self.set_raw(opt_name, &value);
            }
        }
    }

    pub fn apply_defaults(&mut self) {
        self.sources.push(ConfigSource::Defaults);
        for def in self.registry.all().values() {
            if !matches!(def.default_value(), OptionValue::None) {
                self.options
                    .entry(def.name().to_string())
                    .or_insert_with(|| def.default_value().clone());
            }
        }
    }

    pub fn load_defaults_first(&mut self) {
        self.apply_defaults();
        self.parse_env_vars();
        let conf_path = self
            .get_str("conf-path")
            .map(|s| s.to_string())
            .unwrap_or_default();
        if !conf_path.is_empty() && std::path::Path::new(&conf_path).exists() {
            self.parse_file(&conf_path);
        }
    }

    pub fn options(&self) -> &HashMap<String, OptionValue> {
        &self.options
    }
    pub fn errors(&self) -> &[ConfigError] {
        &self.errors
    }
    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }
    pub fn source_count(&self) -> usize {
        self.sources.len()
    }

    pub fn to_json_map(&self) -> serde_json::Value {
        let mut map = serde_json::Map::new();
        for (k, v) in &self.options {
            map.insert(
                k.clone(),
                <&OptionValue as Into<serde_json::Value>>::into(v),
            );
        }
        serde_json::Value::Object(map)
    }
}

impl Default for ConfigParser {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_config_source_display() {
        assert_eq!(ConfigSource::CommandLine.to_string(), "command-line");
        assert_eq!(ConfigSource::Defaults.to_string(), "defaults");
    }

    #[test]
    fn test_parser_creation() {
        let p = ConfigParser::new();
        assert_eq!(p.source_count(), 0);
        assert!(!p.has_errors());
    }

    #[test]
    fn test_set_and_get() {
        let mut p = ConfigParser::new();
        p.set("dir", OptionValue::Str("/downloads".into()));
        assert_eq!(p.get_str("dir").unwrap(), "/downloads");
        assert!(p.contains("dir"));
        assert!(!p.contains("nonexistent"));
    }

    #[test]
    fn test_set_raw_string() {
        let mut p = ConfigParser::new();
        p.set_raw("split", "8");
        assert_eq!(p.get_i64("split").unwrap(), 8);
    }

    #[test]
    fn test_set_raw_bool() {
        let mut p = ConfigParser::new();
        p.set_raw("check-certificate", "false");
        assert!(!p.get_bool("check-certificate").unwrap());
    }

    #[test]
    fn test_parse_cli_args_basic() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--dir=/tmp", "--split=3", "--quiet"]);
        assert_eq!(p.get_str("dir").unwrap(), "/tmp");
        assert_eq!(p.get_i64("split").unwrap(), 3);
        assert!(p.get_bool("quiet").unwrap());
    }

    #[test]
    fn test_parse_cli_args_no_prefix() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--no-check-certificate", "--no-continue"]);
        assert!(!p.get_bool("check-certificate").unwrap());
        assert!(!p.get_bool("continue").unwrap());
    }

    #[test]
    fn test_parse_cli_args_space_separated() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--dir", "/opt/downloads", "--out", "file.iso"]);
        assert_eq!(p.get_str("dir").unwrap(), "/opt/downloads");
        assert_eq!(p.get_str("out").unwrap(), "file.iso");
    }

    #[test]
    fn test_parse_cli_boolean_flag() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["-q", "--dry-run"]);
        assert!(p.get_bool("quiet").unwrap());
        assert!(p.get_bool("dry-run").unwrap());
    }

    #[test]
    fn test_apply_defaults() {
        let mut p = ConfigParser::new();
        p.apply_defaults();
        assert_eq!(p.get_str("dir").unwrap(), ".");
        assert_eq!(p.get_i64("split").unwrap(), 5);
        assert!(p.get_bool("enable-color").unwrap());
    }

    #[test]
    fn test_load_order() {
        let mut p = ConfigParser::new();
        p.load_defaults_first();
        p.set("dir", OptionValue::Str("/override".into()));
        assert_eq!(p.get_str("dir").unwrap(), "/override");
    }

    #[test]
    fn test_to_json_map() {
        let mut p = ConfigParser::new();
        p.set("dir", OptionValue::Str("/tmp".into()));
        p.set("split", OptionValue::Int(10));
        let map = p.to_json_map();
        assert!(map.get("dir").is_some());
        assert!(map.get("split").is_some());
    }

    #[test]
    fn test_error_on_invalid_integer() {
        let mut p = ConfigParser::new();
        p.set_raw("split", "not_a_number");
        assert!(p.has_errors());
        assert_eq!(p.errors()[0].option, "split");
    }

    #[test]
    fn test_error_on_out_of_range() {
        let mut p = ConfigParser::new();
        p.set_raw("split", "100");
        assert!(p.has_errors());
    }

    #[test]
    fn test_error_display() {
        let err = ConfigError {
            source: ConfigSource::CommandLine,
            option: "split".into(),
            message: "value 100 exceeds maximum 16".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("split"));
        assert!(s.contains("command-line"));
    }

    #[test]
    fn test_default_parser() {
        let p = ConfigParser::default();
        assert_eq!(p.source_count(), 0);
    }

    // --- Phase 13 / Wave D — Task D6: 10 CLI Option Parsing Tests ---

    #[test]
    fn test_d6_01_short_option_dir_maps_to_tmp() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["-d", "/tmp"]);
        assert_eq!(p.get_str("dir").unwrap(), "/tmp");
    }

    #[test]
    fn test_d6_02_short_option_split_maps_to_8() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["-s", "8"]);
        assert_eq!(p.get_i64("split").unwrap(), 8);
    }

    #[test]
    fn test_d6_03_long_option_timeout_equals_30() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--timeout=30"]);
        assert_eq!(p.get_i64("timeout").unwrap(), 30);
    }

    #[test]
    fn test_d6_04_boolean_flag_quiet_sets_true() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--quiet"]);
        assert!(p.get_bool("quiet").unwrap());
    }

    #[test]
    fn test_d6_05_list_option_header_parses_correctly() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--header=X:Y,Z:W"]);
        let val = p.get("header").unwrap();
        let list = val.as_list().unwrap();
        assert_eq!(list.len(), 2);
        assert_eq!(list[0], "X:Y");
        assert_eq!(list[1], "Z:W");
    }

    #[test]
    fn test_d6_06_unknown_option_returns_error() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--totally-nonexistent-option"]);
        assert!(p.contains("totally-nonexistent-option"));
        let val = p.get("totally-nonexistent-option").unwrap();
        assert_eq!(val.as_str().unwrap(), "");
    }

    #[test]
    fn test_d6_07_out_of_range_value_rejected() {
        let mut p = ConfigParser::new();
        p.set_raw("split", "0");
        assert!(p.has_errors());
        assert!(p.errors()[0].option == "split");
    }

    #[test]
    fn test_d6_08_default_values_applied_when_not_specified() {
        let mut p = ConfigParser::new();
        p.apply_defaults();
        assert_eq!(p.get_str("dir").unwrap(), ".");
        assert_eq!(p.get_i64("split").unwrap(), 5);
        assert_eq!(p.get_i64("timeout").unwrap(), 60);
        assert!(!p.get_bool("quiet").unwrap());
    }

    #[test]
    fn test_d6_09_multiple_options_parsed_together() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&[
            "--dir=/downloads",
            "--split=4",
            "--timeout=30",
            "--quiet",
            "--out=output.bin",
        ]);
        assert_eq!(p.get_str("dir").unwrap(), "/downloads");
        assert_eq!(p.get_i64("split").unwrap(), 4);
        assert_eq!(p.get_i64("timeout").unwrap(), 30);
        assert!(p.get_bool("quiet").unwrap());
        assert_eq!(p.get_str("out").unwrap(), "output.bin");
    }

    #[test]
    fn test_d6_10_help_flag_skipped_without_error() {
        let mut p = ConfigParser::new();
        p.parse_cli_args(&["--help", "--version", "-h"]);
        assert_eq!(p.source_count(), 1);
        assert!(!p.has_errors());
    }
}