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
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use consts::{
MOD_COMMENT,
FILE_COMMENT,
FILE,
END_INFO,
OUTPUT_COMMENT_FILE,
};
use types::TypeStruct;
pub fn loop_over_files<S>(path: &Path, func: &mut FnMut(&Path, &str),
files_to_ignore: &[S], verbose: bool)
where S: AsRef<Path> {
do_loop_over_files(path.as_ref(), path.as_ref(), func, files_to_ignore, verbose)
}
pub fn do_loop_over_files<S>(work_dir: &Path, path: &Path,
func: &mut FnMut(&Path, &str), files_to_ignore: &[S], verbose: bool)
where S: AsRef<Path> {
match fs::read_dir(path) {
Ok(it) => {
let mut entries = vec!();
for entry in it {
entries.push(entry.unwrap().path().to_owned());
}
entries.sort();
for entry in entries {
check_path_type(work_dir, &entry, func, files_to_ignore, verbose);
}
}
Err(e) => {
println!("Error while trying to iterate over {}: {}", path.display(), e);
}
}
}
fn check_path_type<S>(work_dir: &Path, path: &Path, func: &mut FnMut(&Path, &str),
files_to_ignore: &[S], verbose: bool)
where S: AsRef<Path> {
match fs::metadata(path) {
Ok(m) => {
if m.is_dir() {
if path == Path::new("..") || path == Path::new(".") {
return;
}
do_loop_over_files(work_dir, path, func, files_to_ignore, verbose);
} else {
let path_suffix = strip_prefix(path, work_dir).unwrap();
let ignore = path == Path::new(&format!("./{}", OUTPUT_COMMENT_FILE)) ||
path.extension() != Some(OsStr::new("rs")) ||
files_to_ignore.iter().any(|s| s.as_ref() == path_suffix);
if ignore {
if verbose {
println!("-> {}: ignored", path.display());
}
return;
}
if verbose {
println!("-> {}", path.display());
}
func(work_dir, path_suffix.to_str().unwrap());
}
}
Err(e) => {
println!("An error occurred on '{}': {}", path.display(), e);
}
}
}
pub fn join(s: &[String], join_part: &str) -> String {
let mut ret = String::new();
let mut it = 0;
while it < s.len() {
ret.push_str(&s[it]);
it += 1;
if it < s.len() {
ret.push_str(join_part);
}
}
ret
}
fn strip_prefix<'a>(self_: &'a Path, base: &'a Path)
-> Result<&'a Path, ()> {
iter_after(self_.components(), base.components())
.map(|c| c.as_path())
.ok_or((()))
}
fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
where I: Iterator<Item = A> + Clone,
J: Iterator<Item = A>,
A: PartialEq
{
loop {
let mut iter_next = iter.clone();
match (iter_next.next(), prefix.next()) {
(Some(x), Some(y)) => {
if x != y {
return None;
}
}
(Some(_), None) => return Some(iter),
(None, None) => return Some(iter),
(None, Some(_)) => return None,
}
iter = iter_next;
}
}
pub fn write_comment(id: &TypeStruct, comment: &str,
ignore_macro: bool) -> String {
if ignore_macro {
format!("{}{}{}\n{}", MOD_COMMENT, id, END_INFO, comment)
} else {
format!("{}{:?}{}\n{}", MOD_COMMENT, id, END_INFO, comment)
}
}
pub fn write_item_doc<F>(w: &mut Write, id: &TypeStruct, f: F) -> io::Result<()>
where F: FnOnce(&mut Write) -> io::Result<()> {
try!(writeln!(w, "{}{}{}", MOD_COMMENT, id, END_INFO));
f(w)
}
pub fn write_file_comment(comment: &str, id: &Option<TypeStruct>, ignore_macro: bool) -> String {
if let &Some(ref t) = id {
if ignore_macro {
format!("{} {}{}\n{}", FILE_COMMENT, t, END_INFO, comment)
} else {
format!("{} {:?}{}\n{}", FILE_COMMENT, t, END_INFO, comment)
}
} else {
format!("{}{}\n{}", FILE_COMMENT, END_INFO, comment)
}
}
pub fn write_file(file: &str) -> String {
format!("{}{}{}", FILE, file, END_INFO)
}
pub fn write_file_name(w: &mut Write, name: Option<&str>) -> io::Result<()> {
writeln!(w, "{}{}{}", FILE, name.unwrap_or("*"), END_INFO)
}