MicroTable, a table like in databases

I made a crate because I needed some kind of data structure and could not find anything in public crates. Please look at its source code, and feedback is welcome.

I worked with graphs and needed to search edges by both unique ids, and vertice, and also some attributes. So I made this struct and already use it myself.

The struct also supports (de)serializing with Serde.

The code works and all tests pass. Coverage is close to 100%.

Structs setup:

use microtable::{MicroTable, MicroRecord};

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct ScienceId(usize);
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct AuthorId(usize);
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct BookId(usize);

struct Book {
        id: BookId,
        title: String,
        science: ScienceId,
        author: AuthorId,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
enum BookCategory { Science(ScienceId), Author(AuthorId) }

impl MicroRecord for Book {
	type Key = BookId; // key must be unique
	type Category = BookCategory; // categories may be duplicated
	fn categories(&self) -> Vec<Self::Category> {
		vec![BookCategory::Science(self.science.clone()), BookCategory::Author(self.author.clone())]
	}
	fn key(&self) -> Self::Key { self.id.clone() }
}
...
struct MyData {
	books: MicroTable<Book>
}

Usage:

    let my_data = MyData::new();
	for b in vec![Book { id: BookId(1), title: "Book №1".into(), science: ScienceId(2), author: AuthorId(1) },
		Book { id: BookId(2), title: "Book №2".into(), science: ScienceId(1), author: AuthorId(2) },
		Book { id: BookId(3), title: "Book №3".into(), science: ScienceId(2), author: AuthorId(3) }
	].into_iter() {
		my_data.books.insert(b).unwrap(); // may return Err if keys collide.
	}
	...
	for book in my_data.find(ScienceId(2)) {
		println!("book {book:?} science {:?}", book.science);
	}
	println!("book: {:?}", my_data.get(&BookId(1)));
1 Like