~wezm/annotate-rust

44888630b7ee1dbefa9964ca750b1622b3dc5fb0 — Wesley Moore 1 year, 7 days ago 21855fb
Shuffle things around to use the lib from the binary
6 files changed, 39 insertions(+), 11 deletions(-)

M Cargo.lock
M Cargo.toml
M src/annotate.rs
R src/{main.rs => bin/annotaterust.rs}
M src/git.rs
M src/lib.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 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)",

M Cargo.toml => Cargo.toml +1 -1
@@ 1,5 1,5 @@
[package]
name = "annotaterust"
name = "annotate-rust"
version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
edition = "2018"

M src/annotate.rs => src/annotate.rs +5 -1
@@ 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,

R src/main.rs => src/bin/annotaterust.rs +25 -1
@@ 1,7 1,10 @@
use std::env;
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();


@@ 22,4 25,25 @@ fn main() {

    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");
}

M src/git.rs => src/git.rs +5 -5
@@ 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();

M src/lib.rs => src/lib.rs +2 -2
@@ 1,2 1,2 @@
mod annotate;
mod git;
pub mod annotate;
pub mod git;