khonsulabs / bonsaidb

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

Derive with features #192

Closed ecton closed 2 years ago

ecton commented 2 years ago

When implementing a few of the new derives internally, I noticed one pattern I think could be improved:

#[derive(Debug, Schema)]
#[schema(name = "hosted", authority = "khonsulabs", core = bonsaidb_core)]
#[cfg_attr(feature = "acme", schema(collections = [TlsCertificate, crate::server::acme::AcmeAccount]))]
#[cfg_attr(not(feature = "acme"), schema(collections = [TlsCertificate]))]
pub struct Hosted;

I think that this would look cleaner if written this way:

#[derive(Debug, Schema)]
#[schema(name = "hosted", authority = "khonsulabs", collections = [TlsCertificate], core = bonsaidb_core)]
#[cfg_attr(feature = "acme", schema(collections = [crate::server::acme::AcmeAccount]))]
pub struct Hosted;

This would require the Schema derive to allow collections to be specified multiple times. I don't think it's as likely for views to be feature-dependent, but I can see an argument to support the same thing for Collection(views).

@ModProg

ModProg commented 2 years ago

makes sense. I might even implement this as the default behavior of attribute-derive (maybe I should rename that to something like structattr). Or add a flag aggregation. This could be useful for other types as well, but exspecially for Vecs.