Skip to content

Commit a9a6c18

Browse files
committed
[function-grep] fixed and made better last filter that was made, I put it with wrong language and still used some copy paste behaviour
1 parent 5023886 commit a9a6c18

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

function-grep/src/filter/filter_parsers.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@ pub fn number<'a>(
22
substring: &mut impl Iterator<Item = &'a str>,
33
format: &str,
44
position: &str,
5+
filter: &str,
56
) -> Result<usize, String> {
6-
substring.next().ok_or_else(||format! ("invalid options for function_in_lines filter\nexpected {format}\n missing {position} [number]"))
7-
.and_then(|end| end.parse().map_err(|_| format! ("invalid options for function_in_lines filter\nexpected {format}\n cannot parse {position} [number]")))
7+
substring.next().ok_or_else(||format! ("invalid options for {filter} filter\nexpected {format}\n missing {position} [number]"))
8+
.and_then(|end| end.parse().map_err(|_| format! ("invalid options for {filter} filter\nexpected {format}\n cannot parse {position} [number]")))
89
}
910
pub fn extra<'a>(
1011
substring: &mut impl Iterator<Item = &'a str>,
1112
format: &str,
13+
filter: &str,
1214
) -> Result<(), String> {
13-
substring.next().map_or(Ok(()), |extra| Err(format!( "invalid options for function_in_lines filter\nexpected {format}\n, found extra stuff after {format}: {extra}")))
15+
substring.next().map_or(Ok(()), |extra| Err(format!( "invalid options for {filter} filter\nexpected {format}\n, found extra stuff after {format}: {extra}")))
1416
}
1517
pub fn label<'a>(
1618
substring: &mut impl Iterator<Item = &'a str>,
1719
format: &str,
1820
label: &str,
21+
filter: &str,
1922
) -> Result<(), String> {
20-
substring.next().ok_or_else(||format! ("invalid options for function_in_lines filter\nexpected {format}\n missing label {label}"))
23+
substring.next().ok_or_else(||format! ("invalid options for {filter} filter\nexpected {format}\n missing label {label}"))
2124
.and_then(|l| {
2225
if label == l {
2326
Ok(())
2427
} else {
25-
Err(format!("invalid options for function_in_lines filter\n expected {format}\nexpected {label}, found {l}"))
28+
Err(format!("invalid options for {filter} filter\n expected {format}\nexpected {label}, found {l}"))
2629
}
2730
}
2831
)
@@ -32,6 +35,7 @@ pub fn string<'a>(
3235
substring: &mut impl Iterator<Item = &'a str>,
3336
format: &str,
3437
position: &str,
38+
filter: &str,
3539
) -> Result<String, String> {
36-
substring.next().map(ToOwned::to_owned).ok_or_else(||format! ("invalid options for function_in_lines filter\nexpected {format}\n missing {position} [number]"))
40+
substring.next().map(ToOwned::to_owned).ok_or_else(||format! ("invalid options for function_with_parameter filter\nexpected {format}\n missing {position} [number]"))
3741
}

