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
use crate::{
opts::{Action, Add, Opts, Set},
utils::{check_folder, get_config_path},
};
use anyhow::{Context, Result};
use std::{io::Write, path::PathBuf};
#[derive(Debug, PartialEq)]
pub struct Config {
pub templates_path: PathBuf,
}
#[derive(Debug)]
pub struct Setup {
pub action: Action,
pub config: Config,
pub path: PathBuf,
}
impl Action {
fn copy(&self) -> Self {
match self {
Action::Set(set) => Action::Set(Set {
path: set.path.clone(),
}),
Action::Add(add) => Action::Add(Add {
lib: add.lib.clone(),
pages: add.pages.clone(),
path: add.path.clone(),
}),
Action::Print => Action::Print,
}
}
}
impl Config {
fn create() -> Result<Self> {
let config_path = get_config_path()?;
if std::fs::metadata(&config_path).is_err() {
let mut file =
std::fs::File::create(&config_path).context("Config file not created")?;
file.write_all(b"{\"templates_path\": \"~/tmp\"}")
.context("Config file not written")?;
return Ok(Config {
templates_path: PathBuf::from("~/tmp"),
});
}
let config = std::fs::read_to_string(&config_path).context("Config not found")?;
let config: serde_json::Value =
serde_json::from_str(&config).context("Config not valid")?;
let templates_path = config
.get("templates_path")
.and_then(|v| v.as_str())
.map(|v| PathBuf::from(v))
.context("Templates folder path not found in config")?;
return Ok(Config { templates_path });
}
}
impl TryFrom<&Opts> for PathBuf {
type Error = anyhow::Error;
fn try_from(opts: &Opts) -> Result<Self> {
match &opts.action {
Action::Set(set) => Ok(set.path.to_owned()),
Action::Add(add) => {
if let Some(path) = &add.path {
Ok(path.to_owned())
} else {
Ok(PathBuf::from("."))
}
}
Action::Print => Ok(PathBuf::from(".")),
}
}
}
impl TryFrom<Opts> for Setup {
type Error = anyhow::Error;
fn try_from(opts: Opts) -> Result<Self> {
let copy = opts.action.copy();
let config = Config::create()?;
let path = PathBuf::try_from(&opts)?;
match opts.action {
Action::Set(set) => {
check_folder(&set.path)?;
return Ok(Self {
action: copy,
config,
path,
});
}
Action::Add(add) => {
let pages = add.pages;
if pages.is_empty() {
return Err(anyhow::anyhow!("No pages provided"));
}
return Ok(Self {
action: copy,
config,
path,
});
}
Action::Print => Ok(Self {
action: copy,
config,
path,
}),
}
}
}
#[cfg(test)]
mod test {
use super::Setup;
use crate::opts::{Action, Opts, Set};
use anyhow::Result;
use std::path::PathBuf;
#[test]
fn setup_print() -> Result<()> {
let setup: Setup = Opts {
action: Action::Print,
}
.try_into()?;
assert_eq!(setup.action, Action::Print);
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(Set {
path: template_path.clone(),
}),
}
.try_into();
if template_path.is_dir() {
assert!(setup.is_ok());
} else {
assert!(setup.is_err());
}
return Ok(());
}
}