|
1 | | -use std::collections::HashMap; |
2 | | -use std::io::{self, Write}; |
3 | | -use std::path::PathBuf; |
4 | 1 | use std::process::ExitCode; |
5 | 2 |
|
6 | 3 | #[cfg(feature = "cli")] |
7 | | -use clap::builder::Styles; |
8 | | -use clap::builder::ValueParser; |
9 | | -#[cfg(feature = "cli")] |
10 | | -use clap::{Arg, Command, builder::ArgGroup, value_parser}; |
11 | | - |
12 | | -use notabene::span::Index; |
13 | | -#[cfg(feature = "cli")] |
14 | | -use notabene::{OutputFormat, Rule, parse_file, render}; |
15 | | - |
16 | | -fn parse_rule_code(code: &str) -> Result<String, String> { |
17 | | - let code = code.to_uppercase(); |
18 | | - if Rule::ALL.iter().any(|rule| rule.code() == code) { |
19 | | - Ok(code) |
20 | | - } else { |
21 | | - Err(format!( |
22 | | - "{}", |
23 | | - Rule::ALL |
24 | | - .iter() |
25 | | - .map(|rule| rule.code()) |
26 | | - .collect::<Vec<&str>>() |
27 | | - .join(", ") |
28 | | - )) |
29 | | - } |
30 | | -} |
| 4 | +use notabene::cli; |
31 | 5 |
|
32 | 6 | #[cfg(feature = "cli")] |
33 | 7 | fn main() -> std::process::ExitCode { |
34 | | - let mut rules_by_code = HashMap::new(); |
35 | | - for rule in Rule::ALL { |
36 | | - rules_by_code.insert(rule.code().to_string(), rule); |
37 | | - } |
38 | | - let matches = Command::new("nb") |
39 | | - .arg_required_else_help(true) |
40 | | - .subcommand_required(true) |
41 | | - .styles(Styles::plain()) |
42 | | - .subcommand( |
43 | | - Command::new("check") |
44 | | - .about("Check a changelog") |
45 | | - .arg(Arg::new("FILE").value_parser(value_parser!(PathBuf))) |
46 | | - .arg( |
47 | | - Arg::new("output_format") |
48 | | - .long("output-format") |
49 | | - .value_parser(["full", "json", "jsonl", "short"]) |
50 | | - .default_value("short"), |
51 | | - ), |
52 | | - ) |
53 | | - .subcommand( |
54 | | - Command::new("rule") |
55 | | - .about("Explain a rule") |
56 | | - .group(ArgGroup::new("rule").required(true).args(["RULE", "all"])) |
57 | | - .arg( |
58 | | - Arg::new("RULE") |
59 | | - .help("The rule code (e.g., E100)") |
60 | | - .value_parser(ValueParser::new(parse_rule_code)) |
61 | | - .conflicts_with("all"), |
62 | | - ) |
63 | | - .arg( |
64 | | - Arg::new("all") |
65 | | - .short('a') |
66 | | - .long("all") |
67 | | - .help("Explain all rules") |
68 | | - .action(clap::ArgAction::SetTrue), |
69 | | - ), |
70 | | - ) |
71 | | - .get_matches(); |
72 | | - match matches.subcommand() { |
73 | | - Some(("check", submatches)) => { |
74 | | - let path = submatches |
75 | | - .get_one::<PathBuf>("FILE") |
76 | | - .unwrap_or(&PathBuf::from("CHANGELOG.md")) |
77 | | - .clone(); |
78 | | - let format: &String = submatches.get_one("output_format").expect("default"); |
79 | | - let output_format = match format.as_str() { |
80 | | - "short" => OutputFormat::Short, |
81 | | - "json" => OutputFormat::Json, |
82 | | - "jsonl" => OutputFormat::JsonLines, |
83 | | - "full" => OutputFormat::Full, |
84 | | - _ => unreachable!(), |
85 | | - }; |
86 | | - let (_, diagnostics) = parse_file(&path).unwrap(); |
87 | | - if diagnostics.is_empty() { |
88 | | - ExitCode::SUCCESS |
89 | | - } else { |
90 | | - // TODO: How to avoid reading again to create index? |
91 | | - let Ok(content) = std::fs::read_to_string(&path) else { |
92 | | - todo!(); |
93 | | - }; |
94 | | - let index = Index::new(&content); |
95 | | - let mut output = io::stdout(); |
96 | | - render( |
97 | | - &mut output, |
98 | | - diagnostics.as_slice(), |
99 | | - &content, |
100 | | - Some(&path), |
101 | | - &index, |
102 | | - output_format, |
103 | | - ) |
104 | | - .unwrap(); |
105 | | - ExitCode::FAILURE |
106 | | - } |
107 | | - } |
108 | | - Some(("rule", submatches)) => { |
109 | | - let mut output = io::stdout(); |
110 | | - if submatches.get_flag("all") { |
111 | | - for (i, rule) in Rule::ALL.iter().enumerate() { |
112 | | - if i > 0 { |
113 | | - writeln!(output).unwrap(); |
114 | | - } |
115 | | - write!( |
116 | | - output, |
117 | | - "# {} |
118 | | -
|
119 | | -{} |
120 | | -", |
121 | | - rule.code(), |
122 | | - rule.doc() |
123 | | - ) |
124 | | - .unwrap(); |
125 | | - } |
126 | | - ExitCode::SUCCESS |
127 | | - } else if let Some(code) = submatches.get_one::<String>("RULE") { |
128 | | - let rule = rules_by_code.get(code).unwrap(); |
129 | | - write!( |
130 | | - output, |
131 | | - "# {} |
132 | | -
|
133 | | -{} |
134 | | -", |
135 | | - rule.code(), |
136 | | - rule.doc() |
137 | | - ) |
138 | | - .unwrap(); |
139 | | - ExitCode::SUCCESS |
140 | | - } else { |
141 | | - unreachable!() |
142 | | - } |
143 | | - } |
144 | | - _ => unreachable!(), |
| 8 | + match cli::main() { |
| 9 | + Ok(_) => ExitCode::SUCCESS, |
| 10 | + Err(_) => ExitCode::FAILURE, |
145 | 11 | } |
146 | 12 | } |
0 commit comments