-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
127 lines (112 loc) · 3.08 KB
/
main.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
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
//! This is the nufmt binary documentation
//!
//! # Usage
//!
//! `nufmt` is inteded to be used as this:
//!
//! To format a single file
//! ```shell
//! nufmt file1.nu
//! ```
//!
//! `TODO!`
//!
//! Set options file
//! ```shell
//! nufmt <file> --config nufmt.nuon
//! ```
// throw error if are docs missing
// or finds a broken link doc
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(missing_docs)]
// for debug purposes, allow unused imports and variables
#[allow(unused)]
#[allow(unused_imports)]
#[allow(unused_import_braces)]
use anyhow::Result;
use clap::Parser;
use nufmt::config::Config;
use nufmt::{Input, Session};
use std::error::Error;
use std::io::{stdout, Write};
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about)]
struct Cli {
#[arg(help = "The file or files you want to format in nu")]
files: Vec<PathBuf>,
#[arg(short, long, help = "The configuration file")]
config: Option<PathBuf>,
}
fn main() -> Result<(), Box<dyn Error>> {
// set up logger
env_logger::init();
let cli = Cli::parse();
let exit_code = match execute(cli.files, Config::default()) {
Ok(code) => code,
Err(e) => {
eprintln!("{:#}", e);
1
}
};
// Make sure standard output is flushed before we exit.
std::io::stdout().flush().unwrap();
// Exit with given exit code.
//
// NOTE: this immediately terminates the process without doing any cleanup,
// so make sure to finish all necessary cleanup before this is called.
std::process::exit(exit_code);
}
/// Sends the files to format in lib.rs
fn execute(files: Vec<PathBuf>, options: Config) -> Result<i32> {
// open a session
let out = &mut stdout();
let mut session = Session::new(options, Some(out));
for file in files {
// TODO: this would be a great place to create an enum like
// enum
// enum File {
// stdin,
// single_file,
// folder,
// _mod,
// }
if !file.exists() {
eprintln!("Error: {} not found!", file.to_str().unwrap());
session.add_operational_error()
} else if file.is_dir() {
// TODO: recursive search
eprintln!(
"Error: {} is a directory. Please pass files only.",
file.to_str().unwrap()
);
session.add_operational_error()
} else {
// send the file to lib.rs
println!("formatting file: {:?}", file);
format_and_emit_report(&mut session, Input::File(file));
}
}
let exit_code = if session.has_operational_errors() {
1
} else {
0
};
Ok(exit_code)
}
fn format_and_emit_report<T: Write>(session: &mut Session<'_, T>, input: Input) {
session.format(input);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clap_cli_construction() {
use clap::CommandFactory;
Cli::command().debug_assert()
}
#[test]
fn todo() {
todo!("First fix the library fixes, then we can do the binary tests.")
}
}