Решение на Форматиране на импорти от Георги Йорданов

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

Към профила на Георги Йорданов

Резултати

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

Код

#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
pub struct Import<'a>(pub &'a [&'a str]);
impl<'a> Import<'a> {
pub fn print(&self) {
for i in self.0 {
print!("{}", i);
}
print!("\n");
}
pub fn format(&self) -> String {
self.0.join("::")
}
}
pub enum Order {
Original,
Sorted,
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut result : Vec<String> = vec![];
let mut imports_vec = imports.to_vec();
imports_vec.dedup();
match order {
Order::Original => {},
Order::Sorted => imports_vec.sort(),
}
for i in imports_vec {
result.push(i.format());
}
result
}
#[derive(Clone, Debug)]
struct Node {
data: String,
children: Vec<Node>
}
impl Node {
pub fn new(str: String) -> Node {
Node { data: str, children: vec![] }
}
pub fn add_child(&mut self, str: String) {
self.children.push(Node::new(str));
}
pub fn add_child_front(&mut self, str: String) {
self.children.insert(0, Node::new(str));
}
pub fn format_strings(self, depth: usize) -> String {
if self.children.is_empty() {
String::from(vec![" "; (depth - 1) * 4].into_iter().collect::<String>())
+ &String::from(self.data) + &String::from(",\n")
}
else {
String::from(vec![" "; (depth - 1) * 4].into_iter().collect::<String>())
+ &String::from(self.data) + &String::from("::{\n")
+ &self.children.into_iter().map(|x| x.format_strings(depth + 1)).collect::<Vec<String>>().into_iter().collect::<String>()
+ &String::from(vec![" "; (depth - 1) * 4].into_iter().collect::<String>())
+ &match depth {
1 => String::from("}\n"),
_ => String::from("},\n")
}
}
}
pub fn update_modules(&mut self, imports: &Vec<Import>) {
for child in self.children.iter_mut() {
for c in child.children.iter_mut() {
if !c.children.is_empty() && imports.contains(&Import(&[&child.data, &c.data])) {
c.add_child_front(String::from("self"));
}
}
}
}
}
fn insert_import(import: &[&str], root: &mut Node) {
if import.len() == 0 {
return;
}
let child_count = root.children.len();
for child in root.children.iter_mut() {
if import[0] == child.data {
insert_import(import.get(1..).unwrap(), child);
return;
}
}
root.add_child(String::from(import[0]));
insert_import(import.get(1..).unwrap(), &mut root.children[child_count]);
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result : Vec<String> = vec![];
let mut imports_vec = imports.to_vec();
imports_vec.dedup();
match order {
Order::Original => {},
Order::Sorted => imports_vec.sort(),
}
let mut root : Node = Node::new(String::from(""));
for import in imports_vec.iter() {
insert_import(import.0, &mut root);
}
root.update_modules(&imports_vec);
for child in root.children {
result.push(child.format_strings(1));
}
result
}

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

Compiling solution v0.1.0 (/tmp/d20241203-1739405-l9l6os/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.23s
     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 ... FAILED
test solution_test::test_flat_sorted ... ok
test solution_test::test_flat_sorted_duplicates ... FAILED
test solution_test::test_nested_basic ... ok
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_deep ... ok
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 ... ok
test solution_test::test_nested_original_multi_crate ... ok
test solution_test::test_nested_original_self ... ok
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 ... ok

failures:

---- solution_test::test_flat_original_duplicates stdout ----
thread 'solution_test::test_flat_original_duplicates' panicked at 'assertion failed: `(left == right)`
  left: `["std::string::String", "std::iter::once", "std::iter", "std::iter::repeat", "std::string::String"]`,
 right: `["std::string::String", "std::iter::once", "std::iter", "std::iter::repeat"]`', tests/solution_test.rs:44:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_flat_sorted_duplicates stdout ----
thread 'solution_test::test_flat_sorted_duplicates' panicked at 'assertion failed: `(left == right)`
  left: `["std::iter", "std::iter::once", "std::iter::repeat", "std::string::String", "std::string::String"]`,
 right: `["std::iter", "std::iter::once", "std::iter::repeat", "std::string::String"]`', tests/solution_test.rs:88: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"]`,
 right: `["my_crate\n"]`', tests/solution_test.rs:132:5


failures:
    solution_test::test_flat_original_duplicates
    solution_test::test_flat_sorted_duplicates
    solution_test::test_nested_only_crate

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 коментара)

Георги качи първо решение на 02.12.2024 16:30 (преди 10 месеца)