Решение на Форматиране на импорти от Станислав Стаматов
Към профила на Станислав Стаматов
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Import<'a>(pub &'a [&'a str]);
#[derive(PartialEq, Eq)]
pub enum Order {
Original,
Sorted,
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let vec_imports = imports.to_vec();
match order {
Order::Original => {
let new_vec: Vec<String> = vec_imports
.iter()
.map(|import| import.0.join("::"))
.collect();
new_vec
}
Order::Sorted => {
let new_vec = merge_sort(&vec_imports);
let res: Vec<String> = new_vec.iter().map(|import| import.0.join("::")).collect();
res
}
}
}
fn merge_sort<'a, T>(list: &Vec<T>) -> Vec<T>
where
T: Clone + Eq + Ord,
{
if list.len() <= 1 {
return list.clone();
}
let mid = list.len() / 2;
let left = merge_sort(&list[..mid].to_vec());
let right = merge_sort(&list[mid..].to_vec());
merge_sorted(&left, &right)
}
fn merge_sorted<'a, T>(left: &[T], right: &[T]) -> Vec<T>
where
T: Clone + Eq + Ord,
{
let mut result = Vec::with_capacity(left.len() + right.len());
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] <= right[j] {
result.push(left[i].clone());
i += 1;
} else {
result.push(right[j].clone());
j += 1;
}
}
while i < left.len() {
result.push(left[i].clone());
i += 1;
}
while j < right.len() {
result.push(right[j].clone());
j += 1;
}
result
}
#[test]
fn test_basic() {
let imports = &[
Import(&["my_crate", "a"]),
Import(&["my_crate", "b", "B1"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "c"]),
];
assert_eq!(
format_flat(imports, Order::Sorted),
&[
"my_crate::a",
"my_crate::b::B1",
"my_crate::b::B2",
"my_crate::c",
]
);
}
fn main() {}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-xxei7u/solution)
warning: function `main` is never used
--> src/lib.rs:89:4
|
89 | fn main() {}
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (lib) generated 1 warning
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:122:16
|
122 | assert_eq!(format_nested(imports, Order::Original), Vec::<String>::new());
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:123:16
|
123 | assert_eq!(format_nested(imports, Order::Sorted), Vec::<String>::new());
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:132:16
|
132 | assert_eq!(format_nested(imports, Order::Original), vec!["my_crate\n"]);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:133:16
|
133 | assert_eq!(format_nested(imports, Order::Sorted), vec!["my_crate\n"]);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:140:16
|
140 | assert_eq!(format_nested(imports, Order::Original), vec!["std::{\n a,\n}\n"]);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:141:16
|
141 | assert_eq!(format_nested(imports, Order::Sorted), vec!["std::{\n a,\n}\n"]);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:160:16
|
160 | assert_eq!(format_nested(imports, Order::Original), expected);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:161:16
|
161 | assert_eq!(format_nested(imports, Order::Sorted), expected);
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:174:9
|
174 | format_nested(imports, Order::Original),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:199:9
|
199 | format_nested(imports, Order::Original),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:228:9
|
228 | format_nested(imports, Order::Sorted),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:253:9
|
253 | format_nested(imports, Order::Sorted),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:284:9
|
284 | format_nested(imports, Order::Original),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:310:9
|
310 | format_nested(imports, Order::Sorted),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:335:9
|
335 | format_nested(imports, Order::Original),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:361:9
|
361 | format_nested(imports, Order::Sorted),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:386:9
|
386 | format_nested(imports, Order::Original),
| ^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `format_nested` in this scope
--> tests/solution_test.rs:415:9
|
415 | format_nested(imports, Order::Sorted),
| ^^^^^^^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `solution` due to 18 previous errors
