68 | 68 |
.version("0.0")
|
69 | 69 |
.author("Getty Ritter <rrecutils@infinitenegativeutility.com>")
|
70 | 70 |
.about("Display the Rust AST for a Recutils file")
|
| 71 |
|
71 | 72 |
.arg(clap::Arg::with_name("input")
|
72 | 73 |
.short("i")
|
73 | 74 |
.long("input")
|
74 | 75 |
.value_name("FILE")
|
75 | 76 |
.help("The input recfile (or - for stdin)"))
|
| 77 |
|
76 | 78 |
.arg(clap::Arg::with_name("output")
|
77 | 79 |
.short("o")
|
78 | 80 |
.long("output")
|
79 | 81 |
.value_name("FILE")
|
80 | 82 |
.help("The desired output location (or - for stdout)"))
|
81 | |
.arg(clap::Arg::with_name("template")
|
| 83 |
|
| 84 |
.arg(clap::Arg::with_name("mustache")
|
| 85 |
.short("m")
|
| 86 |
.long("mustache")
|
| 87 |
.value_name("FILE")
|
| 88 |
.help("The mustache template to use"))
|
| 89 |
|
| 90 |
.arg(clap::Arg::with_name("type")
|
82 | 91 |
.short("t")
|
83 | |
.long("template")
|
84 | |
.value_name("FILE")
|
85 | |
.help("The template to use"))
|
| 92 |
.long("type")
|
| 93 |
.value_name("TYPE")
|
| 94 |
.takes_value(true)
|
| 95 |
.help("The type of records to pass to the mustache file"))
|
| 96 |
|
86 | 97 |
.arg(clap::Arg::with_name("joiner")
|
87 | 98 |
.short("j")
|
88 | 99 |
.long("joiner")
|
89 | 100 |
.value_name("STRING")
|
90 | 101 |
.help("The string used to separate each fragment"))
|
| 102 |
|
91 | 103 |
.get_matches();
|
92 | 104 |
|
93 | 105 |
let stdin = io::stdin();
|
|
99 | 111 |
Box::new(io::BufReader::new(fs::File::open(path)?)),
|
100 | 112 |
};
|
101 | 113 |
|
102 | |
let template: String = match matches.value_of("template") {
|
| 114 |
let template: String = match matches.value_of("mustache") {
|
103 | 115 |
Some(path) => {
|
104 | 116 |
use io::Read;
|
105 | 117 |
let mut buf = Vec::new();
|
|
109 | 121 |
None => Err(format!("No template specified!"))?,
|
110 | 122 |
};
|
111 | 123 |
|
112 | |
let recfile = rrecutils::Recfile::parse(input)?;
|
| 124 |
let mut recfile = rrecutils::Recfile::parse(input)?;
|
| 125 |
|
| 126 |
if let Some(typ) = matches.value_of("type") {
|
| 127 |
recfile.filter_by_type(typ);
|
| 128 |
}
|
113 | 129 |
|
114 | 130 |
let mut output: Box<io::Write> =
|
115 | 131 |
match matches.value_of("output").unwrap_or("-") {
|
|
117 | 133 |
path => Box::new(fs::File::open(path)?),
|
118 | 134 |
};
|
119 | 135 |
|
| 136 |
let joiner = matches.value_of("joiner");
|
| 137 |
|
| 138 |
let mut first = true;
|
120 | 139 |
for r in recfile.records.into_iter() {
|
| 140 |
if first {
|
| 141 |
first = false;
|
| 142 |
} else if let Some(j) = joiner {
|
| 143 |
output.write(j.as_bytes())?;
|
| 144 |
output.write(&['\n' as u8])?;
|
| 145 |
}
|
121 | 146 |
R { rec: r }.render(&template, &mut output.as_mut())?;
|
122 | 147 |
}
|
123 | 148 |
|
|
125 | 150 |
}
|
126 | 151 |
|
127 | 152 |
fn main() {
|
| 153 |
use FormatErr::*;
|
128 | 154 |
match run() {
|
129 | 155 |
Ok(()) => (),
|
130 | |
Err(err) => panic!(err),
|
| 156 |
Err(IOError(_)) => panic!("IO Error"),
|
| 157 |
Err(Utf8Error(_)) => panic!("Cannot decode as UTF-8"),
|
| 158 |
Err(Rustache(r)) => panic!("Rustache error: {:?}", r),
|
| 159 |
Err(Generic(s)) => panic!("{}", s),
|
131 | 160 |
}
|
132 | 161 |
}
|