From 84096a02c0464077e55fe43fe11a891bf8a9435f Mon Sep 17 00:00:00 2001 From: baoyachi Date: Wed, 13 Nov 2024 15:15:11 +0800 Subject: [PATCH 1/2] feat(HookExt): Add extended hook functionality with custom deny lists - Added `HookExt` trait to provide extended hook functionality. - Implemented `HookExt` for functions, tuples, and `ShadowHook` struct. - Each implementation provides methods to return the default deny list and the inner hook function. This change allows more flexible handling of hooks and deny lists, enabling better customization and control over the hook behavior. --- src/hook.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 10 +++++--- 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/hook.rs diff --git a/src/hook.rs b/src/hook.rs new file mode 100644 index 0000000..8d82620 --- /dev/null +++ b/src/hook.rs @@ -0,0 +1,70 @@ +use crate::{default_deny, SdResult, ShadowConst}; +use std::collections::BTreeSet; +use std::fs::File; + +/// A trait that extends the functionality of hooks. +/// It provides methods to get the default deny list and the inner hook function. +pub trait HookExt { + /// Returns the default deny list. + fn default_deny(&self) -> BTreeSet; + + /// Returns a reference to the inner hook function. + fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()>; +} + +/// Implement the `HookExt` trait for any function that takes a `&File` and returns a `SdResult<()>`. +impl HookExt for F +where + F: Fn(&File) -> SdResult<()>, +{ + /// Returns the default deny list using the `default_deny` function from the crate. + fn default_deny(&self) -> BTreeSet { + default_deny() + } + + /// Returns a reference to the function itself. + fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> { + self + } +} + +/// Implement the `HookExt` trait for a tuple containing a function and a deny list. +impl HookExt for (F, BTreeSet) +where + F: Fn(&File) -> SdResult<()>, +{ + /// Returns the deny list stored in the second element of the tuple. + fn default_deny(&self) -> BTreeSet { + self.1.clone() + } + + /// Returns a reference to the function stored in the first element of the tuple. + fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> { + &self.0 + } +} + +/// A struct representing a shadow hook with an inner function and a deny list. +pub struct ShadowHook { + /// The inner function that will be used as the hook. + pub hook: F, + + /// The deny list associated with this hook. + pub deny: BTreeSet, +} + +/// Implement the `HookExt` trait for the `ShadowHook` struct. +impl HookExt for ShadowHook +where + F: Fn(&File) -> SdResult<()>, +{ + /// Returns the deny list associated with this `ShadowHook`. + fn default_deny(&self) -> BTreeSet { + self.deny.clone() + } + + /// Returns a reference to the inner function of this `ShadowHook`. + fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> { + &self.hook + } +} diff --git a/src/lib.rs b/src/lib.rs index 3f28b86..7dc6c70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,6 +156,7 @@ mod env; mod err; mod gen_const; mod git; +mod hook; /// Re-exported from the is_debug crate pub use is_debug::*; @@ -178,6 +179,7 @@ use crate::gen_const::{ }; pub use err::{SdResult, ShadowError}; +use crate::hook::HookExt; pub use {build::ShadowConst, env::*, git::*}; pub trait Format { @@ -292,10 +294,10 @@ pub fn new_deny(deny_const: BTreeSet) -> SdResult<()> { /// pub fn new_hook(f: F) -> SdResult<()> where - F: FnOnce(&File) -> SdResult<()>, + F: HookExt, { - let shadow = Shadow::build(default_deny())?; - shadow.hook(f) + let shadow = Shadow::build(f.default_deny())?; + shadow.hook(f.hook_inner()) } /// Returns the contents of [`std::env::vars`] as an ordered map. @@ -328,7 +330,7 @@ impl Shadow { /// The hook function is run as well, allowing it to append to `shadow-rs`'s output. pub fn hook(&self, f: F) -> SdResult<()> where - F: FnOnce(&File) -> SdResult<()>, + F: Fn(&File) -> SdResult<()>, { let desc = r#"/// Below code generated by project custom from by build.rs"#; writeln!(&self.f, "\n{desc}\n")?; From bad046d7a0e012b0c4b383400bd09497a97d5e4c Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Nov 2024 09:45:21 +0800 Subject: [PATCH 2/2] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1623f11..727d746 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.35.2" +version = "0.36.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project"