Решение на Форматиране на импорти от Ивайло Генчев
Резултати
- 7 точки от тестове
- 0 бонус точки
- 7 точки общо
- 7 успешни тест(а)
- 13 неуспешни тест(а)
Код
use std::cmp::Ordering;
pub struct Import<'a>(pub &'a [&'a str]);
pub enum Order {
Original,
Sorted,
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
for import in imports {
res.push(import.0.join("::"));
}
if let Order::Sorted = order {
res.sort();
}
// remove duplicates
let mut unique = Vec::new();
let mut seen = std::collections::HashSet::new();
for str in res {
if !seen.contains(&str) {
seen.insert(str.clone());
unique.push(str);
}
}
unique
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
// use format_flat to sort if need be
let flat = format_flat(imports, order);
// let mut tree = Tree::new();
//
// for import in flat {
// for module in import.split("::") {
// // curr_node.add_child(module);
// }
// }
// let tree = Tree::create_from_imports(imports);
/*
* Iterate over the direct children of tree.root
* DFS for each one - each DFS is a new top-level module
* in recursion, while going forward, append to String:
* "<name>::{\n" for non-leaf vertex (remember depth level for proper indentation).
* "<name>,\n" if vertex is a leaf
* when backtracking close the braces
*
* append string to vector for each DFS
*/
// let mut res: Vec<String> = Vec::new();
flat
}
pub struct Tree {
root: Node,
}
#[derive(Clone)]
pub struct Node {
value: String,
children: Vec<Node>,
}
impl Node {
pub fn new(value: String, children: Vec<Node>) -> Node {
Node { value, children }
}
pub fn add_child(&mut self, value: &str) {
self.children.push(Node {
value: value.into(),
children: Vec::new(),
});
}
}
impl<'a> Tree {
pub fn new() -> Tree {
Tree {
root: Node {
value: "".into(),
children: Vec::new(),
},
}
}
pub fn get_node(value: &str, root: &'a Node) -> Option<&'a Node> {
if let Ordering::Equal = root.value.cmp(&String::from(value)) {
return Some(root);
}
for child in &root.children {
match Self::get_node(value, child) {
Some(node) => {
return Some(node);
}
None => {}
}
}
None
}
pub fn get_node_mut(value: &str, root: &'a mut Node) -> Option<&'a mut Node> {
if let Ordering::Equal = root.value.cmp(&String::from(value)) {
return Some(root);
}
for child in &mut root.children {
match Self::get_node_mut(value, child) {
Some(node) => {
return Some(node);
}
None => {}
}
}
None
}
// pub fn create_from_imports(imports: &[Import]) -> Tree {
// let mut tree = Tree::new();
//
// for import in imports {
//
// let mut curr_node = &mut tree.root;
//
// for module in import.0 {
// match Self::get_node_mut(module, curr_node) {
// None => {
// curr_node.add_child(module);
// }
// Some(node) => {
// curr_node = node;
// }
// }
// }
// }
//
//
// tree
// }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_flat_original() {
let imports = &[
Import(&["my_crate", "c"]),
Import(&["my_crate", "a"]),
Import(&["my_crate", "a"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "b", "B1"]),
];
assert_eq!(
format_flat(imports, Order::Original),
&[
"my_crate::c",
"my_crate::a",
"my_crate::b::B2",
"my_crate::b::B1",
]
);
}
#[test]
fn test_basic_flat_sorted() {
let imports = &[
Import(&["my_crate", "c"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "a"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "b", "B1"]),
];
assert_eq!(
format_flat(imports, Order::Sorted),
&[
"my_crate::a",
"my_crate::b::B1",
"my_crate::b::B2",
"my_crate::c",
]
);
}
#[test]
fn test_basic_nested() {
let imports = &[
Import(&["my_crate", "a"]),
Import(&["my_crate", "b", "B1"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "c"]),
];
assert_eq!(
format_nested(imports, Order::Sorted),
&[concat!(
"my_crate::{\n",
" a,\n",
" b::{\n",
" B1,\n",
" B2,\n",
" },\n",
" c,\n",
"}\n",
)]
);
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-10xegen/solution) warning: field `root` is never read --> src/lib.rs:64:5 | 63 | pub struct Tree { | ---- field in this struct 64 | root: Node, | ^^^^ | = note: `#[warn(dead_code)]` on by default warning: `solution` (lib) generated 1 warning Finished test [unoptimized + debuginfo] target(s) in 1.17s 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 ... ok 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 'assertion failed: `(left == right)` left: `["std::a"]`, right: `["std::{\n a,\n}\n"]`', tests/solution_test.rs:140: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 'assertion failed: `(left == right)` left: `["std::a::b::c::d"]`, right: `["std::{\n a::{\n b::{\n c::{\n d,\n },\n },\n },\n}\n"]`', tests/solution_test.rs:160:5 ---- solution_test::test_nested_only_crate stdout ---- thread 'solution_test::test_nested_only_crate' panicked at 'assertion failed: `(left == right)` left: `["my_crate"]`, right: `["my_crate\n"]`', tests/solution_test.rs:132:5 ---- solution_test::test_nested_original stdout ---- thread 'solution_test::test_nested_original' panicked at 'assertion failed: `(left == right)` left: `["my_crate::c", "my_crate::b::B2", "my_crate::a", "my_crate::b::B1"]`, 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_2 stdout ---- thread 'solution_test::test_nested_original_2' panicked at 'assertion failed: `(left == right)` left: `["my_crate::c", "my_crate::b::B2", "my_crate::a::inner::I1", "my_crate::b::B1", "my_crate::a::A1"]`, 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_duplicates stdout ---- thread 'solution_test::test_nested_original_duplicates' panicked at 'assertion failed: `(left == right)` left: `["my_crate::c", "my_crate::b::B2", "my_crate::a", "my_crate::b::B1"]`, 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_multi_crate stdout ---- thread 'solution_test::test_nested_original_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["crate::b", "std::string::String", "crate::a"]`, 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_self stdout ---- thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)` left: `["my_crate::c", "my_crate::b::B2", "my_crate::a", "my_crate::b::B1", "my_crate::b"]`, 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 stdout ---- thread 'solution_test::test_nested_sorted' panicked at 'assertion failed: `(left == right)` left: `["my_crate::a", "my_crate::b::B1", "my_crate::b::B2", "my_crate::c"]`, right: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:227:5 ---- solution_test::test_nested_sorted_2 stdout ---- thread 'solution_test::test_nested_sorted_2' panicked at 'assertion failed: `(left == right)` left: `["my_crate::a::A1", "my_crate::a::inner::I1", "my_crate::b::B1", "my_crate::b::B2", "my_crate::c"]`, 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_duplicates stdout ---- thread 'solution_test::test_nested_sorted_duplicates' panicked at 'assertion failed: `(left == right)` left: `["my_crate::a", "my_crate::b::B1", "my_crate::b::B2", "my_crate::c"]`, right: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:309:5 ---- solution_test::test_nested_sorted_multi_crate stdout ---- thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["crate::a", "crate::b", "std::string::String"]`, right: `["crate::{\n a,\n b,\n}\n", "std::{\n string::{\n String,\n },\n}\n"]`', tests/solution_test.rs:414:5 ---- solution_test::test_nested_sorted_self stdout ---- thread 'solution_test::test_nested_sorted_self' panicked at 'assertion failed: `(left == right)` left: `["my_crate::a", "my_crate::b", "my_crate::b::B1", "my_crate::b::B2", "my_crate::c"]`, 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_basic solution_test::test_nested_deep 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. 7 passed; 13 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`