Решение на Форматиране на импорти от Васил Тодоров
Резултати
- 14 точки от тестове
- 0 бонус точки
- 14 точки общо
- 14 успешни тест(а)
- 6 неуспешни тест(а)
Код
#[derive(PartialEq)]
pub enum Order {
Original,
Sorted,
}
pub struct Import<'a>(pub &'a [&'a str]);
impl<'a> Import<'a> {
pub fn new(parts: &'a [&'a str]) -> Self {
Import(parts)
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut unique_imports: Vec<String> = Vec::new();
for import in imports {
let import_str = import.0.join("::");
if !unique_imports.contains(&import_str) {
unique_imports.push(import_str);
}
}
if order == Order::Sorted {
unique_imports.sort();
}
return unique_imports;
}
//fn helper(nested: String){
//}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut tree: Vec<(String, Vec<String>)> = Vec::new();
for import in imports {
let path = import.0;
let crate_name = path[0].to_string();
let module_path = path[1..].join("::");
let mut inserted = false;
for (crate_name_in_tree, modules) in &mut tree {
if *crate_name_in_tree == crate_name {
if !modules.contains(&module_path) {
modules.push(module_path.clone());
}
inserted = true;
break;
}
}
if !inserted {
tree.push((crate_name, vec![module_path]));
}
}
if order == Order::Sorted {
tree.sort_by(|a, b| a.0.cmp(&b.0));
for (_, modules) in &mut tree {
modules.sort();
}
}
let mut result = Vec::new();
for (crate_name, modules) in tree {
let mut nested = format!("{}::{{\n ", crate_name);
let mut module_map: Vec<(String, Vec<String>)> = Vec::new();
for module in modules {
let parts: Vec<&str> = module.split("::").collect();
if parts.len() == 1 {
module_map.push(("".to_string(), vec![parts[0].to_string()]));
} else {
let module_name = parts[0].to_string();
let submodule = parts[1..].join("::");
let mut added = false;
for (existing_module, items) in &mut module_map {
if *existing_module == module_name {
items.push(submodule.clone());
added = true;
break;
}
}
if !added {
module_map.push((module_name, vec![submodule]));
}
}
}
let mut first = true;
for (module, items) in module_map {
if !first {
nested.push_str(",\n ");
}
first = false;
if module.is_empty() {
let mut first_item = true;
for item in items {
if !first_item {
nested.push_str(",\n ");
}
first_item = false;
nested.push_str(&item);
}
} else {
nested.push_str(&format!("{}::{{\n ", module));
let mut first_item = true;
for item in items {
if !first_item {
nested.push_str(",\n ");
}
first_item = false;
nested.push_str(&item);
}
nested.push_str(",\n }");
}
}
nested.push_str(",\n}\n");
result.push(nested);
}
return result;
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-zmo04y/solution) Finished test [unoptimized + debuginfo] target(s) in 1.20s 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 ... ok test solution_test::test_nested_empty ... ok test solution_test::test_nested_deep ... FAILED test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original ... ok test solution_test::test_nested_original_duplicates ... ok test solution_test::test_nested_original_2 ... FAILED test solution_test::test_nested_original_multi_crate ... ok test solution_test::test_nested_original_self ... FAILED test solution_test::test_nested_sorted ... ok test solution_test::test_nested_sorted_2 ... FAILED test solution_test::test_nested_sorted_duplicates ... ok test solution_test::test_nested_sorted_multi_crate ... ok test solution_test::test_nested_sorted_self ... FAILED failures: ---- solution_test::test_nested_deep stdout ---- thread 'solution_test::test_nested_deep' panicked at 'assertion failed: `(left == right)` left: `["std::{\n a::{\n b::c::d,\n },\n}\n"]`, right: `["std::{\n a::{\n b::{\n c::{\n d,\n },\n },\n },\n}\n"]`', tests/solution_test.rs:160:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_nested_only_crate stdout ---- thread 'solution_test::test_nested_only_crate' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n ,\n}\n"]`, right: `["my_crate\n"]`', tests/solution_test.rs:132:5 ---- solution_test::test_nested_original_2 stdout ---- thread 'solution_test::test_nested_original_2' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a::{\n inner::I1,\n A1,\n },\n}\n"]`, right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a::{\n inner::{\n I1,\n },\n A1,\n },\n}\n"]`', tests/solution_test.rs:198:5 ---- solution_test::test_nested_original_self stdout ---- thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a,\n b,\n}\n"]`, right: `["my_crate::{\n c,\n b::{\n self,\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:334:5 ---- solution_test::test_nested_sorted_2 stdout ---- thread 'solution_test::test_nested_sorted_2' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a::{\n A1,\n inner::I1,\n },\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`, right: `["my_crate::{\n a::{\n A1,\n inner::{\n I1,\n },\n },\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:252:5 ---- solution_test::test_nested_sorted_self stdout ---- thread 'solution_test::test_nested_sorted_self' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a,\n b,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`, right: `["my_crate::{\n a,\n b::{\n self,\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:360:5 failures: solution_test::test_nested_deep solution_test::test_nested_only_crate solution_test::test_nested_original_2 solution_test::test_nested_original_self solution_test::test_nested_sorted_2 solution_test::test_nested_sorted_self test result: FAILED. 14 passed; 6 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`