I'm in the very early process of writing a simple database (somewhat similar to mongodb) and I'm thinking about how I should implement a query to the database in a typed language like Rust.
I want to have Collection<T> that contains Vec<T> where T is a document. In order to get a mutable reference to change T I would need to query Vec<T> by only certain fields of T if that makes sense. Should I do this with a derive macro?
I would like to do something like this:
struct Collection<T> {
inner: Vec<T>
}
impl<T: Query> Collection<T> {
pub fn query(&mut self, q: impl Query) -> Vec<&mut T> {
// ????
}
}
#[derive(Query)]
struct Foo {
bar: String,
baz: String,
zig: String,
}
// then I might try:
let query = Foo {
bar: "something something".to_string(),
..Something::something(),
};
// res only has Foo in collection's inner Vec<Foo> that matches query.bar:
let res: Vec<Foo> = collection.query(query);
Is this the best way to do a query without all the fields filled? If this isn't possible should I just try to serialize it to json and do queries by json strings? (that would be slow and complicated, though the data will actually already be stored as json on the web backend)