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
extern crate failure;
#[macro_use]
extern crate failure_derive;
#[macro_use]
extern crate nom;
#[macro_use]
extern crate log;
pub mod errors;
pub mod keywords;
pub mod parser;
pub mod scc;
pub mod types;
use errors::{Error, Result};
use std::path::{Path, PathBuf};
use types::Config;
#[derive(Debug, Default)]
pub struct ConfigBuilder {
in_files: Vec<PathBuf>,
out_file: Option<PathBuf>,
include_paths: Vec<PathBuf>,
single_module: bool,
no_output: bool,
error_cycle: bool,
headers: bool,
}
impl ConfigBuilder {
pub fn new<P: AsRef<Path>>(
in_files: &[P],
output: Option<&P>,
output_dir: Option<&P>,
include_paths: &[P],
) -> Result<ConfigBuilder> {
let in_files = in_files
.iter()
.map(|f| f.as_ref().into())
.collect::<Vec<PathBuf>>();
let output = output.map(|f| f.as_ref().into());
let output_dir: Option<PathBuf> = output_dir.map(|f| f.as_ref().into());
let mut include_paths = include_paths
.iter()
.map(|f| f.as_ref().into())
.collect::<Vec<PathBuf>>();
if in_files.is_empty() {
return Err(Error::NoProto);
}
for f in &in_files {
if !f.exists() {
return Err(Error::InputFile(format!("{}", f.display())));
}
}
let out_file = match (output, output_dir) {
(Some(_), None) if in_files.len() > 1 => return Err(Error::OutputMultipleInputs),
(Some(output), None) => Some(output),
(None, Some(output_dir)) => {
if !output_dir.is_dir() {
return Err(Error::OutputDirectory(format!("{}", output_dir.display())));
}
Some(output_dir)
}
(Some(_), Some(_)) => return Err(Error::OutputAndOutputDir),
(None, None) => None,
};
let default = PathBuf::from(".");
if include_paths.is_empty() || !include_paths.contains(&default) {
include_paths.push(default);
}
Ok(ConfigBuilder {
in_files: in_files,
out_file: out_file,
include_paths: include_paths,
headers: true,
..Default::default()
})
}
pub fn single_module(mut self, val: bool) -> Self {
self.single_module = val;
self
}
pub fn no_output(mut self, val: bool) -> Self {
self.no_output = val;
self
}
pub fn error_cycle(mut self, val: bool) -> Self {
self.error_cycle = val;
self
}
pub fn headers(mut self, val: bool) -> Self {
self.headers = val;
self
}
pub fn build(self) -> Vec<Config> {
self.in_files
.iter()
.map(|in_file| {
let mut out_file = in_file.with_extension("rs");
if let Some(ref ofile) = self.out_file {
if ofile.is_dir() {
out_file = ofile.join(out_file.file_name().unwrap());
} else {
out_file = ofile.into();
}
}
Config {
in_file: in_file.to_owned(),
out_file: out_file,
import_search_path: self.include_paths.clone(),
single_module: self.single_module,
no_output: self.no_output,
error_cycle: self.error_cycle,
headers: self.headers,
}
}).collect()
}
}