From 2d23b2622700b115fd21df4066e8a89a95d91e35 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 11 Feb 2024 21:39:18 +0100 Subject: [PATCH] Implement MarcoDB Option and Enum Option Referencing --- src/definitions/macro_db.rs | 102 +++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 12 deletions(-) diff --git a/src/definitions/macro_db.rs b/src/definitions/macro_db.rs index eba36fc..2c63739 100644 --- a/src/definitions/macro_db.rs +++ b/src/definitions/macro_db.rs @@ -74,6 +74,11 @@ macro_rules! macro_db { path: stringify!(config.$($path_referencing).+).to_string(), })); }; + (O ()) => { + /* + TODO + */ + }; (M ()) => { config.$($path_referencing).+.iter().filter(|e| e.$field_name.contains(&self.name)).for_each(|e| by.push(ReferencedBy{ name: e.name.clone(), @@ -99,6 +104,16 @@ macro_rules! macro_db { } */ }; + (EO + ($enum_name:ident, + $enum_type:ident, + $enum_variant:ident, + $fn_name:ident ) + ) => { + /* + TODO + */ + }; (EM ($enum_name:ident, $enum_type:ident, @@ -157,6 +172,32 @@ macro_rules! macro_db_link { } } }; + ( O, + $field_name:ident, + $thing_referencing:ty, + $thing_referenced:ty, + $( $path_referenced:ident ).+ + () + ) => { + impl $thing_referencing { + #[allow(dead_code)] + pub fn $field_name(&self, config: Config) -> Option<$thing_referenced> { + + match self.$field_name.clone() { + None => None, + Some(s) => { + let index = config.$($path_referenced).+.iter().position(|e| *e.name == s); + + match index { + Some(i) => Some(config.$($path_referenced).+[i].clone()), + // This is fine since the config always has to validated before commiting + None => panic!("Referenced Thing: '{:?}' does not exist ", self.$field_name), + } + } + } + } + } + }; ( M, $field_name:ident, $thing_referencing:ty, @@ -194,21 +235,58 @@ macro_rules! macro_db_link { impl $enum_type { #[allow(dead_code)] pub fn $fn_name(&self, config: Config) -> $thing_referenced { - let index = config.$($path_referenced).+.iter().position( - |e| { - if let $enum_type::$enum_variant { $field_name, .. } = self.clone() { - return *e.name == $field_name; - } - return false; - } - ); + if let $enum_type::$enum_variant { $field_name, .. } = self.clone() { + let index = config.$($path_referenced).+.iter().position( + |e| { + return *e.name == $field_name; + } + ); - match index { - Some(i) => config.$($path_referenced).+[i].clone(), - // This is fine since the config always has to validated before commiting - None => panic!("Referenced Thing: does not exist (from Enum)"), + match index { + Some(i) => config.$($path_referenced).+[i].clone(), + // This is fine since the config always has to validated before commiting + None => panic!("Referenced Thing: does not exist (from Enum)"), + } + } else { + panic!("Called Referenced for wrong Enum Variant") } + } + } + }; + ( EO, + $field_name:ident, + $thing_referencing:ty, + $thing_referenced:ty, + $( $path_referenced:ident ).+ + ($enum_name:ident, + $enum_type:ident, + $enum_variant:ident, + $fn_name:ident ) + ) => { + // Unfortunetly Enum Variants are not Types, which is why we can't impl on the Variant and need seperate function names (since multiple variant could have the same field name) + impl $enum_type { + #[allow(dead_code)] + pub fn $fn_name(&self, config: Config) -> Option<$thing_referenced> { + if let $enum_type::$enum_variant { $field_name, .. } = self.clone() { + match $field_name { + None => None, + Some(f) => { + let index = config.$($path_referenced).+.iter().position( + |e| { + return *e.name == f; + } + ); + match index { + Some(i) => Some(config.$($path_referenced).+[i].clone()), + // This is fine since the config always has to validated before commiting + None => panic!("Referenced Thing: does not exist (from Enum)"), + } + } + } + } else { + panic!("Called Referenced for wrong Enum Variant") + } } } };