diff options
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/annotate.rs | 6 | ||||
-rw-r--r-- | src/bin/annotaterust.rs | 49 | ||||
-rw-r--r-- | src/git.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 25 |
7 files changed, 63 insertions, 35 deletions
@@ -9,7 +9,7 @@ dependencies = [ ] [[package]] -name = "annotaterust" +name = "annotate-rust" version = "0.1.0" dependencies = [ "regex 1.1.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1,5 +1,5 @@ [package] -name = "annotaterust" +name = "annotate-rust" version = "0.1.0" authors = ["Wesley Moore <wes@wezm.net>"] edition = "2018" diff --git a/src/annotate.rs b/src/annotate.rs index 4ba97bc..a11d9b7 100644 --- a/src/annotate.rs +++ b/src/annotate.rs @@ -2,15 +2,19 @@ use serde::Serialize; #[derive(Serialize)] #[serde(tag = "type")] -enum Annotation { +pub enum Annotation { + #[serde(rename = "link")] Link { lineno: u32, colno: u32, len: u32, to: String, + #[serde(skip_serializing_if = "Option::is_none")] title: Option<String>, + #[serde(skip_serializing_if = "Option::is_none")] color: Option<String>, }, + #[serde(rename = "markdown")] Markdown { lineno: u32, title: String, diff --git a/src/bin/annotaterust.rs b/src/bin/annotaterust.rs new file mode 100644 index 0000000..46154fe --- /dev/null +++ b/src/bin/annotaterust.rs @@ -0,0 +1,49 @@ +use std::{env, io}; +use std::fs::File; +use std::io::Read; +use std::process; +use std::collections::HashMap; + +use annotate_rust::{git, annotate::Annotation}; + +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); + + let output = git::crawl_git_tree(".").expect("git"); + let blobs = git::parse_ls_tree_output(&output).expect("parse"); + + let mut annotations = HashMap::new(); + annotations.insert(blobs[0].object, Annotation::Markdown { + lineno: 1, + title: "Title".to_string(), + content: "content".to_string() + }); + annotations.insert(blobs[1].object, Annotation::Link { + lineno: 1, + colno: 0, + len: 5, + to: "asdf".to_string(), + title: None, + color: None + }); + + let stdout = io::stdout(); + serde_json::to_writer_pretty(stdout, &annotations).expect("json"); +} @@ -7,12 +7,12 @@ use std::str::Utf8Error; use regex::Regex; #[derive(Debug, PartialEq)] -struct Blob<'a> { - object: &'a str, - path: &'a Path, +pub struct Blob<'a> { + pub object: &'a str, + pub path: &'a Path, } -fn crawl_git_tree<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { +pub fn crawl_git_tree<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { let output = Command::new("git") .current_dir(path) .args(&["ls-tree", "-zr", "HEAD"]) @@ -28,7 +28,7 @@ fn crawl_git_tree<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { Ok(output.stdout) } -fn parse_ls_tree_output<'a>(output: &'a [u8]) -> Result<Vec<Blob<'a>>, Utf8Error> { +pub fn parse_ls_tree_output<'a>(output: &'a [u8]) -> Result<Vec<Blob<'a>>, Utf8Error> { let re = Regex::new(r"^[^ ]+ [^ ]+ ([^\t]+)\t(.+)$").unwrap(); let mut blobs = Vec::new(); @@ -1,2 +1,2 @@ -mod annotate; -mod git; +pub mod annotate; +pub mod git; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 5ec8f34..0000000 --- a/src/main.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::env; -use std::fs::File; -use std::io::Read; -use std::process; - -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); -} |