This commit is contained in:
JMARyA 2024-08-30 11:53:10 +02:00
parent 9b56a434e7
commit 9784c3cac6
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -97,3 +97,20 @@ macro_rules! id_of {
$crate::mongodb::bson::doc! { "_id": $id} $crate::mongodb::bson::doc! { "_id": $id}
}; };
} }
/// A trait to generate a Model API representation in JSON format.
pub trait ToAPI: Sized {
/// Generate public API JSON
fn api(&self) -> impl std::future::Future<Output = serde_json::Value>;
}
/// Converts a slice of items implementing the `ToAPI` trait into a `Vec` of JSON values.
pub async fn vec_to_api(items: &[impl ToAPI]) -> Vec<serde_json::Value> {
let mut ret = Vec::with_capacity(items.len());
for e in items {
ret.push(e.api().await);
}
ret
}