diff options
author | Aleksey Kladov <[email protected]> | 2018-07-30 14:16:58 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-30 14:16:58 +0100 |
commit | 3b6a6f6673041cf9ee315c00f9b0e24e2c067091 (patch) | |
tree | 001e556601e9dd37556338f877759466846c7af0 /cli/src/main.rs | |
parent | d39198490f878a9ae395af1cf923fb7375de4548 (diff) |
Add render test functionality
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r-- | cli/src/main.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs new file mode 100644 index 000000000..94183c552 --- /dev/null +++ b/cli/src/main.rs | |||
@@ -0,0 +1,68 @@ | |||
1 | extern crate clap; | ||
2 | #[macro_use] | ||
3 | extern crate failure; | ||
4 | extern crate libsyntax2; | ||
5 | extern crate tools; | ||
6 | |||
7 | use std::{fs, path::Path, io::Read}; | ||
8 | use clap::{App, Arg, SubCommand}; | ||
9 | use tools::collect_tests; | ||
10 | |||
11 | type Result<T> = ::std::result::Result<T, failure::Error>; | ||
12 | |||
13 | fn main() -> Result<()> { | ||
14 | let matches = App::new("libsyntax2-cli") | ||
15 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | ||
16 | .subcommand( | ||
17 | SubCommand::with_name("render-test") | ||
18 | .arg(Arg::with_name("line").long("--line").required(true).takes_value(true)) | ||
19 | .arg(Arg::with_name("file").long("--file").required(true).takes_value(true)) | ||
20 | ) | ||
21 | .subcommand(SubCommand::with_name("parse")) | ||
22 | .get_matches(); | ||
23 | match matches.subcommand() { | ||
24 | ("parse", _) => { | ||
25 | let tree = parse()?; | ||
26 | println!("{}", tree); | ||
27 | }, | ||
28 | ("render-test", Some(matches)) => { | ||
29 | let file = matches.value_of("file").unwrap(); | ||
30 | let file = Path::new(file); | ||
31 | let line: usize = matches.value_of("line").unwrap().parse()?; | ||
32 | let line = line - 1; | ||
33 | let (test, tree) = render_test(file, line)?; | ||
34 | println!("{}\n{}", test, tree); | ||
35 | } | ||
36 | _ => unreachable!(), | ||
37 | } | ||
38 | Ok(()) | ||
39 | |||
40 | } | ||
41 | |||
42 | fn parse() -> Result<String> { | ||
43 | let text = read_stdin()?; | ||
44 | let file = libsyntax2::parse(text); | ||
45 | let tree = libsyntax2::utils::dump_tree(&file); | ||
46 | Ok(tree) | ||
47 | } | ||
48 | |||
49 | fn read_stdin() -> Result<String> { | ||
50 | let mut buff = String::new(); | ||
51 | ::std::io::stdin().read_to_string(&mut buff)?; | ||
52 | Ok(buff) | ||
53 | } | ||
54 | |||
55 | fn render_test(file: &Path, line: usize) -> Result<(String, String)> { | ||
56 | let text = fs::read_to_string(file)?; | ||
57 | let tests = collect_tests(&text); | ||
58 | let test = tests.into_iter().find(|t| { | ||
59 | t.start_line <= line && line <= t.start_line + t.text.lines().count() | ||
60 | }); | ||
61 | let test = match test { | ||
62 | None => bail!("No test found at line {} at {}", line, file.display()), | ||
63 | Some(test) => test, | ||
64 | }; | ||
65 | let file = libsyntax2::parse(test.text.clone()); | ||
66 | let tree = libsyntax2::utils::dump_tree(&file); | ||
67 | Ok((test.text, tree)) | ||
68 | } | ||