Решение на Форматиране на импорти от Мариан Марчев

Обратно към всички решения

Към профила на Мариан Марчев

Резултати

  • 17 точки от тестове
  • 0 бонус точки
  • 17 точки общо
  • 17 успешни тест(а)
  • 3 неуспешни тест(а)

Код

pub struct Import<'a>(pub &'a [&'a str]);
#[derive(PartialEq)]
pub enum Order {
Original,
Sorted,
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut unique_imports: Vec<String> = Vec::new();
for import in imports {
let path = import.0.join("::");
if !unique_imports.contains(&path) {
unique_imports.push(path);
}
}
if order == Order::Sorted {
unique_imports.sort();
}
unique_imports
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
#[derive(Debug)]
struct Node {
name: String,
children: Vec<Node>,
is_final: bool,
}
impl Node {
fn new(name: &str) -> Self {
Node {
name: name.to_string(),
children: Vec::new(),
is_final: false,
}
}
fn insert(&mut self, path: &[&str]) {
if path.is_empty() {
self.is_final = true;
return;
}
let (first, rest) = path.split_first().unwrap();
if let Some(child) = self.children.iter_mut().find(|c| c.name == *first) {
child.insert(rest);
} else {
let mut new_node = Node::new(first);
new_node.insert(rest);
self.children.push(new_node);
}
}
fn sort_children(&mut self) {
self.children.sort_by(|a, b| a.name.cmp(&b.name));
for c in &mut self.children {
c.sort_children();
}
}
fn display(&self, depth: usize) -> String {
let mut result = String::new();
let indent = " ".repeat(depth);
for child in &self.children {
if child.children.is_empty() {
result.push_str(&format!("{}{},\n", indent, child.name));
} else {
result.push_str(&format!("{}{}::{{\n", indent, child.name));
result.push_str(&child.display(depth + 4));
result.push_str(&format!("{}}},\n", indent));
}
}
result
}
}
let mut root_nodes: Vec<Node> = Vec::new();
for import in imports {
let (root, tail) = import.0.split_first().unwrap();
if let Some(existing_root) = root_nodes.iter_mut().find(|n| n.name == *root) {
existing_root.insert(tail);
} else {
let mut new_root = Node::new(root);
new_root.insert(tail);
root_nodes.push(new_root);
}
}
if order == Order::Sorted {
root_nodes.sort_by(|a, b| a.name.cmp(&b.name));
for root in &mut root_nodes {
root.sort_children();
}
}
root_nodes.into_iter().map(|node| {
let mut result = format!("{}::{{\n", node.name);
result.push_str(&node.display(4));
result.push_str("}\n");
result
}).collect()
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20241203-1739405-1h4k5ds/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.15s
     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_deep ... ok
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_original ... ok
test solution_test::test_nested_only_crate ... FAILED
test solution_test::test_nested_original_duplicates ... ok
test solution_test::test_nested_original_2 ... ok
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 ... 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 ... FAILED

failures:

---- 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"]`,
 right: `["my_crate\n"]`', tests/solution_test.rs:132:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- 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}\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_self stdout ----
thread 'solution_test::test_nested_sorted_self' 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    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_only_crate
    solution_test::test_nested_original_self
    solution_test::test_nested_sorted_self

test result: FAILED. 17 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Мариан качи първо решение на 03.12.2024 16:56 (преди 9 месеца)