From 6332527ab81f93bb6b0c2db7b68214035d639f0e Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sun, 14 Jan 2024 22:20:57 +0100 Subject: [PATCH] feat: write match data to file --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/core/mod.rs | 6 ++++-- src/main.rs | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99567cc..1566f90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/Cargo.toml b/Cargo.toml index d48d7aa..9b490fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/core/mod.rs b/src/core/mod.rs index 9bdde25..00558cb 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,5 +1,7 @@ use std::path::PathBuf; +use serde::Serialize; + /// A collection of programming language definitions. /// /// `LanguageDefinitions` is used to manage a list of programming language definitions, which consist @@ -298,7 +300,7 @@ impl LanguageDefinitions { /// /// assert_eq!((definition.name.as_str(), definition.extension.as_str()), ("Rust", "rs")); /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize)] pub struct LanguageDefinition { pub name: String, pub extension: String, @@ -389,7 +391,7 @@ where /// assert_eq!(language_match.language.name, "Rust"); /// assert_eq!(language_match.language.extension, "rs"); /// ``` -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct LanguageMatch { pub file: PathBuf, pub language: LanguageDefinition, diff --git a/src/main.rs b/src/main.rs index 0255999..992fe4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); + // Print out the results + 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!("----------------") } + // Write match data to file + 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."); + } + } + + // Output the identified language if !sorted_groups.is_empty() { let default: (String, Vec) = (String::from("Unknown"), vec![]); println!("{}", sorted_groups.first().unwrap_or_else(|| &default).0); } + + if args.verbose { + println!("Operation completed in: {}", stopwatch) + } + + Ok(()) } -- libgit2 1.7.2