Skip to content

Commit 0566038

Browse files
committed
New filter changes now (should work) in TUI.
1 parent fb42483 commit 0566038

File tree

6 files changed

+41
-17
lines changed

6 files changed

+41
-17
lines changed

cargo-function-history/src/app/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl App<'_> {
363363
}
364364
filter_name => {
365365
if let Some(filters) =
366-
function_grep::filter::Filters::default().get_filter(*filter_name)
366+
function_grep::filter::Filters::default().get_filter(filter_name)
367367
{
368368
let rest = command_iter.copied().collect::<Vec<_>>().join(" ");
369369
let filt = match filters.to_filter(&rest) {

function-grep/src/filter.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use general_filters::{FunctionInImpl, FunctionInLines, FunctionWithParameterRust};
22
use std::{
33
collections::{hash_map, HashMap},
4-
fmt,
4+
fmt::{self, Display},
55
hash::Hash,
66
};
77

@@ -185,13 +185,14 @@ impl<Supports> InstantiatedFilter<Supports> {
185185
}
186186
}
187187

188+
#[derive(Debug, PartialEq, Eq)]
188189
pub struct All;
189190
impl From<All> for SupportedLanguages {
190191
fn from(_: All) -> Self {
191192
Self::All
192193
}
193-
// add code here
194194
}
195+
#[derive(Debug, PartialEq, Eq)]
195196
pub struct Language(String);
196197
impl From<Language> for SupportedLanguages {
197198
fn from(value: Language) -> Self {
@@ -205,12 +206,13 @@ macro_rules! default_filters {
205206
HashMap::from([$(($filter.filter_info().filter_name().to_string(), &$filter as &'static dyn Filter)),*])
206207
};
207208
}
209+
#[derive(Debug, PartialEq, Eq)]
208210
pub struct Many<T: Info<Supported = Language>> {
209211
pub name: String,
210212
pub filters: HashMap<String, T>,
211213
}
212214

213-
trait Info {
215+
pub trait Info {
214216
type Supported;
215217
fn filter_name(&self) -> String;
216218
}
@@ -220,6 +222,7 @@ pub type FilterType<'a> =
220222
SingleOrMany<&'a dyn Filter<Supports = All>, &'a dyn Filter<Supports = Language>>;
221223
pub type InstantiatedFilterType =
222224
SingleOrMany<InstantiatedFilter<All>, InstantiatedFilter<Language>>;
225+
#[derive(Debug, PartialEq, Eq)]
223226
pub enum SingleOrMany<A: Info<Supported = All>, M: Info<Supported = Language>> {
224227
All(A),
225228
Many(Many<M>),
@@ -228,21 +231,43 @@ impl<A: Info<Supported = All>, M: Info<Supported = Language>> SingleOrMany<A, M>
228231
#[must_use]
229232
pub fn filter_name(&self) -> String {
230233
match self {
231-
SingleOrMany::All(f) => f.filter_name(),
232-
SingleOrMany::Many(many) => many.name.clone(),
234+
Self::All(f) => f.filter_name(),
235+
Self::Many(many) => many.name.clone(),
233236
}
234237
}
235238

236239
#[must_use]
237240
pub fn supports(&self) -> SupportedLanguages {
238241
match self {
239-
SingleOrMany::All(_) => SupportedLanguages::All,
240-
SingleOrMany::Many(many) => {
242+
Self::All(_) => SupportedLanguages::All,
243+
Self::Many(many) => {
241244
SupportedLanguages::Many(many.filters.keys().cloned().collect())
242245
}
243246
}
244247
}
245248
}
249+
impl<A: Info<Supported = All>, M: Info<Supported = Language>> Display for SingleOrMany<A, M> {
250+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251+
write!(f, "{}", self.filter_name())
252+
}
253+
}
254+
impl<'a> FilterType<'a> {
255+
pub fn to_filter(&self, s: &str) -> Result<InstantiatedFilterType, String> {
256+
match self {
257+
SingleOrMany::All(a) => a.to_filter(s).map(SingleOrMany::All),
258+
SingleOrMany::Many(Many { name, filters }) => filters
259+
.iter()
260+
.map(|(name, f)| f.to_filter(s).map(|f| (name.clone(), f)))
261+
.collect::<Result<HashMap<_, _>, _>>()
262+
.map(|filters| {
263+
SingleOrMany::Many(Many {
264+
name: name.to_string(),
265+
filters,
266+
})
267+
}),
268+
}
269+
}
270+
}
246271
pub struct Filters<'a> {
247272
filters: HashMap<String, FilterType<'a>>,
248273
}
@@ -256,7 +281,7 @@ where
256281
self.filter_info().filter_name().to_string()
257282
}
258283
}
259-
impl<T> Info for InstantiatedFilter< T>
284+
impl<T> Info for InstantiatedFilter<T>
260285
where
261286
SupportedLanguages: From<T>,
262287
{

function_history_backend_thread/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use function_grep::filter::InstantiatedFilter;
3+
use function_grep::filter::InstantiatedFilterType;
44
use git_function_history::{FileFilterType, Filter, FunctionHistory};
55

66
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@@ -145,7 +145,7 @@ pub enum HistoryFilterType {
145145
FileAbsolute(String),
146146
FileRelative(String),
147147
Directory(String),
148-
PL(InstantiatedFilter),
148+
PL(InstantiatedFilterType),
149149
None,
150150
}
151151

git-function-history-gui/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use function_grep::filter::SingleOrMany;
1+
use function_grep::filter::FilterType;
22
use std::{collections::HashMap, fmt};
33

44
pub enum HistoryFilterType {
@@ -8,7 +8,7 @@ pub enum HistoryFilterType {
88
FileAbsolute(String),
99
FileRelative(String),
1010
Directory(String),
11-
PL(HashMap<String, String>, SingleOrMany<'static>),
11+
PL(HashMap<String, String>, FilterType<'static>),
1212
None,
1313
}
1414

git-function-history-lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub enum Filter {
9191
Message(String),
9292
/// when you want to filter by proggramming language filter
9393
#[enumstuff(skip)]
94-
PLFilter(function_grep::filter::InstantiatedFilter),
94+
PLFilter(function_grep::filter::InstantiatedFilterType),
9595
/// when you want to filter to only have files that are in a specific language
9696
Language(String),
9797
/// When you want to filter by nothing.

git-function-history-lib/src/types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub struct FunctionHistory {
219219

220220
impl FunctionHistory {
221221
// creates a new `FunctionHistory` from a list of commits
222-
pub fn new(name: String, commit_history: Vec<Commit>) -> Self {
222+
pub const fn new(name: String, commit_history: Vec<Commit>) -> Self {
223223
Self {
224224
name,
225225
commit_history,
@@ -451,9 +451,8 @@ impl Iterator for FunctionHistory {
451451
self.commit_history
452452
.get(self.current_iter_pos)
453453
.cloned()
454-
.map(|c| {
454+
.inspect(|c| {
455455
self.current_iter_pos += 1;
456-
c
457456
})
458457
}
459458
}

0 commit comments

Comments
 (0)