Решение на CSV Filter от Кристиян Стоянов
Към профила на Кристиян Стоянов
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
pub enum CsvError {
IO(std::io::Error),
ParseError(String),
InvalidHeader(String),
InvalidRow(String),
InvalidColumn(String),
}
pub fn skip_next(input: &str, target: char) -> Option<&str> {
let mut chars = input.chars();
if chars.next()==Some(target)
{
if chars.count()>0
{ return Some(input.chars().next().map(|c| &input[c.len_utf8()..]).unwrap());}
else{
return Some("");
}
}
else
{
return None;
}
}
pub fn take_until(input: &str, target: char) -> (&str, &str) {
let k:Vec<&str>=input.split(target).collect::<Vec<&str>>();
return (k[0],input.strip_prefix(k[0]).unwrap());
}
pub fn take_and_skip(input: &str, target: char) -> Option<(&str, &str)> {
let k:Vec<&str>=input.split(target).collect::<Vec<&str>>();
if k.len()<2
{
return None;
}
else
{
return Some((k[0], k[1]));
}
}
use std::collections::HashMap;
use std::io::BufReader;
use std::io::BufRead;
type Row= HashMap<String, String>;
pub struct Csv<R: BufRead> {
pub columns: Vec<String>,
reader: R,
selection: Option<Box<dyn Fn(&Row) -> Result<bool, CsvError>>>,
}
impl<R: BufRead> Csv<R> {
pub fn new(mut reader: R) -> Result<Self, CsvError> {
let mut O:String=String::new();
reader.read_line(&mut O);
if O.len()==0 {
return Err(CsvError::InvalidHeader("".to_string()));
}
let mut i=O.split(",").collect::<Vec<&str>>();
let mut k:Vec<String>=Vec::<String>::new();
for j in 0..i.len() {
i[j]=i[j].trim_start();
i[j]=i[j].trim_end();
for h in 0..k.len()
{
if k[h]==i[j] {
return Err(CsvError::InvalidHeader("".to_string()));
}
}
k.push(i[j].to_string());
}
return Ok( Csv{columns: k,reader: reader,selection:None });
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20210111-1538662-1vfherq/solution) warning: unused import: `std::io::BufReader` --> src/lib.rs:47:5 | 47 | use std::io::BufReader; | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: field is never read: `reader` --> src/lib.rs:55:5 | 55 | reader: R, | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: field is never read: `selection` --> src/lib.rs:56:5 | 56 | selection: Option<Box<dyn Fn(&Row) -> Result<bool, CsvError>>>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: variable `O` should have a snake case name --> src/lib.rs:62:17 | 62 | let mut O:String=String::new(); | ^ help: convert the identifier to snake case (notice the capitalization): `o` | = note: `#[warn(non_snake_case)]` on by default warning: unused `std::result::Result` that must be used --> src/lib.rs:63:9 | 63 | reader.read_line(&mut O); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_must_use)]` on by default = note: this `Result` may be an `Err` variant, which should be handled warning: 5 warnings emitted error[E0599]: no method named `next` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:70:23 | 70 | let row = csv.next().unwrap().unwrap(); | ^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `next` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:76:21 | 76 | assert!(csv.next().is_none()); | ^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `map` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:195:34 | 195 | let filtered_names = csv.map(|row| row.unwrap()["name"].clone()).collect::<Vec<_>>(); | ^^^ method not found in `solution::Csv<BufReader<&[u8]>>` | ::: /tmp/d20210111-1538662-1vfherq/solution/src/lib.rs:53:1 | 53 | pub struct Csv<R: BufRead> { | -------------------------- doesn't satisfy `solution::Csv<BufReader<&[u8]>>: Iterator` | = note: the method `map` exists but the following trait bounds were not satisfied: `solution::Csv<BufReader<&[u8]>>: Iterator` which is required by `&mut solution::Csv<BufReader<&[u8]>>: Iterator` error[E0599]: no method named `write_to` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:234:13 | 234 | csv.write_to(&mut output).unwrap(); | ^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `apply_selection` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:260:13 | 260 | csv.apply_selection(|row| Ok(row["name"].contains("."))); | ^^^^^^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `write_to` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:263:13 | 263 | csv.write_to(&mut output).unwrap(); | ^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `write_to` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:285:13 | 285 | csv.write_to(&mut output).unwrap(); | ^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `parse_line` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:144:19 | 144 | let row = csv.parse_line(r#""Basic Name","13","2020-01-01""#).unwrap(); | ^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `parse_line` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:150:19 | 150 | let row = csv.parse_line(r#"" Name With Spaces "," 13 ","0-0-0""#).unwrap(); | ^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `parse_line` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:162:19 | 162 | let row = csv.parse_line(r#""13", "Name, Basic""#).unwrap(); | ^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `parse_line` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:168:19 | 168 | let row = csv.parse_line(r#""13, or maybe 14","Basic Name""#).unwrap(); | ^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `next` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:180:17 | 180 | assert!(csv.next().is_none()); | ^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `apply_selection` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:211:9 | 211 | csv.apply_selection(|row| { | ^^^^^^^^^^^^^^^ method not found in `solution::Csv<BufReader<&[u8]>>` error[E0599]: no method named `map` found for struct `solution::Csv<BufReader<&[u8]>>` in the current scope --> tests/solution_test.rs:218:30 | 218 | let filtered_names = csv.map(|row| row.unwrap()["name"].clone()).collect::<Vec<_>>(); | ^^^ method not found in `solution::Csv<BufReader<&[u8]>>` | ::: /tmp/d20210111-1538662-1vfherq/solution/src/lib.rs:53:1 | 53 | pub struct Csv<R: BufRead> { | -------------------------- doesn't satisfy `solution::Csv<BufReader<&[u8]>>: Iterator` | = note: the method `map` exists but the following trait bounds were not satisfied: `solution::Csv<BufReader<&[u8]>>: Iterator` which is required by `&mut solution::Csv<BufReader<&[u8]>>: Iterator` error: aborting due to 14 previous errors For more information about this error, try `rustc --explain E0599`. error: could not compile `solution` To learn more, run the command again with --verbose.