Skip to content

Commit 95607a1

Browse files
feat: upgrade is now a cargo feature
1 parent eca33d4 commit 95607a1

File tree

7 files changed

+89
-29
lines changed

7 files changed

+89
-29
lines changed

Cargo.lock

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "grip-grab"
3-
version = "0.5.4"
3+
version = "0.5.5"
44
edition = "2021"
55
authors = ["Alexandre Pasmantier <alex.pasmant@gmail.com>"]
66
license = "Apache-2.0"
@@ -18,19 +18,28 @@ categories = [
1818

1919

2020
[dependencies]
21-
anyhow = "1.0.86"
2221
clap = { version = "4.5.9", features = ["derive"] }
2322
devicons = "0.6.7"
2423
grep = "0.3.1"
2524
ignore = "0.4.22"
2625
serde = { version = "1.0.204", features = ["derive"] }
2726
serde_json = "1.0.120"
2827
termcolor = "1.4.1"
28+
thiserror = "1.0.64"
2929

3030
[[bin]]
3131
name = "gg"
3232
path = "src/main.rs"
3333

34+
35+
[features]
36+
default = []
37+
upgrade = []
38+
39+
[profile.dev]
40+
opt-level = 0
41+
debug = true
42+
3443
[profile.release]
3544
opt-level = 3
3645
debug = "none"

src/cli.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::PathBuf;
22

33
use crate::{printer::PrintMode, utils};
44
use clap::{ArgAction, Parser, Subcommand};
5+
use thiserror::Error;
56

67
#[derive(Parser, Debug)]
78
#[command(name = "grip-grab")]
@@ -148,7 +149,11 @@ impl Default for PostProcessedCli {
148149
}
149150
}
150151

151-
pub fn process_cli_args(mut cli: Cli) -> anyhow::Result<PostProcessedCli> {
152+
#[derive(Error, Debug)]
153+
#[error("Error processing CLI arguments")]
154+
pub struct CliProcessingError {}
155+
156+
pub fn process_cli_args(mut cli: Cli) -> Result<PostProcessedCli, CliProcessingError> {
152157
cli.validate();
153158

154159
if cli.paths.is_empty() {

src/fs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use anyhow::Result;
2-
use ignore::{types::TypesBuilder, WalkBuilder};
1+
use ignore::{types::TypesBuilder, Error, WalkBuilder};
32

43
use std::path::{Path, PathBuf};
54

@@ -43,7 +42,7 @@ pub fn walk_builder(
4342
builder
4443
}
4544

46-
fn add_custom_filetypes(types_builder: &mut TypesBuilder) -> Result<()> {
45+
fn add_custom_filetypes(types_builder: &mut TypesBuilder) -> Result<(), Error> {
4746
Ok(types_builder.add("pystrict", "*.py")?)
4847
}
4948

src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use std::io::{stdin, Read};
1+
use std::io::{self, stdin, Read};
22
use std::path::PathBuf;
33
use std::sync::{mpsc, Arc};
44

55
use clap::Parser;
66

7-
use cli::Commands;
7+
use cli::{CliProcessingError, Commands};
88
use fs::is_readable_stdin;
9-
use grep::regex::RegexMatcher;
9+
use grep::regex::{self, RegexMatcher};
1010
use ignore::DirEntry;
1111
use printer::PrinterConfig;
1212
use search::{build_searcher, search_reader};
13+
use thiserror::Error;
1314
use upgrade::upgrade_gg;
1415

1516
use crate::cli::{process_cli_args, Cli};
@@ -24,7 +25,17 @@ mod search;
2425
mod upgrade;
2526
mod utils;
2627

27-
pub fn main() -> anyhow::Result<()> {
28+
#[derive(Error, Debug)]
29+
pub enum GGError {
30+
#[error("Erorr processing CLI arguments")]
31+
Cli(#[from] CliProcessingError),
32+
#[error(transparent)]
33+
Io(#[from] io::Error),
34+
#[error(transparent)]
35+
Regex(#[from] regex::Error),
36+
}
37+
38+
pub fn main() -> Result<(), GGError> {
2839
let cli_args = process_cli_args(Cli::parse())?;
2940

3041
if let Some(subcommand) = cli_args.sub_command {

src/search.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::fmt;
1+
use std::{fmt, io};
22
use std::{path::PathBuf, slice::Iter};
33

44
use grep::{
55
matcher::{Match, Matcher},
6-
regex::{RegexMatcher, RegexMatcherBuilder},
6+
regex::{self, RegexMatcher, RegexMatcherBuilder},
77
searcher::{sinks::UTF8, Searcher, SearcherBuilder},
88
};
99
use serde::Serialize;
@@ -185,7 +185,7 @@ pub fn search_file<'a>(
185185
path: PathBuf,
186186
matcher: &RegexMatcher,
187187
searcher: &mut Searcher,
188-
) -> anyhow::Result<FileResults> {
188+
) -> Result<FileResults, io::Error> {
189189
let mut partial_results: Vec<PartialSearchResult> = Vec::new();
190190

191191
searcher.search_path(
@@ -236,11 +236,13 @@ pub fn search_file<'a>(
236236
Ok(FileResults { path, results })
237237
}
238238

239+
// io::Error
240+
// std::fmt::Display
239241
pub fn search_reader(
240242
reader: impl std::io::BufRead,
241243
matcher: &RegexMatcher,
242244
searcher: &mut Searcher,
243-
) -> anyhow::Result<Vec<SearchResult>> {
245+
) -> Result<Vec<SearchResult>, io::Error> {
244246
let mut results = Vec::new();
245247
let mut line_number = 0;
246248
searcher.search_reader(
@@ -266,8 +268,9 @@ pub fn search_reader(
266268
Ok(results)
267269
}
268270

269-
pub fn build_matcher(patterns: &Vec<String>) -> anyhow::Result<RegexMatcher> {
271+
pub fn build_matcher(patterns: &Vec<String>) -> Result<RegexMatcher, regex::Error> {
270272
let builder = RegexMatcherBuilder::new();
273+
// matcher Error
271274
Ok(builder.build_many(patterns)?)
272275
}
273276

src/upgrade.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
use std::{
2-
io::{BufRead, BufReader},
3-
process::Command,
4-
thread::{self},
5-
};
6-
71
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
82

3+
#[cfg(not(feature = "upgrade"))]
4+
pub fn upgrade_gg(_force: bool) {
5+
let mut colored_stdout = StandardStream::stdout(ColorChoice::Always);
6+
colored_stdout
7+
.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_italic(true))
8+
.expect("Failed to set color");
9+
10+
println!("\n┌────────────────────────────────────────────┐");
11+
println!("│ Upgrade feature is not enabled. │");
12+
println!("│ Please recompile with `upgrade` feature │");
13+
println!("│ enabled to use this feature: │");
14+
println!("│ │");
15+
println!("│ cargo install grip-grab --features=upgrade │");
16+
println!("└────────────────────────────────────────────┘\n");
17+
18+
colored_stdout.reset().expect("Failed to reset color");
19+
}
20+
21+
#[cfg(feature = "upgrade")]
922
pub fn upgrade_gg(force: bool) {
23+
use std::{
24+
io::{BufRead, BufReader},
25+
process::Command,
26+
thread::{self},
27+
};
28+
1029
let mut colored_stdout = StandardStream::stdout(ColorChoice::Always);
1130
colored_stdout
1231
.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_italic(true))

0 commit comments

Comments
 (0)