Skip to content

Commit

Permalink
Make Builder take a FileSource (#194)
Browse files Browse the repository at this point in the history
Closes #149
  • Loading branch information
spenserblack authored Sep 26, 2023
1 parent e84c746 commit 2b1bf09
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
13 changes: 9 additions & 4 deletions gengo-bin/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Error as ClapError;
use clap::Parser;
use gengo::{analysis::SummaryOpts, Analysis, Builder};
use gengo::{analysis::SummaryOpts, Analysis, Builder, Git};
use indexmap::IndexMap;
use std::io::{self, Write};

Expand Down Expand Up @@ -43,9 +43,14 @@ pub struct CLI {

impl CLI {
pub fn run<Out: Write, Err: Write>(&self, mut out: Out, mut err: Err) -> Result<(), io::Error> {
let gengo = Builder::new(&self.repository, &self.revision)
.read_limit(self.read_limit)
.build();
let git = match Git::new(&self.repository, &self.revision) {
Ok(git) => git,
Err(e) => {
writeln!(err, "failed to create git instance: {}", e)?;
return Ok(());
}
};
let gengo = Builder::new(git).read_limit(self.read_limit).build();
let gengo = match gengo {
Ok(gengo) => gengo,
Err(e) => {
Expand Down
11 changes: 5 additions & 6 deletions gengo/benches/run_on_self.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use criterion::{criterion_group, criterion_main, Criterion};
use gengo::Builder;
use gengo::{Builder, Git};

fn head_benchmark(c: &mut Criterion) {
fn git_benchmark(c: &mut Criterion) {
let revs = {
let mut revs: Vec<_> = (0..3).map(|n| format!("HEAD{}", "^".repeat(n))).collect();
revs.extend_from_slice(&["test/javascript".into()]);
revs
};
for rev in revs {
let gengo = Builder::new(env!("CARGO_MANIFEST_DIR"), &rev)
.build()
.unwrap();
let git = Git::new(env!("CARGO_MANIFEST_DIR"), &rev).unwrap();
let gengo = Builder::new(git).build().unwrap();
c.bench_function(&format!("run on {}", rev), |b| {
b.iter(|| gengo.analyze().unwrap())
});
}
}

criterion_group!(benches, head_benchmark);
criterion_group!(benches, git_benchmark);
criterion_main!(benches);
26 changes: 8 additions & 18 deletions gengo/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,22 @@ use super::vendored::Vendored;
use super::Analyzers;
use super::Gengo;

use crate::file_source::Git;
use crate::file_source::FileSource;
use std::error::Error as ErrorTrait;
use std::path::Path;

/// Builds a new `Gengo` instance.
///
/// # Example
///
/// ```no_run
/// use gengo::Builder;
/// let gengo = Builder::new("path/to/repo", "HEAD").build().unwrap();
/// ```
pub struct Builder<P: AsRef<Path>> {
repository_path: P,
rev: String,
pub struct Builder<FS: for<'fs> FileSource<'fs>> {
file_source: FS,
analyzers: Option<Analyzers>,
read_limit: Option<usize>,
}

impl<P: AsRef<Path>> Builder<P> {
impl<FS: for<'fs> FileSource<'fs>> Builder<FS> {
pub const DEFAULT_READ_LIMIT: usize = 1 << 20;

pub fn new(repository_path: P, rev: &str) -> Self {
pub fn new(file_source: FS) -> Self {
Self {
repository_path,
rev: rev.to_owned(),
file_source,
analyzers: None,
read_limit: None,
}
Expand All @@ -50,8 +40,8 @@ impl<P: AsRef<Path>> Builder<P> {
self
}

pub fn build(self) -> Result<Gengo<Git>, Box<dyn ErrorTrait>> {
let file_source = Git::new(self.repository_path, &self.rev)?;
pub fn build(self) -> Result<Gengo<FS>, Box<dyn ErrorTrait>> {
let file_source = self.file_source;
let analyzers = self.analyzers.unwrap_or_default();
let read_limit = self.read_limit.unwrap_or(Self::DEFAULT_READ_LIMIT);
let documentation = Documentation::new();
Expand Down
17 changes: 14 additions & 3 deletions gengo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
//! Gengo is a language detection library for Git repositories.
//! Gengo is a language detection library for collections of files.
//! Currently, it supports reading from git repositories.
//!
//! # Example
//!
//! ```no_run
//! use gengo::{Builder, Git};
//! let git = Git::new("path/to/repo", "HEAD").unwrap();
//! let gengo = Builder::new(git).build().unwrap();
//! let results = gengo.analyze().unwrap();
//! ```
//! # Built-in Languages
//!
//! While it is possible to provide your own definitions for
//! language detection, Gengo comes with a set of built-in
//! definitions.
//!
//! # Built-in Languages
#![doc = include_str!(concat!(env!("OUT_DIR"), "/language-list.md"))]

pub use analysis::Analysis;
Expand All @@ -13,7 +24,7 @@ use documentation::Documentation;
pub use error::{Error, ErrorKind};
use generated::Generated;

pub use file_source::FileSource;
pub use file_source::{FileSource, Git};
use glob::MatchOptions;
pub use languages::analyzer::Analyzers;
use languages::Category;
Expand Down
9 changes: 3 additions & 6 deletions gengo/tests/gengo_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gengo::Analyzers;
use gengo::Builder;
use gengo::{Analyzers, Builder, Git};
const ROOT: &str = env!("CARGO_MANIFEST_DIR");

mod util;
Expand All @@ -10,10 +9,8 @@ fn test_javascript() {
// guarantee order. Improve this test.
let analyzers = fixture_str!("test_javascript-analyzers.yaml");
let analyzers = Analyzers::from_yaml(analyzers).unwrap();
let gengo = Builder::new(ROOT, "test/javascript")
.analyzers(analyzers)
.build()
.unwrap();
let git = Git::new(ROOT, "test/javascript").unwrap();
let gengo = Builder::new(git).analyzers(analyzers).build().unwrap();
let results = gengo.analyze().unwrap();
insta::assert_debug_snapshot!(results);
}

0 comments on commit 2b1bf09

Please sign in to comment.