summaryrefslogtreecommitdiffstats
path: root/src/bin/annotaterust.rs
blob: 3ecc0235116717be35af22139296acc730bb145f (plain)
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
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::process;
use std::{env, io};

use syn::visit::Visit;

use annotate_rust::annotate::{Annotation, FnVisitor};
use annotate_rust::git;

fn main() {
    let mut args = env::args();
    let _ = args.next(); // executable name

    let filename = match (args.next(), args.next()) {
        (Some(filename), None) => filename,
        _ => {
            eprintln!("Usage: dump-syntax path/to/filename.rs");
            process::exit(1);
        }
    };

    let mut file = File::open(&filename).expect("Unable to open file");

    let mut src = String::new();
    file.read_to_string(&mut src).expect("Unable to read file");

    let syntax = syn::parse_file(&src).expect("Unable to parse file");
    println!("{:#?}", syntax);

    // Visit function invocations
    let mut visitor = FnVisitor;
    visitor.visit_file(&syntax);

    let output = git::crawl_git_tree(".").expect("git");
    let blobs = git::parse_ls_tree_output(&output).expect("parse");

    let mut annotations = HashMap::new();
    annotations
        .entry(blobs[0].object)
        .or_insert(Vec::new())
        .push(Annotation::Markdown {
            lineno: 1,
            title: "Title".to_string(),
            content: "content".to_string(),
        });
    annotations
        .entry(blobs[0].object)
        .or_insert(Vec::new())
        .push(Annotation::Link {
            lineno: 1,
            colno: 0,
            len: 5,
            to: "asdf".to_string(),
            title: None,
            color: None,
        });

    let stdout = io::stdout();
    serde_json::to_writer(stdout, &annotations).expect("json");
}