function-grep/src/filter/general_filters.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@ pub struct FunctionInLines;
1212
impl FunctionInLines {
1313
fn from_str(s: &str) -> Result<(usize, usize), String> {
1414
let mut substring = s.split(' ').filter(|s| *s != " ");
15+
let filter = "function_with_parameters";
1516
let fst = substring.next().ok_or("invalid options for function_in_lines filter\nexpected [number] [number], start: [number] end: [number], or end: [number] start: [number]")?;
1617
match fst {
1718
"start:" => {
1819
let format = "start: [number] end: [number]";
19-
let start = number(&mut substring, format, "start:")?;
20-
label(&mut substring, format, "end:")?;
21-
let end = number(&mut substring, format, "end:")?;
22-
extra(&mut substring, format)?;
20+
let start = number(&mut substring, format, "start:", filter)?;
21+
label(&mut substring, format, "end:", filter)?;
22+
let end = number(&mut substring, format, "end:", filter)?;
23+
extra(&mut substring, format, filter)?;
2324
Ok((start, end))
2425
}
2526
"end:" => {
2627
let format = "end: [number] start: [number]";
27-
let end = number(&mut substring, format, "end:")?;
28-
label(&mut substring, format, "start:")?;
29-
let start = number(&mut substring, format, "start:")?;
30-
extra(&mut substring, format)?;
28+
let end = number(&mut substring, format, "end:", filter)?;
29+
label(&mut substring, format, "start:", filter)?;
30+
let start = number(&mut substring, format, "start:", filter)?;
31+
extra(&mut substring, format, filter)?;
3132
Ok((start, end))
3233
}
3334
n => {
3435
if let Ok(start) = n.parse() {
35-
let end = number(&mut substring, "[number] [number]", "second")?;
36-
extra(&mut substring, "[number] [number]")?;
36+
let end = number(&mut substring, "[number] [number]", "second", filter)?;
37+
extra(&mut substring, "[number] [number]", filter)?;
3738
Ok((start, end))
3839
} else {
3940
Err(format!("invalid options for function_in_lines filter\nexpected [number] [number], start: [number] end: [number], or end: [number] start: [number]\ngiven {n}"))
@@ -136,32 +137,6 @@ impl HasFilterInformation for FunctionWithParameterRust {
136137
}
137138

138139
impl Filter for FunctionWithParameterRust {
139-
fn parse_filter(&self, s: &str) -> Result<FilterFunction, String> {
140-
todo!()
141-
}
142-
}
143-
144-
impl HasFilterInformation for FunctionWithParameterPython {
145-
fn filter_name(&self) -> String {
146-
"function_with_parameter".to_string()
147-
}
148-
149-
fn description(&self) -> String {
150-
"Find a function with a given parameter".to_string()
151-
}
152-
153-
fn supports(&self) -> Self::Supports {
154-
Language("Python".to_owned())
155-
}
156-
157-
fn attributes(&self) -> Attributes {
158-
HashMap::from([(Attribute("name".to_string()), AttributeType::String)])
159-
}
160-
161-
type Supports = Language;
162-
}
163-
164-
impl Filter for FunctionWithParameterPython {
165140
fn parse_filter(&self, s: &str) -> Result<FilterFunction, String> {
166141
let query = Query::new(
167142
&tree_sitter_rust::LANGUAGE.into(),
@@ -202,21 +177,48 @@ impl Filter for FunctionWithParameterPython {
202177
}
203178
}
204179

180+
impl HasFilterInformation for FunctionWithParameterPython {
181+
fn filter_name(&self) -> String {
182+
"function_with_parameter".to_string()
183+
}
184+
185+
fn description(&self) -> String {
186+
"Find a function with a given parameter".to_string()
187+
}
188+
189+
fn supports(&self) -> Self::Supports {
190+
Language("Python".to_owned())
191+
}
192+
193+
fn attributes(&self) -> Attributes {
194+
HashMap::from([(Attribute("name".to_string()), AttributeType::String)])
195+
}
196+
197+
type Supports = Language;
198+
}
199+
200+
impl Filter for FunctionWithParameterPython {
201+
fn parse_filter(&self, s: &str) -> Result<FilterFunction, String> {
202+
todo!()
203+
}
204+
}
205+
205206
fn parse_with_param(s: &str) -> Result<String, String> {
206207
let mut substring = s.split(' ').filter(|s| *s != " ");
207208
let fst = substring.next().ok_or(
208-
"invalid options for function_in_lines filter\nexpected [string] or name: [string]",
209+
"invalid options for function_with_parameter filter\nexpected [string] or name: [string]",
209210
)?;
211+
let filter = "function_with_parameters";
210212
match fst {
211-
"start:" => {
213+
"name:" => {
212214
let format = "name: [string]";
213-
let name = string(&mut substring, format, "name:")?;
214-
extra(&mut substring, format)?;
215+
let name = string(&mut substring, format, "name:", filter)?;
216+
extra(&mut substring, format, filter)?;
215217
Ok(name)
216218
}
217219

218220
name => {
219-
extra(&mut substring, "[string]")?;
221+
extra(&mut substring, "[string]", filter)?;
220222
Ok(name.to_string())
221223
}
222224
}

0 commit comments

Comments
 (0)