Решение на Форматиране на импорти от Илиян Георгиев
Резултати
- 6 точки от тестове
- 0 бонус точки
- 6 точки общо
- 6 успешни тест(а)
- 14 неуспешни тест(а)
Код
use std::{collections::HashSet, vec};
use std::hash::{Hash, Hasher};
#[derive(Debug, Clone)]
pub struct Import<'a>(
pub &'a [&'a str]
);
impl<'a> PartialEq for Import<'a> {
fn eq(&self, other: &Self) -> bool {
if self.0.len() != other.0.len() {
return false;
}
for i in 0..self.0.len() {
if self.0[i] != other.0[i] {
return false;
}
}
return true;
}
}
impl<'a> Eq for Import<'a> {}
pub enum Order {
Original,
Sorted,
}
impl<'a> Hash for Import<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
for item in self.0 {
item.hash(state);
}
}
}
pub fn import_to_string(import: Import) -> String {
let mut import_string: String = String::from(import.0[0]);
for i in 1..import.0.len() {
import_string.insert_str(import_string.len(), "::");
import_string.insert_str(import_string.len(), String::from(import.0[i]).as_str());
}
import_string
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let uniques: Vec<&Import<'_>> = remove_dupes(imports);
let mut result: Vec<String> = Vec::new();
for item in uniques {
let mut import_string = import_to_string(item.clone());
result.push(import_string);
}
match order {
Order::Original => {},
Order::Sorted => {
result.sort();
}
}
return result;
}
pub fn format_nested(imps: &[Import], order: Order) -> Vec<String> {
let uniques: Vec<&Import<'_>> = remove_dupes(imps);
let mut to_sort: Vec<&Import> = uniques.to_vec();
let mut result: Vec<String> = Vec::new();
match order {
Order::Original => {
},
Order::Sorted => {
to_sort.sort_by(|a, b| {
a.0.cmp(&b.0)
});
println!("{:?}", to_sort);
let mut current_padding = 0;
for i in 0..to_sort.len() {
}
}
}
todo!()
}
pub fn remove_dupes<'a>(imports: &'a[Import<'a>]) -> Vec<&'a Import<'a>> {
let mut uniques: Vec<&Import<'a>> = Vec::new();
let mut seen: HashSet<&Import<'a>> = HashSet::new();
for import in imports {
if !seen.contains(import) {
uniques.push(import);
seen.insert(import);
}
}
uniques
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-jucbx3/solution) warning: unused import: `vec` --> src/lib.rs:1:33 | 1 | use std::{collections::HashSet, vec}; | ^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused variable: `result` --> src/lib.rs:73:13 | 73 | let mut result: Vec<String> = Vec::new(); | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_result` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `current_padding` --> src/lib.rs:85:21 | 85 | let mut current_padding = 0; | ^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_current_padding` warning: unused variable: `i` --> src/lib.rs:86:17 | 86 | for i in 0..to_sort.len() { | ^ help: if this is intentional, prefix it with an underscore: `_i` warning: variable does not need to be mutable --> src/lib.rs:55:13 | 55 | let mut import_string = import_to_string(item.clone()); | ----^^^^^^^^^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: variable does not need to be mutable --> src/lib.rs:73:9 | 73 | let mut result: Vec<String> = Vec::new(); | ----^^^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:85:17 | 85 | let mut current_padding = 0; | ----^^^^^^^^^^^^^^^ | | | help: remove this `mut` warning: `solution` (lib) generated 7 warnings Finished test [unoptimized + debuginfo] target(s) in 1.95s Running tests/solution_test.rs (target/debug/deps/solution_test-1428e1090729d165) running 20 tests test solution_test::test_flat_empty ... ok test solution_test::test_flat_multi_crate ... ok test solution_test::test_flat_original ... ok test solution_test::test_flat_original_duplicates ... ok test solution_test::test_flat_sorted ... ok test solution_test::test_flat_sorted_duplicates ... ok test solution_test::test_nested_basic ... FAILED test solution_test::test_nested_deep ... FAILED test solution_test::test_nested_empty ... FAILED test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original ... FAILED test solution_test::test_nested_original_2 ... FAILED test solution_test::test_nested_original_duplicates ... FAILED test solution_test::test_nested_original_multi_crate ... FAILED test solution_test::test_nested_original_self ... FAILED test solution_test::test_nested_sorted ... FAILED test solution_test::test_nested_sorted_2 ... FAILED test solution_test::test_nested_sorted_duplicates ... FAILED test solution_test::test_nested_sorted_multi_crate ... FAILED test solution_test::test_nested_sorted_self ... FAILED failures: ---- solution_test::test_nested_basic stdout ---- thread 'solution_test::test_nested_basic' panicked at 'not yet implemented', src/lib.rs:92:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_nested_deep stdout ---- thread 'solution_test::test_nested_deep' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_empty stdout ---- thread 'solution_test::test_nested_empty' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_only_crate stdout ---- thread 'solution_test::test_nested_only_crate' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_original stdout ---- thread 'solution_test::test_nested_original' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_original_2 stdout ---- thread 'solution_test::test_nested_original_2' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_original_duplicates stdout ---- thread 'solution_test::test_nested_original_duplicates' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_original_multi_crate stdout ---- thread 'solution_test::test_nested_original_multi_crate' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_original_self stdout ---- thread 'solution_test::test_nested_original_self' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_sorted stdout ---- [Import(["my_crate", "a"]), Import(["my_crate", "b", "B1"]), Import(["my_crate", "b", "B2"]), Import(["my_crate", "c"])] thread 'solution_test::test_nested_sorted' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_sorted_2 stdout ---- [Import(["my_crate", "a", "A1"]), Import(["my_crate", "a", "inner", "I1"]), Import(["my_crate", "b", "B1"]), Import(["my_crate", "b", "B2"]), Import(["my_crate", "c"])] thread 'solution_test::test_nested_sorted_2' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_sorted_duplicates stdout ---- [Import(["my_crate", "a"]), Import(["my_crate", "b", "B1"]), Import(["my_crate", "b", "B2"]), Import(["my_crate", "c"])] thread 'solution_test::test_nested_sorted_duplicates' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_sorted_multi_crate stdout ---- [Import(["crate", "a"]), Import(["crate", "b"]), Import(["std", "string", "String"])] thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'not yet implemented', src/lib.rs:92:5 ---- solution_test::test_nested_sorted_self stdout ---- [Import(["my_crate", "a"]), Import(["my_crate", "b"]), Import(["my_crate", "b", "B1"]), Import(["my_crate", "b", "B2"]), Import(["my_crate", "c"])] thread 'solution_test::test_nested_sorted_self' panicked at 'not yet implemented', src/lib.rs:92:5 failures: solution_test::test_nested_basic solution_test::test_nested_deep solution_test::test_nested_empty solution_test::test_nested_only_crate solution_test::test_nested_original solution_test::test_nested_original_2 solution_test::test_nested_original_duplicates solution_test::test_nested_original_multi_crate solution_test::test_nested_original_self solution_test::test_nested_sorted solution_test::test_nested_sorted_2 solution_test::test_nested_sorted_duplicates solution_test::test_nested_sorted_multi_crate solution_test::test_nested_sorted_self test result: FAILED. 6 passed; 14 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`