khonsulabs / bonsaidb

A developer-friendly document database that grows with you, written in Rust
https://bonsaidb.io/
Apache License 2.0
1.01k stars 37 forks source link

Add derive macro for Schema #164

Closed ecton closed 2 years ago

ecton commented 2 years ago

Proposal:

#[derive(Schema)]
#[schema(name = "Name", authority = "Authority", collections = [A, B, C])]
pub enum MySchema {}

expands to:

impl Schema for MySchema {
    fn schema_name() -> SchemaName {
        SchemaName::new("Authority", "Name")
    }

    fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
        schema.define_collection::<A>()?;
        schema.define_collection::<B>()?;
        schema.define_collection::<C>()?;
        Ok(())
    }
}

Implementation should be very similar to #138.

The macro needs to be able to have the location of bonsaidb_core specified as an optional attribute. By default, it should use ::bonsaidb::core as the path to bonsaidb_core, but a user should be able to override it based on their usage. E.g., #[schema(core = bonsaidb_server::core)].