From d473a328207a20296d033681d52a041f0103c2ee Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 18 Jul 2024 17:15:12 +0200 Subject: [PATCH] add reference_of!() macro --- src/model/reference.rs | 24 ++++++++++++++++++++++++ src/model/valid.rs | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/model/reference.rs b/src/model/reference.rs index 8508b06..04bdd1a 100644 --- a/src/model/reference.rs +++ b/src/model/reference.rs @@ -84,3 +84,27 @@ pub trait Referencable { Reference(format!("{}::{}", Self::collection_name(), self.id())) } } + +/// Get a `Reference` of `Model` with specific ID +/// +/// # Parameters +/// * `model`: The `Model` to look for +/// * `id`: The ID of the `Model` +/// +/// # Example +/// ```ignore +/// let myref: Option = reference_of!(Person, "someid"); +/// ``` +#[macro_export] +macro_rules! reference_of { + ($model:ident, $id:ident) => {{ + $model::get_partial($id, &serde_json::json!({})) + .await + .map(|x| x.reference()) + }}; + ($model:ident, $id:literal) => {{ + $model::get_partial($id, &serde_json::json!({})) + .await + .map(|x| x.reference()) + }}; +} diff --git a/src/model/valid.rs b/src/model/valid.rs index 1de7187..82f1fc4 100644 --- a/src/model/valid.rs +++ b/src/model/valid.rs @@ -18,7 +18,7 @@ macro_rules! validate { /// It will check all supplied types and return `false` if none are matching. /// /// # Example -/// ``` +/// ```ignore /// fn validate(&self) -> bool { /// assert_reference_of!(self.owner, Person); /// true