feat: write match data to file
Diff
Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++
Cargo.toml | 2 ++
src/core/mod.rs | 6 ++++--
src/main.rs | 35 ++++++++++++++++++++++++++++++-----
4 files changed, 81 insertions(+), 7 deletions(-)
@@ -168,6 +168,12 @@ dependencies = [
]
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "jobserver"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -277,12 +283,51 @@ dependencies = [
]
[[package]]
name = "ryu"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "serde"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.111"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "smolguess"
version = "0.1.0"
dependencies = [
"clap",
"git2",
"itertools",
"serde",
"serde_json",
"stopwatch",
]
@@ -9,4 +9,6 @@ edition = "2021"
clap = { version = "4.4.16", features = ["derive"] }
git2 = "0.18.1"
itertools = "0.12.0"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
stopwatch = { git = "https://git.holly.sh/stopwatch.git", version = "0.1.0" }
@@ -1,5 +1,7 @@
use std::path::PathBuf;
use serde::Serialize;
@@ -298,7 +300,7 @@ impl LanguageDefinitions {
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct LanguageDefinition {
pub name: String,
pub extension: String,
@@ -389,7 +391,7 @@ where
#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct LanguageMatch {
pub file: PathBuf,
pub language: LanguageDefinition,
@@ -1,4 +1,10 @@
use std::{collections::HashMap, path::PathBuf, vec};
use std::{
collections::HashMap,
fs::File,
io::{self, Write},
path::PathBuf,
vec,
};
use clap::Parser;
use itertools::Itertools;
@@ -22,7 +28,7 @@ struct CliArgs {
pub verbose: bool,
}
fn main() {
fn main() -> io::Result<()> {
let args: CliArgs = CliArgs::parse();
let mut stopwatch = stopwatch::Stopwatch::new();
@@ -50,16 +56,35 @@ fn main() {
stopwatch.stop();
let json: String = serde_json::to_string_pretty(&sorted_groups)?;
if args.verbose {
sorted_groups
.iter()
.for_each(|item| println!("{}: {}", item.0, item.1.len()));
println!("{}", json);
println!("----------------")
}
if args.export.is_some() {
let file = PathBuf::from(args.export.unwrap());
if !file.is_dir() {
File::create(file)?.write_all(json.as_bytes())?;
} else {
println!("This is a directory. Aborting file write.");
}
}
if !sorted_groups.is_empty() {
let default: (String, Vec<LanguageMatch>) = (String::from("Unknown"), vec![]);
println!("{}", sorted_groups.first().unwrap_or_else(|| &default).0);
}
if args.verbose {
println!("Operation completed in: {}", stopwatch)
}
Ok(())
}