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
use crate::{
    config::Config,
    opts::{Action, Opts},
    utils::{check_file, check_folder},
};
use anyhow::Result;
use std::path::PathBuf;

#[derive(Debug)]
pub struct Setup {
    pub action: Action,
    pub config: Config,
    pub path: PathBuf,
}

impl TryFrom<Opts> for Setup {
    type Error = anyhow::Error;

    fn try_from(opts: Opts) -> Result<Self> {
        let action = opts.action;
        let config = Config::create()?;
        match action {
            Action::Set(_) => {
                let path = PathBuf::try_from(&action)?;
                check_folder(&path)?;
                return Ok(Self {
                    action,
                    config,
                    path,
                });
            }
            Action::Var(_) => {
                let path = PathBuf::try_from(&action)?;
                check_file(&path)?;
                return Ok(Self {
                    action,
                    config,
                    path,
                });
            }
            Action::Use(ref val) => {
                let path = PathBuf::try_from(&action)?;
                let pages = &val.pages;
                if pages.is_empty() {
                    return Err(anyhow::anyhow!("No pages provided"));
                }
                return Ok(Self {
                    action,
                    config,
                    path,
                });
            }
            Action::List => {
                let path = PathBuf::try_from(&action)?;
                check_folder(&path)?;
                return Ok(Self {
                    action,
                    config,
                    path,
                });
            }
            Action::Config => {
                let path = PathBuf::try_from(&action)?;
                Ok(Self {
                    action,
                    config,
                    path,
                })
            }
        }
    }
}

impl TryFrom<&Action> for PathBuf {
    type Error = anyhow::Error;

    fn try_from(action: &Action) -> Result<Self> {
        match &action {
            Action::Set(path) => Ok(path.path.to_owned()),
            Action::Use(add) => {
                if let Some(path) = &add.path {
                    Ok(path.to_owned())
                } else {
                    Ok(PathBuf::from("."))
                }
            }
            Action::Var(path) => Ok(path.path.to_owned()),
            Action::List => Ok(PathBuf::from(".")),
            Action::Config => Ok(PathBuf::from(".")),
        }
    }
}

#[cfg(test)]
mod test {
    use super::Setup;
    use crate::opts::{Action, Opts, Path};
    use anyhow::Result;
    use std::path::PathBuf;

    #[test]
    fn setup_print() -> Result<()> {
        let setup: Setup = Opts {
            action: Action::Config,
        }
        .try_into()?;
        assert_eq!(setup.action, Action::Config);
        assert_eq!(setup.path, PathBuf::from("."));
        return Ok(());
    }

    #[test]
    fn setup_set() -> Result<()> {
        let template_path = PathBuf::from("/templates");
        let setup: Result<Setup> = Opts {
            action: Action::Set(Path {
                path: template_path.clone(),
            }),
        }
        .try_into();
        if template_path.is_dir() {
            assert!(setup.is_ok());
        } else {
            assert!(setup.is_err());
        }
        return Ok(());
    }
}