Решение на CSV Filter от Илиян Руйков
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 14 неуспешни тест(а)
Код
use std::io::BufReader;
use std::fs::File;
pub fn skip_next(input: &str, target: char) -> Option<&str> {
let len = input.chars().count();
if len <= 0 {
None
}
else {
let first = input.chars().next().unwrap();
if first == target {
Some(&input[1..])
}
else {
None
}
}
}
pub fn take_until(input: &str, target: char) -> (&str, &str) {
let len = input.chars().count();
if len <= 0 {
("", "")
}
else {
let mut current = 0;
let mut target_postion = len;
for elem in input.chars() {
if elem == target {
target_postion = current;
break;
}
current += 1;
}
(&input[..target_postion], &input[target_postion..])
}
}
pub fn take_and_skip(input: &str, target: char) -> Option<(&str, &str)> {
let len = input.chars().count();
if len <= 0 {
None
}
else {
let mut current = 0;
let mut met_target = false;
let mut target_postion = len;
for elem in input.chars() {
if elem == target {
target_postion = current;
met_target = true;
break;
}
current += 1;
}
if met_target == false {
None
}
else {
Some((&input[..target_postion], &input[(target_postion + 1)..]))
}
}
}
#[derive(Debug)]
pub enum CsvError {
IO(std::io::Error),
ParseError(String),
InvalidHeader(String),
InvalidRow(String),
InvalidColumn(String),
}
use std::collections::HashMap;
type Row = HashMap<String, String>;
use std::io::BufRead;
pub struct Csv<R: BufRead> {
pub columns: Vec<String>,
reader: R,
selection: Option<Box<dyn Fn(&Row) -> Result<bool, CsvError>>>,
}
use std::io::Write;
impl<R: BufRead> Csv<R> {
pub fn new(mut reader: R) -> Result<Self, CsvError> {
todo!()
}
pub fn parse_line(&mut self, line: &str) -> Result<Row, CsvError> {
todo!()
}
pub fn apply_selection<F>(&mut self, callback: F)
where F: Fn(&Row) -> Result<bool, CsvError> + 'static
{
todo!()
}
pub fn write_to<W: Write>(mut self, mut writer: W) -> Result<(), CsvError> {
todo!()
}
}
impl<R: BufRead> Iterator for Csv<R> {
type Item = Result<Row, CsvError>;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20210111-1538662-1wfhr2t/solution) warning: unused import: `std::io::BufReader` --> src/lib.rs:1:5 | 1 | use std::io::BufReader; | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `std::fs::File` --> src/lib.rs:2:5 | 2 | use std::fs::File; | ^^^^^^^^^^^^^ warning: unused variable: `reader` --> src/lib.rs:128:16 | 128 | pub fn new(mut reader: R) -> Result<Self, CsvError> { | ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_reader` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `line` --> src/lib.rs:132:34 | 132 | pub fn parse_line(&mut self, line: &str) -> Result<Row, CsvError> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_line` warning: unused variable: `callback` --> src/lib.rs:136:42 | 136 | pub fn apply_selection<F>(&mut self, callback: F) | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_callback` warning: unused variable: `writer` --> src/lib.rs:142:41 | 142 | pub fn write_to<W: Write>(mut self, mut writer: W) -> Result<(), CsvError> { | ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_writer` warning: variable does not need to be mutable --> src/lib.rs:128:16 | 128 | pub fn new(mut reader: R) -> Result<Self, CsvError> { | ----^^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: variable does not need to be mutable --> src/lib.rs:142:31 | 142 | pub fn write_to<W: Write>(mut self, mut writer: W) -> Result<(), CsvError> { | ----^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:142:41 | 142 | pub fn write_to<W: Write>(mut self, mut writer: W) -> Result<(), CsvError> { | ----^^^^^^ | | | help: remove this `mut` warning: field is never read: `reader` --> src/lib.rs:120:5 | 120 | reader: R, | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: field is never read: `selection` --> src/lib.rs:121:5 | 121 | selection: Option<Box<dyn Fn(&Row) -> Result<bool, CsvError>>>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: 11 warnings emitted Finished test [unoptimized + debuginfo] target(s) in 3.74s Running target/debug/deps/solution_test-8916805fc40a2dab running 15 tests test solution_test::test_csv_basic ... FAILED test solution_test::test_csv_duplicate_columns ... FAILED test solution_test::test_csv_empty ... FAILED test solution_test::test_csv_iterating_with_a_selection ... FAILED test solution_test::test_csv_iterating_with_no_selection ... FAILED test solution_test::test_csv_parse_line ... FAILED test solution_test::test_csv_parse_line_with_commas ... FAILED test solution_test::test_csv_selection_and_writing ... FAILED test solution_test::test_csv_single_column_no_data ... FAILED test solution_test::test_csv_writing_without_a_selection ... FAILED test solution_test::test_csv_writing_without_any_rows ... FAILED test solution_test::test_parsing_helpers_for_unicode ... FAILED test solution_test::test_skip_next ... ok test solution_test::test_take_and_skip ... FAILED test solution_test::test_take_until ... FAILED failures: ---- solution_test::test_csv_basic stdout ---- thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:60:5 ---- solution_test::test_csv_duplicate_columns stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_empty stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_iterating_with_a_selection stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_iterating_with_no_selection stdout ---- thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:185:5 ---- solution_test::test_csv_parse_line stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_parse_line_with_commas stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_selection_and_writing stdout ---- thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:251:5 ---- solution_test::test_csv_single_column_no_data stdout ---- thread 'main' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 ---- solution_test::test_csv_writing_without_a_selection stdout ---- thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:224:5 ---- solution_test::test_csv_writing_without_any_rows stdout ---- thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20210111-1538662-1wfhr2t/solution/src/lib.rs:129:9 thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:278:5 ---- solution_test::test_parsing_helpers_for_unicode stdout ---- thread 'main' panicked at 'byte index 1 is not a char boundary; it is inside '↓' (bytes 0..3) of `↓яга`', src/lib.rs:17:19 ---- solution_test::test_take_and_skip stdout ---- thread 'main' panicked at 'byte index 5 is not a char boundary; it is inside 'б' (bytes 4..6) of `баба/яга`', src/lib.rs:95:46 ---- solution_test::test_take_until stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `("ба", "ба/яга")`, right: `("баба", "/яга")`', tests/solution_test.rs:121:5 failures: solution_test::test_csv_basic solution_test::test_csv_duplicate_columns solution_test::test_csv_empty solution_test::test_csv_iterating_with_a_selection solution_test::test_csv_iterating_with_no_selection solution_test::test_csv_parse_line solution_test::test_csv_parse_line_with_commas solution_test::test_csv_selection_and_writing solution_test::test_csv_single_column_no_data solution_test::test_csv_writing_without_a_selection solution_test::test_csv_writing_without_any_rows solution_test::test_parsing_helpers_for_unicode solution_test::test_take_and_skip solution_test::test_take_until test result: FAILED. 1 passed; 14 failed; 0 ignored; 0 measured; 0 filtered out error: test failed, to rerun pass '--test solution_test'