41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
|
|
use std::{io::Read, path::PathBuf};
|
||
|
|
use std::fs::File;
|
||
|
|
use std::io::{BufRead, BufReader, Cursor};
|
||
|
|
|
||
|
|
pub fn handle(path: PathBuf,filter_str:String, start_index: u32, end_index: u32) -> Box<dyn Read>{
|
||
|
|
let reader = BufReader::new(File::open(path).unwrap());
|
||
|
|
let mut lines = reader.lines().collect::<Result<Vec<_>, _>>().unwrap();
|
||
|
|
lines = filter_by_string(lines, filter_str.as_str());
|
||
|
|
lines = take(&mut lines, start_index, end_index);
|
||
|
|
|
||
|
|
Box::new(Cursor::new(lines.join("\n").into_bytes())) as Box<dyn Read>
|
||
|
|
}
|
||
|
|
|
||
|
|
fn filter_by_string(reader: Vec<String>, filter_str: &str) ->Vec<String>{
|
||
|
|
if filter_str == "" {
|
||
|
|
return reader
|
||
|
|
}
|
||
|
|
let mut result: Vec<String> = Vec::new();
|
||
|
|
result.push(reader[0].clone());
|
||
|
|
for line in reader[1..].iter(){
|
||
|
|
if line.contains(filter_str){
|
||
|
|
result.push(line.to_string());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<String> {
|
||
|
|
if end_index == 0 && start_index == 0{
|
||
|
|
return reader.to_owned();
|
||
|
|
}
|
||
|
|
let header = reader[0].clone();
|
||
|
|
let mut body= Vec::new();
|
||
|
|
body.push(header);
|
||
|
|
if end_index == 0{
|
||
|
|
body.append(&mut reader[start_index as usize..].to_owned());
|
||
|
|
}else{
|
||
|
|
body.append(&mut reader[start_index as usize..end_index as usize].to_owned());
|
||
|
|
}
|
||
|
|
body
|
||
|
|
}
|