Решение на Форматиране на импорти от Клементина Картевска
Към профила на Клементина Картевска
Резултати
- 11 точки от тестове
- 0 бонус точки
- 11 точки общо
- 11 успешни тест(а)
- 9 неуспешни тест(а)
Код
use std::collections::{BTreeMap, HashMap};
pub struct Import<'a>(pub &'a [&'a str]);
pub enum Order {
Original,
Sorted,
}
#[derive(Debug, Default)]
struct Node {
children: BTreeMap<String, Node>,
has_self: bool,
}
impl Node {
fn insert(&mut self, path: &[&str]) {
if path.is_empty() {
return;
}
let (first, rest) = path.split_first().unwrap();
let child = self.children.entry(first.to_string()).or_default();
if rest.is_empty() {
child.has_self = true;
} else {
child.insert(rest);
}
}
fn format(&self, indent: usize, order: &Order) -> String {
let mut lines = vec![];
if self.has_self {
lines.push(format!("{}self,", " ".repeat(indent)));
}
let mut keys: Vec<_> = self.children.keys().collect();
if let Order::Sorted = order {
keys.sort();
}
for key in keys {
let child = &self.children[key];
if child.children.is_empty() {
lines.push(format!("{}{},", " ".repeat(indent), key));
} else {
lines.push(format!("{}{}::{{", " ".repeat(indent), key));
lines.push(child.format(indent + 4, order));
lines.push(format!("{}}},", " ".repeat(indent)));
}
}
lines.join("\n")
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut unique_imports: Vec<String> = imports
.iter()
.map(|import| import.0.join("::"))
.collect();
unique_imports.sort();
unique_imports.dedup();
if let Order::Original = order {
let mut seen = HashMap::new();
unique_imports.retain(|imp| seen.insert(imp.clone(), true).is_none());
}
unique_imports
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut roots: HashMap<String, Node> = HashMap::new();
for import in imports {
if let Some((root, rest)) = import.0.split_first() {
roots.entry((*root).to_string()).or_default().insert(rest);
}
}
let mut keys: Vec<_> = roots.keys().collect();
if let Order::Sorted = order {
keys.sort();
}
keys
.into_iter()
.map(|key| {
let node = &roots[key];
let formatted = node.format(4, &order);
format!("{}::{{\n{}\n}}\n", key, formatted) // Добавен нов ред тук
})
.collect()
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-1lb0t1n/solution) Finished test [unoptimized + debuginfo] target(s) in 1.47s 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 ... FAILED test solution_test::test_flat_original ... FAILED test solution_test::test_flat_original_duplicates ... FAILED 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_deep ... ok test solution_test::test_nested_empty ... ok test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original_2 ... FAILED test solution_test::test_nested_original ... FAILED test solution_test::test_nested_original_multi_crate ... FAILED test solution_test::test_nested_original_duplicates ... FAILED test solution_test::test_nested_sorted ... ok test solution_test::test_nested_original_self ... FAILED test solution_test::test_nested_sorted_2 ... ok test solution_test::test_nested_sorted_duplicates ... ok test solution_test::test_nested_sorted_multi_crate ... ok test solution_test::test_nested_sorted_self ... ok failures: ---- solution_test::test_flat_multi_crate stdout ---- thread 'solution_test::test_flat_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["bar::string::String", "foo::string::String", "std::string::String"]`, right: `["std::string::String", "foo::string::String", "bar::string::String"]`', tests/solution_test.rs:107:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_flat_original stdout ---- thread 'solution_test::test_flat_original' panicked at 'assertion failed: `(left == right)` left: `["my_crate::a", "my_crate::b", "my_crate::c::C1", "my_crate::c::C2", "my_crate::x::y::z::W1"]`, right: `["my_crate::c::C1", "my_crate::b", "my_crate::x::y::z::W1", "my_crate::a", "my_crate::c::C2"]`', tests/solution_test.rs:21:5 ---- solution_test::test_flat_original_duplicates stdout ---- thread 'solution_test::test_flat_original_duplicates' panicked at 'assertion failed: `(left == right)` left: `["std::iter", "std::iter::once", "std::iter::repeat", "std::string::String"]`, right: `["std::string::String", "std::iter::once", "std::iter", "std::iter::repeat"]`', tests/solution_test.rs:44:5 ---- 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 a::{\n A1,\n inner::{\n I1,\n },\n },\n b::{\n B1,\n B2,\n },\n c,\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 stdout ---- thread 'solution_test::test_nested_original' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`, right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:173:5 ---- solution_test::test_nested_original_multi_crate stdout ---- thread 'solution_test::test_nested_original_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["crate::{\n a,\n b,\n}\n", "std::{\n string::{\n String,\n },\n}\n"]`, right: `["crate::{\n b,\n a,\n}\n", "std::{\n string::{\n String,\n },\n}\n"]`', tests/solution_test.rs:385:5 ---- solution_test::test_nested_original_duplicates stdout ---- thread 'solution_test::test_nested_original_duplicates' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`, right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:283: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 a,\n b::{\n self,\n B1,\n B2,\n },\n c,\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 failures: solution_test::test_flat_multi_crate solution_test::test_flat_original solution_test::test_flat_original_duplicates 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 test result: FAILED. 11 passed; 9 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`