异常处理

This commit is contained in:
JIe 2024-11-21 09:50:20 +08:00
parent 7cd9616cf6
commit cb63a82646

View File

@ -1,8 +1,9 @@
use std::{io::Read, path::PathBuf};
use std::fs::File;
use std::io::{BufRead, BufReader, Cursor};
use std::ptr::read;
pub fn handle(path: PathBuf,filter_str:String, start_index: u32, end_index: u32) -> Box<dyn Read>{
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());
@ -22,6 +23,9 @@ fn filter_by_string(reader: Vec<String>, filter_str: &str) ->Vec<String>{
}
fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<String> {
if end_index < 0 && start_index < 0{
panic!("start_index and end_index must be positive");
}
if end_index == 0 && start_index == 0{
return reader.to_owned();
}
@ -30,8 +34,10 @@ fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<Strin
body.push(header);
if end_index == 0{
body.append(&mut reader[start_index as usize..].to_owned());
}else{
}else if(end_index < reader.len() as u32){
body.append(&mut reader[start_index as usize..end_index as usize].to_owned());
}else {
body.append(&mut reader[start_index as usize..].to_owned());
}
body
}