aboutsummaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-30 14:16:58 +0100
committerAleksey Kladov <[email protected]>2018-07-30 14:16:58 +0100
commit3b6a6f6673041cf9ee315c00f9b0e24e2c067091 (patch)
tree001e556601e9dd37556338f877759466846c7af0 /cli
parentd39198490f878a9ae395af1cf923fb7375de4548 (diff)
Add render test functionality
Diffstat (limited to 'cli')
-rw-r--r--cli/Cargo.toml11
-rw-r--r--cli/src/main.rs68
2 files changed, 79 insertions, 0 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
new file mode 100644
index 000000000..a259eef63
--- /dev/null
+++ b/cli/Cargo.toml
@@ -0,0 +1,11 @@
1[package]
2name = "cli"
3version = "0.1.0"
4authors = ["Aleksey Kladov <[email protected]>"]
5publish = false
6
7[dependencies]
8clap = "2.32.0"
9failure = "0.1.1"
10libsyntax2 = { path = "../" }
11tools = { path = "../tools" }
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 @@
1extern crate clap;
2#[macro_use]
3extern crate failure;
4extern crate libsyntax2;
5extern crate tools;
6
7use std::{fs, path::Path, io::Read};
8use clap::{App, Arg, SubCommand};
9use tools::collect_tests;
10
11type Result<T> = ::std::result::Result<T, failure::Error>;
12
13fn 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
42fn 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
49fn read_stdin() -> Result<String> {
50 let mut buff = String::new();
51 ::std::io::stdin().read_to_string(&mut buff)?;
52 Ok(buff)
53}
54
55fn 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}