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
use crate::Vector;
use crate::{Flag, FlagValue};
use std::collections::VecDeque;
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub struct Context {
    pub raw_args: Vec<String>,
    pub args: VecDeque<String>,
    pub common_flags: Vector<Flag>,
    pub local_flags: Vector<Flag>,
    pub current_path: PathBuf,
    pub common_flags_values: Vector<(String, FlagValue)>,
    pub local_flags_values: Vector<(String, FlagValue)>,
    pub unknown_flags: Vector<(String, FlagValue)>,
}

impl Context {
    pub fn new(
        raw_args: Vec<String>,
        args: VecDeque<String>,
        common_flags: Vector<Flag>,
        local_flags: Vector<Flag>,
        current_path: &str,
    ) -> Context {
        Context {
            raw_args,
            args,
            common_flags,
            local_flags,
            current_path: PathBuf::from(current_path),
            common_flags_values: Vector::default(),
            local_flags_values: Vector::default(),
            unknown_flags: Vector::default(),
        }
    }
    pub fn build_new(
        raw_args: Vec<String>,
        args: VecDeque<String>,
        common_flags: Vector<Flag>,
        local_flags: Vector<Flag>,
        current_path: PathBuf,
        common_flags_values: Vector<(String, FlagValue)>,
        local_flags_values: Vector<(String, FlagValue)>,
        unknown_flags: Vector<(String, FlagValue)>,
    ) -> Context {
        Context {
            raw_args,
            args,
            common_flags,
            local_flags,
            current_path,
            common_flags_values,
            local_flags_values,
            unknown_flags,
        }
    }

    pub fn root() -> Option<Context> {
        None
    }

    pub fn args(mut self, args: VecDeque<String>) -> Self {
        self.args = args;
        self
    }

    pub fn current(&self) -> &Path {
        &self.current_path
    }

    pub fn change_current(&mut self, path: PathBuf) {
        self.current_path = path;
    }
}

impl From<Vec<String>> for Context {
    fn from(raw_args: Vec<String>) -> Context {
        let args = VecDeque::from(raw_args.clone());
        let current_path = match &raw_args.get(0) {
            Some(str) => PathBuf::from(str),
            None => PathBuf::new(),
        };
        Context {
            raw_args,
            args,
            common_flags: Vector::default(),
            local_flags: Vector::default(),
            current_path,
            common_flags_values: Vector::default(),
            local_flags_values: Vector::default(),
            unknown_flags: Vector::default(),
        }
    }
}