summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWesley Moore <wes@wezm.net>2019-07-08 11:30:32 +1000
committerWesley Moore <wes@wezm.net>2019-07-08 11:30:32 +1000
commit44888630b7ee1dbefa9964ca750b1622b3dc5fb0 (patch)
treedbae5946a1990eea3db9957763c172904410e267
parent21855fb6e2e5f5a263dd47db0992350e4806f4cf (diff)
Shuffle things around to use the lib from the binary
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/annotate.rs6
-rw-r--r--src/bin/annotaterust.rs49
-rw-r--r--src/git.rs10
-rw-r--r--src/lib.rs4
-rw-r--r--src/main.rs25
7 files changed, 63 insertions, 35 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6d2b720..74352a4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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)",
diff --git a/Cargo.toml b/Cargo.toml
index a12ed6d..e229d66 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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");
+}
diff --git a/src/git.rs b/src/git.rs
index 293f225..0f59c69 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -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();
diff --git a/src/lib.rs b/src/lib.rs
index 4ec8e11..540cb3f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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);
-}