mirror of
https://github.com/speatzle/nfsense.git
synced 2025-06-28 07:19:37 +00:00
Implement MarcoDB Option and Enum Option Referencing
This commit is contained in:
parent
e3ab77008a
commit
2d23b26227
1 changed files with 90 additions and 12 deletions
|
@ -74,6 +74,11 @@ macro_rules! macro_db {
|
||||||
path: stringify!(config.$($path_referencing).+).to_string(),
|
path: stringify!(config.$($path_referencing).+).to_string(),
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
(O ()) => {
|
||||||
|
/*
|
||||||
|
TODO
|
||||||
|
*/
|
||||||
|
};
|
||||||
(M ()) => {
|
(M ()) => {
|
||||||
config.$($path_referencing).+.iter().filter(|e| e.$field_name.contains(&self.name)).for_each(|e| by.push(ReferencedBy{
|
config.$($path_referencing).+.iter().filter(|e| e.$field_name.contains(&self.name)).for_each(|e| by.push(ReferencedBy{
|
||||||
name: e.name.clone(),
|
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
|
(EM
|
||||||
($enum_name:ident,
|
($enum_name:ident,
|
||||||
$enum_type: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,
|
( M,
|
||||||
$field_name:ident,
|
$field_name:ident,
|
||||||
$thing_referencing:ty,
|
$thing_referencing:ty,
|
||||||
|
@ -194,13 +235,11 @@ macro_rules! macro_db_link {
|
||||||
impl $enum_type {
|
impl $enum_type {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn $fn_name(&self, config: Config) -> $thing_referenced {
|
pub fn $fn_name(&self, config: Config) -> $thing_referenced {
|
||||||
|
if let $enum_type::$enum_variant { $field_name, .. } = self.clone() {
|
||||||
let index = config.$($path_referenced).+.iter().position(
|
let index = config.$($path_referenced).+.iter().position(
|
||||||
|e| {
|
|e| {
|
||||||
if let $enum_type::$enum_variant { $field_name, .. } = self.clone() {
|
|
||||||
return *e.name == $field_name;
|
return *e.name == $field_name;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
match index {
|
match index {
|
||||||
|
@ -208,7 +247,46 @@ macro_rules! macro_db_link {
|
||||||
// This is fine since the config always has to validated before commiting
|
// This is fine since the config always has to validated before commiting
|
||||||
None => panic!("Referenced Thing: does not exist (from Enum)"),
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue