add api
This commit is contained in:
parent
9b56a434e7
commit
9784c3cac6
1 changed files with 17 additions and 0 deletions
17
src/lib.rs
17
src/lib.rs
|
@ -97,3 +97,20 @@ macro_rules! id_of {
|
|||
$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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue