-
Notifications
You must be signed in to change notification settings - Fork 216
/
format.rs
36 lines (30 loc) 路 1021 Bytes
/
format.rs
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
use datamodel::ast::reformat::Reformatter;
use std::{
fs::{self, File},
io::{self, BufWriter, Read},
};
use crate::FormatOpts;
pub fn run(opts: FormatOpts) {
let datamodel_string = match opts.input {
Some(file_name) => {
fs::read_to_string(&file_name).expect(&format!("Unable to open file {}", file_name.display()))
}
None => {
let mut buf = String::new();
io::stdin()
.read_to_string(&mut buf)
.expect("Unable to read from stdin.");
buf
}
};
match opts.output {
Some(file_name) => {
let file = File::open(&file_name).expect(&format!("Unable to open file {}", file_name.display()));
let mut stream = BufWriter::new(file);
Reformatter::reformat_to(&datamodel_string, &mut stream, opts.tabwidth);
}
None => {
Reformatter::reformat_to(&datamodel_string, &mut io::stdout().lock(), opts.tabwidth);
}
}
}